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.

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