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.

430 lines
13KB

  1. /*
  2. * raw FLAC muxer
  3. * Copyright (c) 2006-2009 Justin Ruggles
  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 "libavutil/channel_layout.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavcodec/flac.h"
  25. #include "libavcodec/packet_internal.h"
  26. #include "avformat.h"
  27. #include "avio_internal.h"
  28. #include "flacenc.h"
  29. #include "id3v2.h"
  30. #include "internal.h"
  31. #include "vorbiscomment.h"
  32. typedef struct FlacMuxerContext {
  33. const AVClass *class;
  34. int write_header;
  35. int audio_stream_idx;
  36. int waiting_pics;
  37. /* audio packets are queued here until we get all the attached pictures */
  38. AVPacketList *queue, *queue_end;
  39. /* updated streaminfo sent by the encoder at the end */
  40. uint8_t streaminfo[FLAC_STREAMINFO_SIZE];
  41. int updated_streaminfo;
  42. unsigned attached_types;
  43. } FlacMuxerContext;
  44. static int flac_write_block_padding(AVIOContext *pb, unsigned int n_padding_bytes,
  45. int last_block)
  46. {
  47. avio_w8(pb, last_block ? 0x81 : 0x01);
  48. avio_wb24(pb, n_padding_bytes);
  49. ffio_fill(pb, 0, n_padding_bytes);
  50. return 0;
  51. }
  52. static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m,
  53. int last_block, int bitexact)
  54. {
  55. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  56. int64_t len;
  57. ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
  58. len = ff_vorbiscomment_length(*m, vendor, NULL, 0);
  59. if (len >= ((1<<24) - 4))
  60. return AVERROR(EINVAL);
  61. avio_w8(pb, last_block ? 0x84 : 0x04);
  62. avio_wb24(pb, len);
  63. ff_vorbiscomment_write(pb, *m, vendor, NULL, 0);
  64. return 0;
  65. }
  66. static int flac_write_picture(struct AVFormatContext *s, AVPacket *pkt)
  67. {
  68. FlacMuxerContext *c = s->priv_data;
  69. AVIOContext *pb = s->pb;
  70. const AVPixFmtDescriptor *pixdesc;
  71. const CodecMime *mime = ff_id3v2_mime_tags;
  72. AVDictionaryEntry *e;
  73. const char *mimetype = NULL, *desc = "";
  74. const AVStream *st = s->streams[pkt->stream_index];
  75. int i, mimelen, desclen, type = 0, blocklen;
  76. if (!pkt->data)
  77. return 0;
  78. while (mime->id != AV_CODEC_ID_NONE) {
  79. if (mime->id == st->codecpar->codec_id) {
  80. mimetype = mime->str;
  81. break;
  82. }
  83. mime++;
  84. }
  85. if (!mimetype) {
  86. av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
  87. "write an attached picture.\n", st->index);
  88. return AVERROR(EINVAL);
  89. }
  90. mimelen = strlen(mimetype);
  91. /* get the picture type */
  92. e = av_dict_get(st->metadata, "comment", NULL, 0);
  93. for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
  94. if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
  95. type = i;
  96. break;
  97. }
  98. }
  99. if ((c->attached_types & (1 << type)) & 0x6) {
  100. av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]);
  101. return AVERROR(EINVAL);
  102. }
  103. if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG ||
  104. st->codecpar->width != 32 ||
  105. st->codecpar->height != 32)) {
  106. av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG");
  107. return AVERROR(EINVAL);
  108. }
  109. c->attached_types |= (1 << type);
  110. /* get the description */
  111. if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
  112. desc = e->value;
  113. desclen = strlen(desc);
  114. blocklen = 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size;
  115. if (blocklen >= 1<<24) {
  116. av_log(s, AV_LOG_ERROR, "Picture block too big %d >= %d\n", blocklen, 1<<24);
  117. return AVERROR(EINVAL);
  118. }
  119. avio_w8(pb, 0x06);
  120. avio_wb24(pb, blocklen);
  121. avio_wb32(pb, type);
  122. avio_wb32(pb, mimelen);
  123. avio_write(pb, mimetype, mimelen);
  124. avio_wb32(pb, desclen);
  125. avio_write(pb, desc, desclen);
  126. avio_wb32(pb, st->codecpar->width);
  127. avio_wb32(pb, st->codecpar->height);
  128. if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
  129. avio_wb32(pb, av_get_bits_per_pixel(pixdesc));
  130. else
  131. avio_wb32(pb, 0);
  132. avio_wb32(pb, 0);
  133. avio_wb32(pb, pkt->size);
  134. avio_write(pb, pkt->data, pkt->size);
  135. return 0;
  136. }
  137. static int flac_finish_header(struct AVFormatContext *s)
  138. {
  139. int i, ret, padding = s->metadata_header_padding;
  140. if (padding < 0)
  141. padding = 8192;
  142. /* The FLAC specification states that 24 bits are used to represent the
  143. * size of a metadata block so we must clip this value to 2^24-1. */
  144. padding = av_clip_uintp2(padding, 24);
  145. for (i = 0; i < s->nb_streams; i++) {
  146. AVStream *st = s->streams[i];
  147. AVPacket *pkt = st->priv_data;
  148. if (!pkt)
  149. continue;
  150. ret = flac_write_picture(s, pkt);
  151. av_packet_unref(pkt);
  152. if (ret < 0 && (s->error_recognition & AV_EF_EXPLODE))
  153. return ret;
  154. }
  155. ret = flac_write_block_comment(s->pb, &s->metadata, !padding,
  156. s->flags & AVFMT_FLAG_BITEXACT);
  157. if (ret)
  158. return ret;
  159. /* The command line flac encoder defaults to placing a seekpoint
  160. * every 10s. So one might add padding to allow that later
  161. * but there seems to be no simple way to get the duration here.
  162. * So just add the amount requested by the user. */
  163. if (padding)
  164. flac_write_block_padding(s->pb, padding, 1);
  165. return 0;
  166. }
  167. static int flac_init(struct AVFormatContext *s)
  168. {
  169. AVCodecParameters *par;
  170. FlacMuxerContext *c = s->priv_data;
  171. int i;
  172. c->audio_stream_idx = -1;
  173. for (i = 0; i < s->nb_streams; i++) {
  174. AVStream *st = s->streams[i];
  175. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  176. if (c->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_FLAC) {
  177. av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one FLAC "
  178. "audio stream is required.\n");
  179. return AVERROR(EINVAL);
  180. }
  181. par = s->streams[i]->codecpar;
  182. c->audio_stream_idx = i;
  183. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  184. if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
  185. av_log(s, AV_LOG_WARNING, "Video stream #%d is not an attached picture. Ignoring\n", i);
  186. continue;
  187. } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
  188. av_log(s, AV_LOG_ERROR, "GIF image support is not implemented.\n");
  189. return AVERROR_PATCHWELCOME;
  190. } else if (!c->write_header) {
  191. av_log(s, AV_LOG_ERROR, "Can't write attached pictures without a header.\n");
  192. return AVERROR(EINVAL);
  193. }
  194. c->waiting_pics++;
  195. } else {
  196. av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in FLAC.\n");
  197. return AVERROR(EINVAL);
  198. }
  199. }
  200. if (c->audio_stream_idx < 0) {
  201. av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
  202. return AVERROR(EINVAL);
  203. }
  204. /* add the channel layout tag */
  205. if (par->channel_layout &&
  206. !(par->channel_layout & ~0x3ffffULL) &&
  207. !ff_flac_is_native_layout(par->channel_layout)) {
  208. AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
  209. NULL, 0);
  210. if (chmask) {
  211. av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
  212. "already present, this muxer will not overwrite it.\n");
  213. } else {
  214. uint8_t buf[32];
  215. snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
  216. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
  217. }
  218. }
  219. return 0;
  220. }
  221. static int flac_write_header(struct AVFormatContext *s)
  222. {
  223. FlacMuxerContext *c = s->priv_data;
  224. AVCodecParameters *par = s->streams[c->audio_stream_idx]->codecpar;
  225. int ret;
  226. if (!c->write_header)
  227. return 0;
  228. ret = ff_flac_write_header(s->pb, par->extradata,
  229. par->extradata_size, 0);
  230. if (ret < 0)
  231. return ret;
  232. if (!c->waiting_pics)
  233. ret = flac_finish_header(s);
  234. return ret;
  235. }
  236. static int flac_write_audio_packet(struct AVFormatContext *s, AVPacket *pkt)
  237. {
  238. FlacMuxerContext *c = s->priv_data;
  239. uint8_t *streaminfo;
  240. int streaminfo_size;
  241. /* check for updated streaminfo */
  242. streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  243. &streaminfo_size);
  244. if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
  245. memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
  246. c->updated_streaminfo = 1;
  247. }
  248. if (pkt->size)
  249. avio_write(s->pb, pkt->data, pkt->size);
  250. return 0;
  251. }
  252. static int flac_queue_flush(AVFormatContext *s)
  253. {
  254. FlacMuxerContext *c = s->priv_data;
  255. AVPacket pkt;
  256. int ret, write = 1;
  257. ret = flac_finish_header(s);
  258. if (ret < 0)
  259. write = 0;
  260. while (c->queue) {
  261. avpriv_packet_list_get(&c->queue, &c->queue_end, &pkt);
  262. if (write && (ret = flac_write_audio_packet(s, &pkt)) < 0)
  263. write = 0;
  264. av_packet_unref(&pkt);
  265. }
  266. return ret;
  267. }
  268. static int flac_write_trailer(struct AVFormatContext *s)
  269. {
  270. AVIOContext *pb = s->pb;
  271. int64_t file_size;
  272. FlacMuxerContext *c = s->priv_data;
  273. if (c->waiting_pics) {
  274. av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
  275. "attached pictures.\n");
  276. flac_queue_flush(s);
  277. }
  278. if (!c->write_header || !c->updated_streaminfo)
  279. return 0;
  280. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  281. /* rewrite the STREAMINFO header block data */
  282. file_size = avio_tell(pb);
  283. avio_seek(pb, 8, SEEK_SET);
  284. avio_write(pb, c->streaminfo, FLAC_STREAMINFO_SIZE);
  285. avio_seek(pb, file_size, SEEK_SET);
  286. } else {
  287. av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
  288. }
  289. return 0;
  290. }
  291. static void flac_deinit(struct AVFormatContext *s)
  292. {
  293. FlacMuxerContext *c = s->priv_data;
  294. avpriv_packet_list_free(&c->queue, &c->queue_end);
  295. for (unsigned i = 0; i < s->nb_streams; i++)
  296. av_packet_free((AVPacket **)&s->streams[i]->priv_data);
  297. }
  298. static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  299. {
  300. FlacMuxerContext *c = s->priv_data;
  301. int ret;
  302. if (pkt->stream_index == c->audio_stream_idx) {
  303. if (c->waiting_pics) {
  304. /* buffer audio packets until we get all the pictures */
  305. ret = avpriv_packet_list_put(&c->queue, &c->queue_end, pkt, av_packet_ref, 0);
  306. if (ret < 0) {
  307. av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n");
  308. c->waiting_pics = 0;
  309. ret = flac_queue_flush(s);
  310. if (ret < 0)
  311. return ret;
  312. return flac_write_audio_packet(s, pkt);
  313. }
  314. } else
  315. return flac_write_audio_packet(s, pkt);
  316. } else {
  317. AVStream *st = s->streams[pkt->stream_index];
  318. if (!c->waiting_pics ||
  319. !(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
  320. return 0;
  321. /* warn only once for each stream */
  322. if (st->nb_frames == 1) {
  323. av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
  324. " ignoring.\n", pkt->stream_index);
  325. }
  326. if (st->nb_frames >= 1)
  327. return 0;
  328. st->priv_data = av_packet_clone(pkt);
  329. if (!st->priv_data)
  330. av_log(s, AV_LOG_ERROR, "Out of memory queueing an attached picture; skipping\n");
  331. c->waiting_pics--;
  332. /* flush the buffered audio packets */
  333. if (!c->waiting_pics &&
  334. (ret = flac_queue_flush(s)) < 0)
  335. return ret;
  336. }
  337. return 0;
  338. }
  339. static const AVOption flacenc_options[] = {
  340. { "write_header", "Write the file header", offsetof(FlacMuxerContext, write_header), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  341. { NULL },
  342. };
  343. static const AVClass flac_muxer_class = {
  344. .class_name = "flac muxer",
  345. .item_name = av_default_item_name,
  346. .option = flacenc_options,
  347. .version = LIBAVUTIL_VERSION_INT,
  348. };
  349. AVOutputFormat ff_flac_muxer = {
  350. .name = "flac",
  351. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  352. .priv_data_size = sizeof(FlacMuxerContext),
  353. .mime_type = "audio/x-flac",
  354. .extensions = "flac",
  355. .audio_codec = AV_CODEC_ID_FLAC,
  356. .video_codec = AV_CODEC_ID_PNG,
  357. .init = flac_init,
  358. .write_header = flac_write_header,
  359. .write_packet = flac_write_packet,
  360. .write_trailer = flac_write_trailer,
  361. .deinit = flac_deinit,
  362. .flags = AVFMT_NOTIMESTAMPS,
  363. .priv_class = &flac_muxer_class,
  364. };