LCOV - code coverage report
Current view: top level - libavformat - flacenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 89 212 42.0 %
Date: 2018-05-20 11:54:08 Functions: 8 10 80.0 %

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

Generated by: LCOV version 1.13