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.

297 lines
9.7KB

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