flacdec.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Raw FLAC demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavcodec/flac.h"
  22. #include "avformat.h"
  23. #include "id3v2.h"
  24. #include "internal.h"
  25. #include "rawdec.h"
  26. #include "oggdec.h"
  27. #include "vorbiscomment.h"
  28. #include "libavcodec/bytestream.h"
  29. #define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0)
  30. static int parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
  31. {
  32. const CodecMime *mime = ff_id3v2_mime_tags;
  33. enum AVCodecID id = AV_CODEC_ID_NONE;
  34. uint8_t mimetype[64], *desc = NULL, *data = NULL;
  35. AVIOContext *pb = NULL;
  36. AVStream *st;
  37. int type, width, height;
  38. int len, ret = 0;
  39. pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
  40. if (!pb)
  41. return AVERROR(ENOMEM);
  42. /* read the picture type */
  43. type = avio_rb32(pb);
  44. if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
  45. av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
  46. if (s->error_recognition & AV_EF_EXPLODE) {
  47. RETURN_ERROR(AVERROR_INVALIDDATA);
  48. }
  49. type = 0;
  50. }
  51. /* picture mimetype */
  52. len = avio_rb32(pb);
  53. if (len <= 0 ||
  54. avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
  55. av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
  56. "picture.\n");
  57. if (s->error_recognition & AV_EF_EXPLODE)
  58. ret = AVERROR_INVALIDDATA;
  59. goto fail;
  60. }
  61. mimetype[len] = 0;
  62. while (mime->id != AV_CODEC_ID_NONE) {
  63. if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
  64. id = mime->id;
  65. break;
  66. }
  67. mime++;
  68. }
  69. if (id == AV_CODEC_ID_NONE) {
  70. av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
  71. mimetype);
  72. if (s->error_recognition & AV_EF_EXPLODE)
  73. ret = AVERROR_INVALIDDATA;
  74. goto fail;
  75. }
  76. /* picture description */
  77. len = avio_rb32(pb);
  78. if (len > 0) {
  79. if (!(desc = av_malloc(len + 1))) {
  80. RETURN_ERROR(AVERROR(ENOMEM));
  81. }
  82. if (avio_read(pb, desc, len) != len) {
  83. av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
  84. if (s->error_recognition & AV_EF_EXPLODE)
  85. ret = AVERROR(EIO);
  86. goto fail;
  87. }
  88. desc[len] = 0;
  89. }
  90. /* picture metadata */
  91. width = avio_rb32(pb);
  92. height = avio_rb32(pb);
  93. avio_skip(pb, 8);
  94. /* picture data */
  95. len = avio_rb32(pb);
  96. if (len <= 0) {
  97. av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
  98. if (s->error_recognition & AV_EF_EXPLODE)
  99. ret = AVERROR_INVALIDDATA;
  100. goto fail;
  101. }
  102. if (!(data = av_malloc(len))) {
  103. RETURN_ERROR(AVERROR(ENOMEM));
  104. }
  105. if (avio_read(pb, data, len) != len) {
  106. av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
  107. if (s->error_recognition & AV_EF_EXPLODE)
  108. ret = AVERROR(EIO);
  109. goto fail;
  110. }
  111. st = avformat_new_stream(s, NULL);
  112. if (!st) {
  113. RETURN_ERROR(AVERROR(ENOMEM));
  114. }
  115. av_init_packet(&st->attached_pic);
  116. st->attached_pic.data = data;
  117. st->attached_pic.size = len;
  118. st->attached_pic.destruct = av_destruct_packet;
  119. st->attached_pic.stream_index = st->index;
  120. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  121. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  122. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  123. st->codec->codec_id = id;
  124. st->codec->width = width;
  125. st->codec->height = height;
  126. av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
  127. if (desc)
  128. av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
  129. av_freep(&pb);
  130. return 0;
  131. fail:
  132. av_freep(&desc);
  133. av_freep(&data);
  134. av_freep(&pb);
  135. return ret;
  136. }
  137. static int flac_read_header(AVFormatContext *s)
  138. {
  139. int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
  140. uint8_t header[4];
  141. uint8_t *buffer=NULL;
  142. AVStream *st = avformat_new_stream(s, NULL);
  143. if (!st)
  144. return AVERROR(ENOMEM);
  145. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  146. st->codec->codec_id = AV_CODEC_ID_FLAC;
  147. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  148. /* the parameters will be extracted from the compressed bitstream */
  149. /* if fLaC marker is not found, assume there is no header */
  150. if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  151. avio_seek(s->pb, -4, SEEK_CUR);
  152. return 0;
  153. }
  154. /* process metadata blocks */
  155. while (!url_feof(s->pb) && !metadata_last) {
  156. avio_read(s->pb, header, 4);
  157. avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
  158. &metadata_size);
  159. switch (metadata_type) {
  160. /* allocate and read metadata block for supported types */
  161. case FLAC_METADATA_TYPE_STREAMINFO:
  162. case FLAC_METADATA_TYPE_CUESHEET:
  163. case FLAC_METADATA_TYPE_PICTURE:
  164. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  165. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  166. if (!buffer) {
  167. return AVERROR(ENOMEM);
  168. }
  169. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  170. RETURN_ERROR(AVERROR(EIO));
  171. }
  172. break;
  173. /* skip metadata block for unsupported types */
  174. default:
  175. ret = avio_skip(s->pb, metadata_size);
  176. if (ret < 0)
  177. return ret;
  178. }
  179. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  180. FLACStreaminfo si;
  181. /* STREAMINFO can only occur once */
  182. if (found_streaminfo) {
  183. RETURN_ERROR(AVERROR_INVALIDDATA);
  184. }
  185. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  186. RETURN_ERROR(AVERROR_INVALIDDATA);
  187. }
  188. found_streaminfo = 1;
  189. st->codec->extradata = buffer;
  190. st->codec->extradata_size = metadata_size;
  191. buffer = NULL;
  192. /* get codec params from STREAMINFO header */
  193. avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
  194. /* set time base and duration */
  195. if (si.samplerate > 0) {
  196. avpriv_set_pts_info(st, 64, 1, si.samplerate);
  197. if (si.samples > 0)
  198. st->duration = si.samples;
  199. }
  200. } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  201. uint8_t isrc[13];
  202. uint64_t start;
  203. const uint8_t *offset;
  204. int i, chapters, track, ti;
  205. if (metadata_size < 431)
  206. RETURN_ERROR(AVERROR_INVALIDDATA);
  207. offset = buffer + 395;
  208. chapters = bytestream_get_byte(&offset) - 1;
  209. if (chapters <= 0)
  210. RETURN_ERROR(AVERROR_INVALIDDATA);
  211. for (i = 0; i < chapters; i++) {
  212. if (offset + 36 - buffer > metadata_size)
  213. RETURN_ERROR(AVERROR_INVALIDDATA);
  214. start = bytestream_get_be64(&offset);
  215. track = bytestream_get_byte(&offset);
  216. bytestream_get_buffer(&offset, isrc, 12);
  217. isrc[12] = 0;
  218. offset += 14;
  219. ti = bytestream_get_byte(&offset);
  220. if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
  221. offset += ti * 12;
  222. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  223. }
  224. av_freep(&buffer);
  225. } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
  226. ret = parse_picture(s, buffer, metadata_size);
  227. av_freep(&buffer);
  228. if (ret < 0) {
  229. av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
  230. return ret;
  231. }
  232. } else {
  233. /* STREAMINFO must be the first block */
  234. if (!found_streaminfo) {
  235. RETURN_ERROR(AVERROR_INVALIDDATA);
  236. }
  237. /* process supported blocks other than STREAMINFO */
  238. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  239. if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
  240. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  241. }
  242. }
  243. av_freep(&buffer);
  244. }
  245. }
  246. return 0;
  247. fail:
  248. av_free(buffer);
  249. return ret;
  250. }
  251. static int flac_probe(AVProbeData *p)
  252. {
  253. uint8_t *bufptr = p->buf;
  254. uint8_t *end = p->buf + p->buf_size;
  255. if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
  256. else return AVPROBE_SCORE_MAX/2;
  257. }
  258. AVInputFormat ff_flac_demuxer = {
  259. .name = "flac",
  260. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  261. .read_probe = flac_probe,
  262. .read_header = flac_read_header,
  263. .read_packet = ff_raw_read_partial_packet,
  264. .flags = AVFMT_GENERIC_INDEX,
  265. .extensions = "flac",
  266. .raw_codec_id = AV_CODEC_ID_FLAC,
  267. };