• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/flvenc.c

Go to the documentation of this file.
00001 /*
00002  * FLV muxer
00003  * Copyright (c) 2003 The FFmpeg Project
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avformat.h"
00022 #include "flv.h"
00023 #include "riff.h"
00024 #include "avc.h"
00025 
00026 #undef NDEBUG
00027 #include <assert.h>
00028 
00029 static const AVCodecTag flv_video_codec_ids[] = {
00030     {CODEC_ID_FLV1,    FLV_CODECID_H263  },
00031     {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
00032     {CODEC_ID_VP6F,    FLV_CODECID_VP6   },
00033     {CODEC_ID_VP6,     FLV_CODECID_VP6   },
00034     {CODEC_ID_H264,    FLV_CODECID_H264  },
00035     {CODEC_ID_NONE,    0}
00036 };
00037 
00038 static const AVCodecTag flv_audio_codec_ids[] = {
00039     {CODEC_ID_MP3,       FLV_CODECID_MP3    >> FLV_AUDIO_CODECID_OFFSET},
00040     {CODEC_ID_PCM_S8,    FLV_CODECID_PCM    >> FLV_AUDIO_CODECID_OFFSET},
00041     {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM    >> FLV_AUDIO_CODECID_OFFSET},
00042     {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
00043     {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM  >> FLV_AUDIO_CODECID_OFFSET},
00044     {CODEC_ID_AAC,       FLV_CODECID_AAC    >> FLV_AUDIO_CODECID_OFFSET},
00045     {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
00046     {CODEC_ID_NONE,      0}
00047 };
00048 
00049 typedef struct FLVContext {
00050     int reserved;
00051     int64_t duration_offset;
00052     int64_t filesize_offset;
00053     int64_t duration;
00054     int delay; 
00055 } FLVContext;
00056 
00057 static int get_audio_flags(AVCodecContext *enc){
00058     int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
00059 
00060     if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
00061         return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
00062     else {
00063     switch (enc->sample_rate) {
00064         case    44100:
00065             flags |= FLV_SAMPLERATE_44100HZ;
00066             break;
00067         case    22050:
00068             flags |= FLV_SAMPLERATE_22050HZ;
00069             break;
00070         case    11025:
00071             flags |= FLV_SAMPLERATE_11025HZ;
00072             break;
00073         case     8000: //nellymoser only
00074         case     5512: //not mp3
00075             if(enc->codec_id != CODEC_ID_MP3){
00076                 flags |= FLV_SAMPLERATE_SPECIAL;
00077                 break;
00078             }
00079         default:
00080             av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
00081             return -1;
00082     }
00083     }
00084 
00085     if (enc->channels > 1) {
00086         flags |= FLV_STEREO;
00087     }
00088 
00089     switch(enc->codec_id){
00090     case CODEC_ID_MP3:
00091         flags |= FLV_CODECID_MP3    | FLV_SAMPLESSIZE_16BIT;
00092         break;
00093     case CODEC_ID_PCM_S8:
00094         flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_8BIT;
00095         break;
00096     case CODEC_ID_PCM_S16BE:
00097         flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_16BIT;
00098         break;
00099     case CODEC_ID_PCM_S16LE:
00100         flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
00101         break;
00102     case CODEC_ID_ADPCM_SWF:
00103         flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
00104         break;
00105     case CODEC_ID_NELLYMOSER:
00106         if (enc->sample_rate == 8000) {
00107             flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
00108         } else {
00109             flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
00110         }
00111         break;
00112     case 0:
00113         flags |= enc->codec_tag<<4;
00114         break;
00115     default:
00116         av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
00117         return -1;
00118     }
00119 
00120     return flags;
00121 }
00122 
00123 static void put_amf_string(ByteIOContext *pb, const char *str)
00124 {
00125     size_t len = strlen(str);
00126     put_be16(pb, len);
00127     put_buffer(pb, str, len);
00128 }
00129 
00130 static void put_amf_double(ByteIOContext *pb, double d)
00131 {
00132     put_byte(pb, AMF_DATA_TYPE_NUMBER);
00133     put_be64(pb, av_dbl2int(d));
00134 }
00135 
00136 static void put_amf_bool(ByteIOContext *pb, int b) {
00137     put_byte(pb, AMF_DATA_TYPE_BOOL);
00138     put_byte(pb, !!b);
00139 }
00140 
00141 static int flv_write_header(AVFormatContext *s)
00142 {
00143     ByteIOContext *pb = s->pb;
00144     FLVContext *flv = s->priv_data;
00145     AVCodecContext *audio_enc = NULL, *video_enc = NULL;
00146     int i;
00147     double framerate = 0.0;
00148     int metadata_size_pos, data_size;
00149 
00150     for(i=0; i<s->nb_streams; i++){
00151         AVCodecContext *enc = s->streams[i]->codec;
00152         if (enc->codec_type == CODEC_TYPE_VIDEO) {
00153             if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
00154                 framerate = av_q2d(s->streams[i]->r_frame_rate);
00155             } else {
00156                 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
00157             }
00158             video_enc = enc;
00159             if(enc->codec_tag == 0) {
00160                 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
00161                 return -1;
00162             }
00163         } else {
00164             audio_enc = enc;
00165             if(get_audio_flags(enc)<0)
00166                 return -1;
00167         }
00168         av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
00169     }
00170     put_tag(pb,"FLV");
00171     put_byte(pb,1);
00172     put_byte(pb,   FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
00173                  + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
00174     put_be32(pb,9);
00175     put_be32(pb,0);
00176 
00177     for(i=0; i<s->nb_streams; i++){
00178         if(s->streams[i]->codec->codec_tag == 5){
00179             put_byte(pb,8); // message type
00180             put_be24(pb,0); // include flags
00181             put_be24(pb,0); // time stamp
00182             put_be32(pb,0); // reserved
00183             put_be32(pb,11); // size
00184             flv->reserved=5;
00185         }
00186     }
00187 
00188     /* write meta_tag */
00189     put_byte(pb, 18);         // tag type META
00190     metadata_size_pos= url_ftell(pb);
00191     put_be24(pb, 0);          // size of data part (sum of all parts below)
00192     put_be24(pb, 0);          // time stamp
00193     put_be32(pb, 0);          // reserved
00194 
00195     /* now data of data_size size */
00196 
00197     /* first event name as a string */
00198     put_byte(pb, AMF_DATA_TYPE_STRING);
00199     put_amf_string(pb, "onMetaData"); // 12 bytes
00200 
00201     /* mixed array (hash) with size and string/type/data tuples */
00202     put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
00203     put_be32(pb, 5*!!video_enc + 4*!!audio_enc + 2); // +2 for duration and file size
00204 
00205     put_amf_string(pb, "duration");
00206     flv->duration_offset= url_ftell(pb);
00207     put_amf_double(pb, 0); // delayed write
00208 
00209     if(video_enc){
00210         put_amf_string(pb, "width");
00211         put_amf_double(pb, video_enc->width);
00212 
00213         put_amf_string(pb, "height");
00214         put_amf_double(pb, video_enc->height);
00215 
00216         put_amf_string(pb, "videodatarate");
00217         put_amf_double(pb, video_enc->bit_rate / 1024.0);
00218 
00219         put_amf_string(pb, "framerate");
00220         put_amf_double(pb, framerate);
00221 
00222         put_amf_string(pb, "videocodecid");
00223         put_amf_double(pb, video_enc->codec_tag);
00224     }
00225 
00226     if(audio_enc){
00227         put_amf_string(pb, "audiodatarate");
00228         put_amf_double(pb, audio_enc->bit_rate / 1024.0);
00229 
00230         put_amf_string(pb, "audiosamplerate");
00231         put_amf_double(pb, audio_enc->sample_rate);
00232 
00233         put_amf_string(pb, "audiosamplesize");
00234         put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_S8 ? 8 : 16);
00235 
00236         put_amf_string(pb, "stereo");
00237         put_amf_bool(pb, audio_enc->channels == 2);
00238 
00239         put_amf_string(pb, "audiocodecid");
00240         put_amf_double(pb, audio_enc->codec_tag);
00241     }
00242 
00243     put_amf_string(pb, "filesize");
00244     flv->filesize_offset= url_ftell(pb);
00245     put_amf_double(pb, 0); // delayed write
00246 
00247     put_amf_string(pb, "");
00248     put_byte(pb, AMF_END_OF_OBJECT);
00249 
00250     /* write total size of tag */
00251     data_size= url_ftell(pb) - metadata_size_pos - 10;
00252     url_fseek(pb, metadata_size_pos, SEEK_SET);
00253     put_be24(pb, data_size);
00254     url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
00255     put_be32(pb, data_size + 11);
00256 
00257     for (i = 0; i < s->nb_streams; i++) {
00258         AVCodecContext *enc = s->streams[i]->codec;
00259         if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
00260             int64_t pos;
00261             put_byte(pb, enc->codec_type == CODEC_TYPE_VIDEO ?
00262                      FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
00263             put_be24(pb, 0); // size patched later
00264             put_be24(pb, 0); // ts
00265             put_byte(pb, 0); // ts ext
00266             put_be24(pb, 0); // streamid
00267             pos = url_ftell(pb);
00268             if (enc->codec_id == CODEC_ID_AAC) {
00269                 put_byte(pb, get_audio_flags(enc));
00270                 put_byte(pb, 0); // AAC sequence header
00271                 put_buffer(pb, enc->extradata, enc->extradata_size);
00272             } else {
00273                 put_byte(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
00274                 put_byte(pb, 0); // AVC sequence header
00275                 put_be24(pb, 0); // composition time
00276                 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
00277             }
00278             data_size = url_ftell(pb) - pos;
00279             url_fseek(pb, -data_size - 10, SEEK_CUR);
00280             put_be24(pb, data_size);
00281             url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
00282             put_be32(pb, data_size + 11); // previous tag size
00283         }
00284     }
00285 
00286     return 0;
00287 }
00288 
00289 static int flv_write_trailer(AVFormatContext *s)
00290 {
00291     int64_t file_size;
00292 
00293     ByteIOContext *pb = s->pb;
00294     FLVContext *flv = s->priv_data;
00295 
00296     file_size = url_ftell(pb);
00297 
00298     /* update informations */
00299     url_fseek(pb, flv->duration_offset, SEEK_SET);
00300     put_amf_double(pb, flv->duration / (double)1000);
00301     url_fseek(pb, flv->filesize_offset, SEEK_SET);
00302     put_amf_double(pb, file_size);
00303 
00304     url_fseek(pb, file_size, SEEK_SET);
00305     return 0;
00306 }
00307 
00308 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
00309 {
00310     ByteIOContext *pb = s->pb;
00311     AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
00312     FLVContext *flv = s->priv_data;
00313     unsigned ts;
00314     int size= pkt->size;
00315     uint8_t *data= NULL;
00316     int flags, flags_size;
00317 
00318 //    av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
00319 
00320     if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
00321        enc->codec_id == CODEC_ID_AAC)
00322         flags_size= 2;
00323     else if(enc->codec_id == CODEC_ID_H264)
00324         flags_size= 5;
00325     else
00326         flags_size= 1;
00327 
00328     if (enc->codec_type == CODEC_TYPE_VIDEO) {
00329         put_byte(pb, FLV_TAG_TYPE_VIDEO);
00330 
00331         flags = enc->codec_tag;
00332         if(flags == 0) {
00333             av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
00334             return -1;
00335         }
00336 
00337         flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
00338     } else {
00339         assert(enc->codec_type == CODEC_TYPE_AUDIO);
00340         flags = get_audio_flags(enc);
00341 
00342         assert(size);
00343 
00344         put_byte(pb, FLV_TAG_TYPE_AUDIO);
00345     }
00346 
00347     if (enc->codec_id == CODEC_ID_H264) {
00348         /* check if extradata looks like mp4 formated */
00349         if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
00350             if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
00351                 return -1;
00352         }
00353         if (!flv->delay && pkt->dts < 0)
00354             flv->delay = -pkt->dts;
00355     }
00356 
00357     ts = pkt->dts + flv->delay; // add delay to force positive dts
00358     put_be24(pb,size + flags_size);
00359     put_be24(pb,ts);
00360     put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
00361     put_be24(pb,flv->reserved);
00362     put_byte(pb,flags);
00363     if (enc->codec_id == CODEC_ID_VP6)
00364         put_byte(pb,0);
00365     if (enc->codec_id == CODEC_ID_VP6F)
00366         put_byte(pb, enc->extradata_size ? enc->extradata[0] : 0);
00367     else if (enc->codec_id == CODEC_ID_AAC)
00368         put_byte(pb,1); // AAC raw
00369     else if (enc->codec_id == CODEC_ID_H264) {
00370         put_byte(pb,1); // AVC NALU
00371         put_be24(pb,pkt->pts - pkt->dts);
00372     }
00373 
00374     put_buffer(pb, data ? data : pkt->data, size);
00375 
00376     put_be32(pb,size+flags_size+11); // previous tag size
00377     flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
00378 
00379     put_flush_packet(pb);
00380 
00381     av_free(data);
00382 
00383     return 0;
00384 }
00385 
00386 AVOutputFormat flv_muxer = {
00387     "flv",
00388     NULL_IF_CONFIG_SMALL("FLV format"),
00389     "video/x-flv",
00390     "flv",
00391     sizeof(FLVContext),
00392 #if CONFIG_LIBMP3LAME
00393     CODEC_ID_MP3,
00394 #else // CONFIG_LIBMP3LAME
00395     CODEC_ID_ADPCM_SWF,
00396 #endif // CONFIG_LIBMP3LAME
00397     CODEC_ID_FLV1,
00398     flv_write_header,
00399     flv_write_packet,
00400     flv_write_trailer,
00401     .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
00402     .flags= AVFMT_GLOBALHEADER,
00403 };

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1