You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
9.7KB

  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. AVBufferRef *data = NULL;
  35. uint8_t mimetype[64], *desc = NULL;
  36. AVIOContext *pb = NULL;
  37. AVStream *st;
  38. int type, width, height;
  39. int len, ret = 0;
  40. pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
  41. if (!pb)
  42. return AVERROR(ENOMEM);
  43. /* read the picture type */
  44. type = avio_rb32(pb);
  45. if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
  46. av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
  47. if (s->error_recognition & AV_EF_EXPLODE) {
  48. RETURN_ERROR(AVERROR_INVALIDDATA);
  49. }
  50. type = 0;
  51. }
  52. /* picture mimetype */
  53. len = avio_rb32(pb);
  54. if (len <= 0 ||
  55. avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
  56. av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
  57. "picture.\n");
  58. if (s->error_recognition & AV_EF_EXPLODE)
  59. ret = AVERROR_INVALIDDATA;
  60. goto fail;
  61. }
  62. mimetype[len] = 0;
  63. while (mime->id != AV_CODEC_ID_NONE) {
  64. if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
  65. id = mime->id;
  66. break;
  67. }
  68. mime++;
  69. }
  70. if (id == AV_CODEC_ID_NONE) {
  71. av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
  72. mimetype);
  73. if (s->error_recognition & AV_EF_EXPLODE)
  74. ret = AVERROR_INVALIDDATA;
  75. goto fail;
  76. }
  77. /* picture description */
  78. len = avio_rb32(pb);
  79. if (len > 0) {
  80. if (!(desc = av_malloc(len + 1))) {
  81. RETURN_ERROR(AVERROR(ENOMEM));
  82. }
  83. if (avio_read(pb, desc, len) != len) {
  84. av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
  85. if (s->error_recognition & AV_EF_EXPLODE)
  86. ret = AVERROR(EIO);
  87. goto fail;
  88. }
  89. desc[len] = 0;
  90. }
  91. /* picture metadata */
  92. width = avio_rb32(pb);
  93. height = avio_rb32(pb);
  94. avio_skip(pb, 8);
  95. /* picture data */
  96. len = avio_rb32(pb);
  97. if (len <= 0) {
  98. av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
  99. if (s->error_recognition & AV_EF_EXPLODE)
  100. ret = AVERROR_INVALIDDATA;
  101. goto fail;
  102. }
  103. if (!(data = av_buffer_alloc(len))) {
  104. RETURN_ERROR(AVERROR(ENOMEM));
  105. }
  106. if (avio_read(pb, data->data, len) != len) {
  107. av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
  108. if (s->error_recognition & AV_EF_EXPLODE)
  109. ret = AVERROR(EIO);
  110. goto fail;
  111. }
  112. st = avformat_new_stream(s, NULL);
  113. if (!st) {
  114. RETURN_ERROR(AVERROR(ENOMEM));
  115. }
  116. av_init_packet(&st->attached_pic);
  117. st->attached_pic.buf = data;
  118. st->attached_pic.data = data->data;
  119. st->attached_pic.size = len;
  120. st->attached_pic.stream_index = st->index;
  121. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  122. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  123. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  124. st->codec->codec_id = id;
  125. st->codec->width = width;
  126. st->codec->height = height;
  127. av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
  128. if (desc)
  129. av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
  130. av_freep(&pb);
  131. return 0;
  132. fail:
  133. av_buffer_unref(&data);
  134. av_freep(&desc);
  135. av_freep(&pb);
  136. return ret;
  137. }
  138. static int flac_read_header(AVFormatContext *s)
  139. {
  140. int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
  141. uint8_t header[4];
  142. uint8_t *buffer=NULL;
  143. AVStream *st = avformat_new_stream(s, NULL);
  144. if (!st)
  145. return AVERROR(ENOMEM);
  146. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  147. st->codec->codec_id = AV_CODEC_ID_FLAC;
  148. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  149. /* the parameters will be extracted from the compressed bitstream */
  150. /* if fLaC marker is not found, assume there is no header */
  151. if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  152. avio_seek(s->pb, -4, SEEK_CUR);
  153. return 0;
  154. }
  155. /* process metadata blocks */
  156. while (!url_feof(s->pb) && !metadata_last) {
  157. avio_read(s->pb, header, 4);
  158. avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
  159. &metadata_size);
  160. switch (metadata_type) {
  161. /* allocate and read metadata block for supported types */
  162. case FLAC_METADATA_TYPE_STREAMINFO:
  163. case FLAC_METADATA_TYPE_CUESHEET:
  164. case FLAC_METADATA_TYPE_PICTURE:
  165. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  166. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  167. if (!buffer) {
  168. return AVERROR(ENOMEM);
  169. }
  170. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  171. RETURN_ERROR(AVERROR(EIO));
  172. }
  173. break;
  174. /* skip metadata block for unsupported types */
  175. default:
  176. ret = avio_skip(s->pb, metadata_size);
  177. if (ret < 0)
  178. return ret;
  179. }
  180. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  181. FLACStreaminfo si;
  182. /* STREAMINFO can only occur once */
  183. if (found_streaminfo) {
  184. RETURN_ERROR(AVERROR_INVALIDDATA);
  185. }
  186. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  187. RETURN_ERROR(AVERROR_INVALIDDATA);
  188. }
  189. found_streaminfo = 1;
  190. st->codec->extradata = buffer;
  191. st->codec->extradata_size = metadata_size;
  192. buffer = NULL;
  193. /* get codec params from STREAMINFO header */
  194. avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
  195. /* set time base and duration */
  196. if (si.samplerate > 0) {
  197. avpriv_set_pts_info(st, 64, 1, si.samplerate);
  198. if (si.samples > 0)
  199. st->duration = si.samples;
  200. }
  201. } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  202. uint8_t isrc[13];
  203. uint64_t start;
  204. const uint8_t *offset;
  205. int i, chapters, track, ti;
  206. if (metadata_size < 431)
  207. RETURN_ERROR(AVERROR_INVALIDDATA);
  208. offset = buffer + 395;
  209. chapters = bytestream_get_byte(&offset) - 1;
  210. if (chapters <= 0)
  211. RETURN_ERROR(AVERROR_INVALIDDATA);
  212. for (i = 0; i < chapters; i++) {
  213. if (offset + 36 - buffer > metadata_size)
  214. RETURN_ERROR(AVERROR_INVALIDDATA);
  215. start = bytestream_get_be64(&offset);
  216. track = bytestream_get_byte(&offset);
  217. bytestream_get_buffer(&offset, isrc, 12);
  218. isrc[12] = 0;
  219. offset += 14;
  220. ti = bytestream_get_byte(&offset);
  221. if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
  222. offset += ti * 12;
  223. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  224. }
  225. av_freep(&buffer);
  226. } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
  227. ret = parse_picture(s, buffer, metadata_size);
  228. av_freep(&buffer);
  229. if (ret < 0) {
  230. av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
  231. return ret;
  232. }
  233. } else {
  234. /* STREAMINFO must be the first block */
  235. if (!found_streaminfo) {
  236. RETURN_ERROR(AVERROR_INVALIDDATA);
  237. }
  238. /* process supported blocks other than STREAMINFO */
  239. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  240. if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
  241. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  242. }
  243. }
  244. av_freep(&buffer);
  245. }
  246. }
  247. return 0;
  248. fail:
  249. av_free(buffer);
  250. return ret;
  251. }
  252. static int flac_probe(AVProbeData *p)
  253. {
  254. if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
  255. return 0;
  256. return AVPROBE_SCORE_EXTENSION;
  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. };