]> git.sesse.net Git - ffmpeg/blob - libavformat/flvenc.c
Remove return statements following infinite loops without break
[ffmpeg] / libavformat / flvenc.c
1 /*
2  * FLV muxer
3  * Copyright (c) 2003 The Libav Project
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 "avformat.h"
22 #include "flv.h"
23 #include "internal.h"
24 #include "avc.h"
25 #include "metadata.h"
26 #include "libavutil/dict.h"
27
28 #undef NDEBUG
29 #include <assert.h>
30
31 static const AVCodecTag flv_video_codec_ids[] = {
32     {CODEC_ID_FLV1,    FLV_CODECID_H263  },
33     {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
34     {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
35     {CODEC_ID_VP6F,    FLV_CODECID_VP6   },
36     {CODEC_ID_VP6,     FLV_CODECID_VP6   },
37     {CODEC_ID_H264,    FLV_CODECID_H264  },
38     {CODEC_ID_NONE,    0}
39 };
40
41 static const AVCodecTag flv_audio_codec_ids[] = {
42     {CODEC_ID_MP3,       FLV_CODECID_MP3    >> FLV_AUDIO_CODECID_OFFSET},
43     {CODEC_ID_PCM_U8,    FLV_CODECID_PCM    >> FLV_AUDIO_CODECID_OFFSET},
44     {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM    >> FLV_AUDIO_CODECID_OFFSET},
45     {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
46     {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM  >> FLV_AUDIO_CODECID_OFFSET},
47     {CODEC_ID_AAC,       FLV_CODECID_AAC    >> FLV_AUDIO_CODECID_OFFSET},
48     {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
49     {CODEC_ID_SPEEX,     FLV_CODECID_SPEEX  >> FLV_AUDIO_CODECID_OFFSET},
50     {CODEC_ID_NONE,      0}
51 };
52
53 typedef struct FLVContext {
54     int reserved;
55     int64_t duration_offset;
56     int64_t filesize_offset;
57     int64_t duration;
58     int delay; ///< first dts delay for AVC
59     int64_t last_video_ts;
60 } FLVContext;
61
62 static int get_audio_flags(AVCodecContext *enc){
63     int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
64
65     if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
66         return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
67     else if (enc->codec_id == CODEC_ID_SPEEX) {
68         if (enc->sample_rate != 16000) {
69             av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
70             return -1;
71         }
72         if (enc->channels != 1) {
73             av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
74             return -1;
75         }
76         if (enc->frame_size / 320 > 8) {
77             av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than "
78                                         "8 frames per packet. Adobe Flash "
79                                         "Player cannot handle this!\n");
80         }
81         return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
82     } else {
83     switch (enc->sample_rate) {
84         case    44100:
85             flags |= FLV_SAMPLERATE_44100HZ;
86             break;
87         case    22050:
88             flags |= FLV_SAMPLERATE_22050HZ;
89             break;
90         case    11025:
91             flags |= FLV_SAMPLERATE_11025HZ;
92             break;
93         case     8000: //nellymoser only
94         case     5512: //not mp3
95             if(enc->codec_id != CODEC_ID_MP3){
96                 flags |= FLV_SAMPLERATE_SPECIAL;
97                 break;
98             }
99         default:
100             av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
101             return -1;
102     }
103     }
104
105     if (enc->channels > 1) {
106         flags |= FLV_STEREO;
107     }
108
109     switch(enc->codec_id){
110     case CODEC_ID_MP3:
111         flags |= FLV_CODECID_MP3    | FLV_SAMPLESSIZE_16BIT;
112         break;
113     case CODEC_ID_PCM_U8:
114         flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_8BIT;
115         break;
116     case CODEC_ID_PCM_S16BE:
117         flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_16BIT;
118         break;
119     case CODEC_ID_PCM_S16LE:
120         flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
121         break;
122     case CODEC_ID_ADPCM_SWF:
123         flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
124         break;
125     case CODEC_ID_NELLYMOSER:
126         if (enc->sample_rate == 8000) {
127             flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
128         } else {
129             flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
130         }
131         break;
132     case 0:
133         flags |= enc->codec_tag<<4;
134         break;
135     default:
136         av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
137         return -1;
138     }
139
140     return flags;
141 }
142
143 static void put_amf_string(AVIOContext *pb, const char *str)
144 {
145     size_t len = strlen(str);
146     avio_wb16(pb, len);
147     avio_write(pb, str, len);
148 }
149
150 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
151     avio_w8(pb, FLV_TAG_TYPE_VIDEO);
152     avio_wb24(pb, 5);  /* Tag Data Size */
153     avio_wb24(pb, ts);  /* lower 24 bits of timestamp in ms*/
154     avio_w8(pb, (ts >> 24) & 0x7F);  /* MSB of ts in ms*/
155     avio_wb24(pb, 0);  /* StreamId = 0 */
156     avio_w8(pb, 23);  /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
157     avio_w8(pb, 2);  /* AVC end of sequence */
158     avio_wb24(pb, 0);  /* Always 0 for AVC EOS. */
159     avio_wb32(pb, 16);  /* Size of FLV tag */
160 }
161
162 static void put_amf_double(AVIOContext *pb, double d)
163 {
164     avio_w8(pb, AMF_DATA_TYPE_NUMBER);
165     avio_wb64(pb, av_dbl2int(d));
166 }
167
168 static void put_amf_bool(AVIOContext *pb, int b) {
169     avio_w8(pb, AMF_DATA_TYPE_BOOL);
170     avio_w8(pb, !!b);
171 }
172
173 static int flv_write_header(AVFormatContext *s)
174 {
175     AVIOContext *pb = s->pb;
176     FLVContext *flv = s->priv_data;
177     AVCodecContext *audio_enc = NULL, *video_enc = NULL;
178     int i;
179     double framerate = 0.0;
180     int metadata_size_pos, data_size;
181     AVDictionaryEntry *tag = NULL;
182
183     for(i=0; i<s->nb_streams; i++){
184         AVCodecContext *enc = s->streams[i]->codec;
185         if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
186             if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
187                 framerate = av_q2d(s->streams[i]->r_frame_rate);
188             } else {
189                 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
190             }
191             video_enc = enc;
192             if(enc->codec_tag == 0) {
193                 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
194                 return -1;
195             }
196         } else {
197             audio_enc = enc;
198             if(get_audio_flags(enc)<0)
199                 return -1;
200         }
201         av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
202     }
203     avio_write(pb, "FLV", 3);
204     avio_w8(pb,1);
205     avio_w8(pb,   FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
206                  + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
207     avio_wb32(pb,9);
208     avio_wb32(pb,0);
209
210     for(i=0; i<s->nb_streams; i++){
211         if(s->streams[i]->codec->codec_tag == 5){
212             avio_w8(pb,8); // message type
213             avio_wb24(pb,0); // include flags
214             avio_wb24(pb,0); // time stamp
215             avio_wb32(pb,0); // reserved
216             avio_wb32(pb,11); // size
217             flv->reserved=5;
218         }
219     }
220
221     flv->last_video_ts = -1;
222
223     /* write meta_tag */
224     avio_w8(pb, 18);         // tag type META
225     metadata_size_pos= avio_tell(pb);
226     avio_wb24(pb, 0);          // size of data part (sum of all parts below)
227     avio_wb24(pb, 0);          // time stamp
228     avio_wb32(pb, 0);          // reserved
229
230     /* now data of data_size size */
231
232     /* first event name as a string */
233     avio_w8(pb, AMF_DATA_TYPE_STRING);
234     put_amf_string(pb, "onMetaData"); // 12 bytes
235
236     /* mixed array (hash) with size and string/type/data tuples */
237     avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
238     avio_wb32(pb, 5*!!video_enc + 5*!!audio_enc + 2); // +2 for duration and file size
239
240     put_amf_string(pb, "duration");
241     flv->duration_offset= avio_tell(pb);
242     put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
243
244     if(video_enc){
245         put_amf_string(pb, "width");
246         put_amf_double(pb, video_enc->width);
247
248         put_amf_string(pb, "height");
249         put_amf_double(pb, video_enc->height);
250
251         put_amf_string(pb, "videodatarate");
252         put_amf_double(pb, video_enc->bit_rate / 1024.0);
253
254         put_amf_string(pb, "framerate");
255         put_amf_double(pb, framerate);
256
257         put_amf_string(pb, "videocodecid");
258         put_amf_double(pb, video_enc->codec_tag);
259     }
260
261     if(audio_enc){
262         put_amf_string(pb, "audiodatarate");
263         put_amf_double(pb, audio_enc->bit_rate / 1024.0);
264
265         put_amf_string(pb, "audiosamplerate");
266         put_amf_double(pb, audio_enc->sample_rate);
267
268         put_amf_string(pb, "audiosamplesize");
269         put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
270
271         put_amf_string(pb, "stereo");
272         put_amf_bool(pb, audio_enc->channels == 2);
273
274         put_amf_string(pb, "audiocodecid");
275         put_amf_double(pb, audio_enc->codec_tag);
276     }
277
278     while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
279         put_amf_string(pb, tag->key);
280         avio_w8(pb, AMF_DATA_TYPE_STRING);
281         put_amf_string(pb, tag->value);
282     }
283
284     put_amf_string(pb, "filesize");
285     flv->filesize_offset= avio_tell(pb);
286     put_amf_double(pb, 0); // delayed write
287
288     put_amf_string(pb, "");
289     avio_w8(pb, AMF_END_OF_OBJECT);
290
291     /* write total size of tag */
292     data_size= avio_tell(pb) - metadata_size_pos - 10;
293     avio_seek(pb, metadata_size_pos, SEEK_SET);
294     avio_wb24(pb, data_size);
295     avio_skip(pb, data_size + 10 - 3);
296     avio_wb32(pb, data_size + 11);
297
298     for (i = 0; i < s->nb_streams; i++) {
299         AVCodecContext *enc = s->streams[i]->codec;
300         if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
301             int64_t pos;
302             avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
303                      FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
304             avio_wb24(pb, 0); // size patched later
305             avio_wb24(pb, 0); // ts
306             avio_w8(pb, 0); // ts ext
307             avio_wb24(pb, 0); // streamid
308             pos = avio_tell(pb);
309             if (enc->codec_id == CODEC_ID_AAC) {
310                 avio_w8(pb, get_audio_flags(enc));
311                 avio_w8(pb, 0); // AAC sequence header
312                 avio_write(pb, enc->extradata, enc->extradata_size);
313             } else {
314                 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
315                 avio_w8(pb, 0); // AVC sequence header
316                 avio_wb24(pb, 0); // composition time
317                 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
318             }
319             data_size = avio_tell(pb) - pos;
320             avio_seek(pb, -data_size - 10, SEEK_CUR);
321             avio_wb24(pb, data_size);
322             avio_skip(pb, data_size + 10 - 3);
323             avio_wb32(pb, data_size + 11); // previous tag size
324         }
325     }
326
327     return 0;
328 }
329
330 static int flv_write_trailer(AVFormatContext *s)
331 {
332     int64_t file_size;
333
334     AVIOContext *pb = s->pb;
335     FLVContext *flv = s->priv_data;
336     int i;
337
338     /* Add EOS tag */
339     for (i = 0; i < s->nb_streams; i++) {
340         AVCodecContext *enc = s->streams[i]->codec;
341         if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
342                 enc->codec_id == CODEC_ID_H264) {
343             put_avc_eos_tag(pb, flv->last_video_ts);
344         }
345     }
346
347     file_size = avio_tell(pb);
348
349     /* update informations */
350     avio_seek(pb, flv->duration_offset, SEEK_SET);
351     put_amf_double(pb, flv->duration / (double)1000);
352     avio_seek(pb, flv->filesize_offset, SEEK_SET);
353     put_amf_double(pb, file_size);
354
355     avio_seek(pb, file_size, SEEK_SET);
356     return 0;
357 }
358
359 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
360 {
361     AVIOContext *pb = s->pb;
362     AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
363     FLVContext *flv = s->priv_data;
364     unsigned ts;
365     int size= pkt->size;
366     uint8_t *data= NULL;
367     int flags, flags_size;
368
369 //    av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
370
371     if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
372        enc->codec_id == CODEC_ID_AAC)
373         flags_size= 2;
374     else if(enc->codec_id == CODEC_ID_H264)
375         flags_size= 5;
376     else
377         flags_size= 1;
378
379     if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
380         avio_w8(pb, FLV_TAG_TYPE_VIDEO);
381
382         flags = enc->codec_tag;
383         if(flags == 0) {
384             av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
385             return -1;
386         }
387
388         flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
389     } else {
390         assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
391         flags = get_audio_flags(enc);
392
393         assert(size);
394
395         avio_w8(pb, FLV_TAG_TYPE_AUDIO);
396     }
397
398     if (enc->codec_id == CODEC_ID_H264) {
399         /* check if extradata looks like mp4 formated */
400         if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
401             if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
402                 return -1;
403         }
404         if (!flv->delay && pkt->dts < 0)
405             flv->delay = -pkt->dts;
406     }
407
408     ts = pkt->dts + flv->delay; // add delay to force positive dts
409     if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
410         if (flv->last_video_ts < ts)
411             flv->last_video_ts = ts;
412     }
413     avio_wb24(pb,size + flags_size);
414     avio_wb24(pb,ts);
415     avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
416     avio_wb24(pb,flv->reserved);
417     avio_w8(pb,flags);
418     if (enc->codec_id == CODEC_ID_VP6)
419         avio_w8(pb,0);
420     if (enc->codec_id == CODEC_ID_VP6F)
421         avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
422     else if (enc->codec_id == CODEC_ID_AAC)
423         avio_w8(pb,1); // AAC raw
424     else if (enc->codec_id == CODEC_ID_H264) {
425         avio_w8(pb,1); // AVC NALU
426         avio_wb24(pb,pkt->pts - pkt->dts);
427     }
428
429     avio_write(pb, data ? data : pkt->data, size);
430
431     avio_wb32(pb,size+flags_size+11); // previous tag size
432     flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
433
434     avio_flush(pb);
435
436     av_free(data);
437
438     return pb->error;
439 }
440
441 AVOutputFormat ff_flv_muxer = {
442     "flv",
443     NULL_IF_CONFIG_SMALL("FLV format"),
444     "video/x-flv",
445     "flv",
446     sizeof(FLVContext),
447 #if CONFIG_LIBMP3LAME
448     CODEC_ID_MP3,
449 #else // CONFIG_LIBMP3LAME
450     CODEC_ID_ADPCM_SWF,
451 #endif // CONFIG_LIBMP3LAME
452     CODEC_ID_FLV1,
453     flv_write_header,
454     flv_write_packet,
455     flv_write_trailer,
456     .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
457     .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
458 };