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.

285 lines
9.1KB

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