LCOV - code coverage report
Current view: top level - src/libavformat - ffmenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 140 199 70.4 %
Date: 2017-01-23 11:54:22 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /*
       2             :  * FFM (ffserver live feed) muxer
       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             : 
      22             : #include "libavutil/intreadwrite.h"
      23             : #include "libavutil/intfloat.h"
      24             : #include "libavutil/avassert.h"
      25             : #include "libavutil/parseutils.h"
      26             : #include "libavutil/opt.h"
      27             : #include "avformat.h"
      28             : #include "avio_internal.h"
      29             : #include "internal.h"
      30             : #include "ffm.h"
      31             : 
      32          91 : static void flush_packet(AVFormatContext *s)
      33             : {
      34          91 :     FFMContext *ffm = s->priv_data;
      35             :     int fill_size, h;
      36          91 :     AVIOContext *pb = s->pb;
      37             : 
      38          91 :     fill_size = ffm->packet_end - ffm->packet_ptr;
      39          91 :     memset(ffm->packet_ptr, 0, fill_size);
      40             : 
      41             :     av_assert1(avio_tell(pb) % ffm->packet_size == 0);
      42             : 
      43             :     /* put header */
      44          91 :     avio_wb16(pb, PACKET_ID);
      45          91 :     avio_wb16(pb, fill_size);
      46          91 :     avio_wb64(pb, ffm->dts);
      47          91 :     h = ffm->frame_offset;
      48          91 :     if (ffm->first_packet)
      49           1 :         h |= 0x8000;
      50          91 :     avio_wb16(pb, h);
      51          91 :     avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
      52          91 :     avio_flush(pb);
      53             : 
      54             :     /* prepare next packet */
      55          91 :     ffm->frame_offset = 0; /* no key frame */
      56          91 :     ffm->packet_ptr = ffm->packet;
      57          91 :     ffm->first_packet = 0;
      58          91 : }
      59             : 
      60             : /* 'first' is true if first data of a frame */
      61         128 : static void ffm_write_data(AVFormatContext *s,
      62             :                            const uint8_t *buf, int size,
      63             :                            int64_t dts, int header)
      64             : {
      65         128 :     FFMContext *ffm = s->priv_data;
      66             :     int len;
      67             : 
      68         128 :     if (header && ffm->frame_offset == 0) {
      69          29 :         ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
      70          29 :         ffm->dts = dts;
      71             :     }
      72             : 
      73             :     /* write as many packets as needed */
      74         474 :     while (size > 0) {
      75         218 :         len = ffm->packet_end - ffm->packet_ptr;
      76         218 :         if (len > size)
      77         128 :             len = size;
      78         218 :         memcpy(ffm->packet_ptr, buf, len);
      79             : 
      80         218 :         ffm->packet_ptr += len;
      81         218 :         buf += len;
      82         218 :         size -= len;
      83         218 :         if (ffm->packet_ptr >= ffm->packet_end)
      84          90 :             flush_packet(s);
      85             :     }
      86         128 : }
      87             : 
      88           5 : static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
      89             : {
      90             :     uint8_t *dyn_buf;
      91           5 :     int dyn_size= avio_close_dyn_buf(dpb, &dyn_buf);
      92           5 :     avio_wb32(pb, id);
      93           5 :     avio_wb32(pb, dyn_size);
      94           5 :     avio_write(pb, dyn_buf, dyn_size);
      95           5 :     av_free(dyn_buf);
      96           5 : }
      97             : 
      98           2 : static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecParameters *ctxpar, unsigned tag, int type)
      99             : {
     100             :     AVIOContext *tmp;
     101           2 :     char *buf = NULL;
     102           2 :     int ret, need_coma = 0;
     103           2 :     AVCodecContext *ctx = NULL;
     104             : 
     105             : #define SKIP_DEFAULTS   AV_OPT_SERIALIZE_SKIP_DEFAULTS
     106             : #define OPT_FLAGS_EXACT AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
     107             : #define ENC             AV_OPT_FLAG_ENCODING_PARAM
     108             : 
     109           2 :     if (avio_open_dyn_buf(&tmp) < 0)
     110           0 :         return AVERROR(ENOMEM);
     111             : 
     112             :     // AVCodecParameters does not suport AVOptions, we thus must copy it over to a context that does
     113             :     // otherwise it could be used directly and this would be much simpler
     114           2 :     ctx = avcodec_alloc_context3(NULL);
     115           2 :     if (!ctx) {
     116           0 :         ret = AVERROR(ENOMEM);
     117           0 :         goto fail;
     118             :     }
     119           2 :     avcodec_parameters_to_context(ctx, ctxpar);
     120             : 
     121           2 :     if ((ret = av_opt_serialize(ctx, ENC | type, SKIP_DEFAULTS, &buf, '=', ',')) < 0)
     122           0 :         goto fail;
     123           2 :     if (buf && strlen(buf)) {
     124           2 :         avio_write(tmp, buf, strlen(buf));
     125           2 :         av_freep(&buf);
     126           2 :         need_coma = 1;
     127             :     }
     128           2 :     if ((ret = av_opt_serialize(ctx, 0, SKIP_DEFAULTS | OPT_FLAGS_EXACT, &buf, '=', ',')) < 0)
     129           0 :         goto fail;
     130           2 :     if (buf && strlen(buf)) {
     131           2 :         if (need_coma)
     132           2 :             avio_w8(tmp, ',');
     133           2 :         avio_write(tmp, buf, strlen(buf));
     134             :     }
     135           2 :     av_freep(&buf);
     136           2 :     avio_w8(tmp, 0);
     137           2 :     write_header_chunk(pb, tmp, tag);
     138           2 :     avcodec_free_context(&ctx);
     139           2 :     return 0;
     140             :   fail:
     141           0 :     av_free(buf);
     142           0 :     ffio_free_dyn_buf(&tmp);
     143           0 :     avcodec_free_context(&ctx);
     144           0 :     return ret;
     145             : 
     146             : #undef SKIP_DEFAULTS
     147             : #undef OPT_FLAGS_EXACT
     148             : #undef ENC
     149             : }
     150             : 
     151           0 : static int ffm_write_recommended_config(AVIOContext *pb, AVCodecParameters *codecpar, unsigned tag,
     152             :                                         const char *configuration)
     153             : {
     154             :     int ret;
     155           0 :     const AVCodec *enc = avcodec_find_encoder(codecpar->codec_id);
     156             :     AVIOContext *tmp;
     157           0 :     AVDictionaryEntry *t = NULL;
     158           0 :     AVDictionary *all = NULL, *comm = NULL, *prv = NULL;
     159           0 :     char *buf = NULL;
     160             : 
     161           0 :     if (!enc || !enc->priv_class || !enc->priv_data_size) {
     162             :         /* codec is not known/has no private options, so save everything as common options */
     163           0 :         if (avio_open_dyn_buf(&tmp) < 0)
     164           0 :             return AVERROR(ENOMEM);
     165           0 :         avio_put_str(tmp, configuration);
     166           0 :         write_header_chunk(pb, tmp, tag);
     167           0 :         return 0;
     168             :     }
     169             : 
     170           0 :     if ((ret = av_dict_parse_string(&all, configuration, "=", ",", 0)) < 0)
     171           0 :         return ret;
     172             : 
     173           0 :     while ((t = av_dict_get(all, "", t, AV_DICT_IGNORE_SUFFIX))) {
     174           0 :         if (av_opt_find((void *)&enc->priv_class, t->key, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)) {
     175           0 :             if ((ret = av_dict_set(&prv, t->key, t->value, 0)) < 0)
     176           0 :                 goto fail;
     177           0 :         } else if ((ret = av_dict_set(&comm, t->key, t->value, 0)) < 0)
     178           0 :             goto fail;
     179             :     }
     180             : 
     181           0 :     if (comm) {
     182           0 :         if ((ret = av_dict_get_string(comm, &buf, '=', ',')) < 0 ||
     183             :             (ret = avio_open_dyn_buf(&tmp)) < 0)
     184             :             goto fail;
     185           0 :         avio_put_str(tmp, buf);
     186           0 :         av_freep(&buf);
     187           0 :         write_header_chunk(pb, tmp, tag);
     188             :     }
     189           0 :     if (prv) {
     190           0 :         if ((ret = av_dict_get_string(prv, &buf, '=', ',')) < 0 ||
     191             :             (ret = avio_open_dyn_buf(&tmp)) < 0)
     192             :             goto fail;
     193           0 :         avio_put_str(tmp, buf);
     194           0 :         write_header_chunk(pb, tmp, MKBETAG('C', 'P', 'R', 'V'));
     195             :     }
     196             : 
     197             :   fail:
     198           0 :     av_free(buf);
     199           0 :     av_dict_free(&all);
     200           0 :     av_dict_free(&comm);
     201           0 :     av_dict_free(&prv);
     202           0 :     return ret;
     203             : }
     204             : 
     205           1 : static int ffm_write_header(AVFormatContext *s)
     206             : {
     207           1 :     FFMContext *ffm = s->priv_data;
     208             :     AVStream *st;
     209           1 :     AVIOContext *pb = s->pb;
     210             :     AVCodecParameters *codecpar;
     211             :     int bit_rate, i, ret;
     212             : 
     213           1 :     if ((ret = ff_parse_creation_time_metadata(s, &ffm->start_time, 0)) < 0)
     214           0 :         return ret;
     215             : 
     216           1 :     ffm->packet_size = FFM_PACKET_SIZE;
     217             : 
     218             :     /* header */
     219           1 :     avio_wl32(pb, MKTAG('F', 'F', 'M', '2'));
     220           1 :     avio_wb32(pb, ffm->packet_size);
     221           1 :     avio_wb64(pb, 0); /* current write position */
     222             : 
     223           1 :     if(avio_open_dyn_buf(&pb) < 0)
     224           0 :         return AVERROR(ENOMEM);
     225             : 
     226           1 :     avio_wb32(pb, s->nb_streams);
     227           1 :     bit_rate = 0;
     228           3 :     for(i=0;i<s->nb_streams;i++) {
     229           2 :         st = s->streams[i];
     230           2 :         bit_rate += st->codecpar->bit_rate;
     231             :     }
     232           1 :     avio_wb32(pb, bit_rate);
     233             : 
     234           1 :     write_header_chunk(s->pb, pb, MKBETAG('M', 'A', 'I', 'N'));
     235             : 
     236             :     /* list of streams */
     237           3 :     for(i=0;i<s->nb_streams;i++) {
     238           2 :         int flags = 0;
     239           2 :         st = s->streams[i];
     240           2 :         avpriv_set_pts_info(st, 64, 1, 1000000);
     241           2 :         if(avio_open_dyn_buf(&pb) < 0)
     242           0 :             return AVERROR(ENOMEM);
     243             : 
     244           2 :         codecpar = st->codecpar;
     245             :         /* generic info */
     246           2 :         avio_wb32(pb, codecpar->codec_id);
     247           2 :         avio_w8(pb, codecpar->codec_type);
     248           2 :         avio_wb32(pb, codecpar->bit_rate);
     249           2 :         if (codecpar->extradata_size)
     250           0 :             flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
     251             : 
     252             :         // If the user is not providing us with a configuration we have to fill it in as we cannot access the encoder
     253           2 :         if (!st->recommended_encoder_configuration) {
     254           2 :             if (s->flags & AVFMT_FLAG_BITEXACT)
     255           2 :                 flags |= AV_CODEC_FLAG_BITEXACT;
     256             :         }
     257             : 
     258           2 :         avio_wb32(pb, flags);
     259           2 :         avio_wb32(pb, 0); // flags2
     260           2 :         avio_wb32(pb, 0); // debug
     261           2 :         if (codecpar->extradata_size) {
     262           0 :             avio_wb32(pb, codecpar->extradata_size);
     263           0 :             avio_write(pb, codecpar->extradata, codecpar->extradata_size);
     264             :         }
     265           2 :         write_header_chunk(s->pb, pb, MKBETAG('C', 'O', 'M', 'M'));
     266             :         /* specific info */
     267           2 :         switch(codecpar->codec_type) {
     268             :         case AVMEDIA_TYPE_VIDEO:
     269           1 :             if (st->recommended_encoder_configuration) {
     270           0 :                 av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
     271             :                        st->recommended_encoder_configuration);
     272           0 :                 if ((ret = ffm_write_recommended_config(s->pb, codecpar, MKBETAG('S', '2', 'V', 'I'),
     273           0 :                                                         st->recommended_encoder_configuration)) < 0)
     274           0 :                 return ret;
     275           1 :             } else if ((ret = ffm_write_header_codec_ctx(s->pb, codecpar, MKBETAG('S', '2', 'V', 'I'), AV_OPT_FLAG_VIDEO_PARAM)) < 0)
     276           0 :                 return ret;
     277           1 :             break;
     278             :         case AVMEDIA_TYPE_AUDIO:
     279           1 :             if (st->recommended_encoder_configuration) {
     280           0 :                 av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
     281             :                        st->recommended_encoder_configuration);
     282           0 :                 if ((ret = ffm_write_recommended_config(s->pb, codecpar, MKBETAG('S', '2', 'A', 'U'),
     283           0 :                                                         st->recommended_encoder_configuration)) < 0)
     284           0 :                 return ret;
     285           1 :             } else if ((ret = ffm_write_header_codec_ctx(s->pb, codecpar, MKBETAG('S', '2', 'A', 'U'), AV_OPT_FLAG_AUDIO_PARAM)) < 0)
     286           0 :                 return ret;
     287           1 :             break;
     288             :         default:
     289           0 :             return -1;
     290             :         }
     291             :     }
     292           1 :     pb = s->pb;
     293             : 
     294           1 :     avio_wb64(pb, 0); // end of header
     295             : 
     296             :     /* flush until end of block reached */
     297        3815 :     while ((avio_tell(pb) % ffm->packet_size) != 0)
     298        3813 :         avio_w8(pb, 0);
     299             : 
     300           1 :     avio_flush(pb);
     301             : 
     302             :     /* init packet mux */
     303           1 :     ffm->packet_ptr = ffm->packet;
     304           1 :     ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
     305           1 :     av_assert0(ffm->packet_end >= ffm->packet);
     306           1 :     ffm->frame_offset = 0;
     307           1 :     ffm->dts = 0;
     308           1 :     ffm->first_packet = 1;
     309             : 
     310           1 :     return 0;
     311             : }
     312             : 
     313          64 : static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
     314             : {
     315          64 :     FFMContext *ffm = s->priv_data;
     316             :     int64_t dts;
     317             :     uint8_t header[FRAME_HEADER_SIZE+4];
     318          64 :     int header_size = FRAME_HEADER_SIZE;
     319             : 
     320          64 :     dts = ffm->start_time + pkt->dts;
     321             :     /* packet size & key_frame */
     322          64 :     header[0] = pkt->stream_index;
     323          64 :     header[1] = 0;
     324          64 :     if (pkt->flags & AV_PKT_FLAG_KEY)
     325          42 :         header[1] |= FLAG_KEY_FRAME;
     326          64 :     AV_WB24(header+2, pkt->size);
     327          64 :     AV_WB24(header+5, pkt->duration);
     328          64 :     AV_WB64(header+8, ffm->start_time + pkt->pts);
     329          64 :     if (pkt->pts != pkt->dts) {
     330          25 :         header[1] |= FLAG_DTS;
     331          25 :         AV_WB32(header+16, pkt->pts - pkt->dts);
     332          25 :         header_size += 4;
     333             :     }
     334          64 :     ffm_write_data(s, header, header_size, dts, 1);
     335          64 :     ffm_write_data(s, pkt->data, pkt->size, dts, 0);
     336             : 
     337          64 :     return 0;
     338             : }
     339             : 
     340           1 : static int ffm_write_trailer(AVFormatContext *s)
     341             : {
     342           1 :     FFMContext *ffm = s->priv_data;
     343             : 
     344             :     /* flush packets */
     345           1 :     if (ffm->packet_ptr > ffm->packet)
     346           1 :         flush_packet(s);
     347             : 
     348           1 :     return 0;
     349             : }
     350             : 
     351             : AVOutputFormat ff_ffm_muxer = {
     352             :     .name              = "ffm",
     353             :     .long_name         = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
     354             :     .extensions        = "ffm",
     355             :     .priv_data_size    = sizeof(FFMContext),
     356             :     .audio_codec       = AV_CODEC_ID_MP2,
     357             :     .video_codec       = AV_CODEC_ID_MPEG1VIDEO,
     358             :     .write_header      = ffm_write_header,
     359             :     .write_packet      = ffm_write_packet,
     360             :     .write_trailer     = ffm_write_trailer,
     361             :     .flags             = AVFMT_TS_NEGATIVE,
     362             : };

Generated by: LCOV version 1.12