Newer version, using a vectorized version of the
[libav.git] / libavformat / flvenc.c
CommitLineData
d4f5d74a
GM
1/*
2 * FLV encoder.
3 * Copyright (c) 2003 The FFmpeg Project.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "avformat.h"
20
068f2a22
MN
21#undef NDEBUG
22#include <assert.h>
23
d4f5d74a
GM
24#define VIDEO_FIFO_SIZE 512
25
26typedef struct FLVFrame {
27 int type;
28 int timestamp;
11a8e425 29 int reserved;
d4f5d74a
GM
30 int flags;
31 uint8_t *data;
32 int size;
33 struct FLVFrame *next;
34} FLVFrame;
35
36typedef struct FLVContext {
37 int hasAudio;
38 int hasVideo;
d4f5d74a 39 int initDelay;
d4f5d74a 40 int64_t sampleCount;
d4f5d74a 41 int64_t frameCount;
11a8e425 42 int reserved;
d4f5d74a
GM
43 FLVFrame *frames;
44} FLVContext;
45
d4f5d74a
GM
46#ifdef CONFIG_MP3LAME
47
d4f5d74a
GM
48static const int sSampleRates[3][4] = {
49 {44100, 48000, 32000, 0},
50 {22050, 24000, 16000, 0},
51 {11025, 12000, 8000, 0},
52};
53
54static const int sBitRates[2][3][15] = {
55 { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
56 { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
57 { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
58 },
59 { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
60 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
61 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
62 },
63};
64
65static const int sSamplesPerFrame[3][3] =
66{
67 { 384, 1152, 1152 },
68 { 384, 1152, 576 },
69 { 384, 1152, 576 }
70};
71
72static const int sBitsPerSlot[3] = {
73 32,
74 8,
75 8
76};
77
78static int mp3info(void *data, int *byteSize, int *samplesPerFrame, int *sampleRate, int *isMono )
79{
80 uint8_t *dataTmp = (uint8_t *)data;
81 uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3];
82 int layerID = 3 - ((header >> 17) & 0x03);
83 int bitRateID = ((header >> 12) & 0x0f);
84 int sampleRateID = ((header >> 10) & 0x03);
85 int bitRate = 0;
86 int bitsPerSlot = sBitsPerSlot[layerID];
87 int isPadded = ((header >> 9) & 0x01);
88
89 if ( (( header >> 21 ) & 0x7ff) != 0x7ff ) {
90 return 0;
91 }
92
93 if ( !isPadded ) {
92a26775
MN
94// printf("Fatal error: mp3 data is not padded!\n");
95// exit(0);
d4f5d74a
GM
96 }
97
98 *isMono = ((header >> 6) & 0x03) == 0x03;
99
100 if ( (header >> 19 ) & 0x01 ) {
92a26775 101 //MPEG1
d4f5d74a
GM
102 *sampleRate = sSampleRates[0][sampleRateID];
103 bitRate = sBitRates[0][layerID][bitRateID] * 1000;
104 *samplesPerFrame = sSamplesPerFrame[0][layerID];
d4f5d74a
GM
105 } else {
106 if ( (header >> 20) & 0x01 ) {
92a26775 107 //MPEG2
d4f5d74a
GM
108 *sampleRate = sSampleRates[1][sampleRateID];
109 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
110 *samplesPerFrame = sSamplesPerFrame[1][layerID];
111 } else {
92a26775 112 //MPEG2.5
d4f5d74a
GM
113 *sampleRate = sSampleRates[2][sampleRateID];
114 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
115 *samplesPerFrame = sSamplesPerFrame[2][layerID];
116 }
117 }
92a26775 118
747a0554 119 *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) );
d4f5d74a
GM
120 return 1;
121}
122#endif // CONFIG_MP3LAME
123
11a8e425
MN
124static void put_be24(ByteIOContext *pb, int value)
125{
126 put_byte(pb, (value>>16) & 0xFF );
127 put_byte(pb, (value>> 8) & 0xFF );
128 put_byte(pb, (value>> 0) & 0xFF );
129}
130
d4f5d74a
GM
131static int flv_write_header(AVFormatContext *s)
132{
133 ByteIOContext *pb = &s->pb;
134 FLVContext *flv = s->priv_data;
11a8e425 135 int i;
d4f5d74a 136
d4f5d74a
GM
137 flv->hasAudio = 0;
138 flv->hasVideo = 0;
139
d4f5d74a 140 flv->initDelay = -1;
d4f5d74a
GM
141
142 flv->frames = 0;
143
144 put_tag(pb,"FLV");
145 put_byte(pb,1);
146 put_byte(pb,0); // delayed write
147 put_be32(pb,9);
148 put_be32(pb,0);
11a8e425
MN
149
150 for(i=0; i<s->nb_streams; i++){
151 AVCodecContext *enc = &s->streams[i]->codec;
9ee91c2f 152 av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
11a8e425
MN
153 if(enc->codec_tag == 5){
154 put_byte(pb,8); // message type
155 put_be24(pb,0); // include flags
156 put_be24(pb,0); // time stamp
157 put_be32(pb,0); // reserved
158 put_be32(pb,11); // size
159 flv->reserved=5;
160 }
161 }
d4f5d74a
GM
162
163 return 0;
164}
165
d4f5d74a
GM
166static void InsertSorted(FLVContext *flv, FLVFrame *frame)
167{
168 if ( !flv->frames ) {
169 flv->frames = frame;
170 } else {
171 FLVFrame *trav = flv->frames;
172 FLVFrame *prev = 0;
173 for (;trav;) {
11a8e425 174 if ( trav->timestamp > frame->timestamp) {
d4f5d74a
GM
175 frame->next = trav;
176 if ( prev ) {
177 prev->next = frame;
178 } else {
179 flv->frames = frame;
180 }
181 break;
182 }
183 prev = trav;
184 trav = trav->next;
185 }
186 if ( !trav ) {
187 prev->next = frame;
188 }
189 }
190}
191
192static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
193{
11a8e425 194//av_log(NULL, AV_LOG_DEBUG, "T%02X S%d T%d R%d F%02X ... R%08X\n", frame->type, frame->size+1, frame->timestamp, 0, frame->flags, frame->size+1+11);
d4f5d74a
GM
195 put_byte(pb,frame->type); // message type
196 put_be24(pb,frame->size+1); // include flags
197 put_be24(pb,frame->timestamp); // time stamp
11a8e425 198 put_be32(pb,frame->reserved); // reserved
d4f5d74a
GM
199 put_byte(pb,frame->flags);
200 put_buffer(pb, frame->data, frame->size);
201 put_be32(pb,frame->size+1+11); // reserved
202 av_free(frame->data);
203}
204
205static void Dump(FLVContext *flv, ByteIOContext *pb, int count)
206{
207 int c=0;
208 FLVFrame *trav = flv->frames;
209 FLVFrame *prev = 0;
210 for (;trav;c++) {
211 trav = trav->next;
212 }
213 trav = flv->frames;
214 for ( ; c >= count; c-- ) {
215 DumpFrame(pb,trav);
216 prev = trav;
217 trav = trav->next;
218 av_free(prev);
219 }
220 flv->frames = trav;
221}
222
223static int flv_write_trailer(AVFormatContext *s)
224{
14b32253
MN
225 int64_t file_size;
226 int flags = 0;
227
d4f5d74a
GM
228 ByteIOContext *pb = &s->pb;
229 FLVContext *flv = s->priv_data;
230
231 Dump(flv,pb,1);
232
14b32253 233 file_size = url_ftell(pb);
d4f5d74a
GM
234 flags |= flv->hasAudio ? 4 : 0;
235 flags |= flv->hasVideo ? 1 : 0;
236 url_fseek(pb, 4, SEEK_SET);
237 put_byte(pb,flags);
238 url_fseek(pb, file_size, SEEK_SET);
239 return 0;
240}
241
242static int flv_write_packet(AVFormatContext *s, int stream_index,
49057904 243 const uint8_t *buf, int size, int64_t timestamp)
d4f5d74a
GM
244{
245 ByteIOContext *pb = &s->pb;
246 AVCodecContext *enc = &s->streams[stream_index]->codec;
247 FLVContext *flv = s->priv_data;
068f2a22 248 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
d4f5d74a 249
068f2a22
MN
250 frame->next = 0;
251 frame->size = size;
252 frame->data = av_malloc(size);
253 frame->timestamp = timestamp;
11a8e425 254 frame->reserved= flv->reserved;
068f2a22
MN
255 memcpy(frame->data,buf,size);
256
257// av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
258
d4f5d74a 259 if (enc->codec_type == CODEC_TYPE_VIDEO) {
d4f5d74a
GM
260 frame->type = 9;
261 frame->flags = 2; // choose h263
262 frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator
d4f5d74a
GM
263 //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
264 //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
d4f5d74a
GM
265 flv->hasVideo = 1;
266
267 InsertSorted(flv,frame);
268
269 flv->frameCount ++;
270 }
271 else if (enc->codec_type == CODEC_TYPE_AUDIO) {
068f2a22 272 int soundFormat = 0x02;
92a26775 273
068f2a22
MN
274 switch (enc->sample_rate) {
275 case 44100:
276 soundFormat |= 0x0C;
277 break;
278 case 22050:
279 soundFormat |= 0x08;
280 break;
281 case 11025:
282 soundFormat |= 0x04;
283 break;
284 case 8000: //nellymoser only
285 case 5512: //not mp3
286 soundFormat |= 0x00;
287 break;
288 default:
289 assert(0);
290 }
d4f5d74a 291
068f2a22
MN
292 if (enc->channels > 1) {
293 soundFormat |= 0x01;
294 }
295
296 switch(enc->codec_id){
297 case CODEC_ID_MP3:
298 soundFormat |= 0x20;
299 break;
300 case 0:
301 soundFormat |= enc->codec_tag<<4;
302 break;
303 default:
304 assert(0);
305 }
306
307 assert(size);
308 if ( flv->initDelay == -1 ) {
309 flv->initDelay = timestamp;
310 }
311
312 frame->type = 8;
313 frame->flags = soundFormat;
d4f5d74a 314
92a26775 315// if ( flv->audioTime == -1 ) {
d4f5d74a
GM
316// flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
317// if ( flv->audioTime < 0 ) {
318// flv->audioTime = 0;
319// }
92a26775 320// }
d4f5d74a 321
068f2a22
MN
322#ifdef CONFIG_MP3LAME
323 if (enc->codec_id == CODEC_ID_MP3 ) {
d4f5d74a
GM
324 int mp3FrameSize = 0;
325 int mp3SampleRate = 0;
326 int mp3IsMono = 0;
327 int mp3SamplesPerFrame = 0;
747a0554
TU
328
329 /* copy out mp3 header from ring buffer */
068f2a22
MN
330 if(!mp3info(buf,&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono))
331 assert(0);
332 assert ( size == mp3FrameSize );
333 assert(enc->sample_rate == mp3SampleRate);
334// assert(enc->frame_size == mp3SamplesPerFrame);
335//av_log(NULL, AV_LOG_DEBUG, "sizes: %d %d\n", enc->frame_size, mp3SamplesPerFrame);
336
337 frame->timestamp = (1000*flv->sampleCount + enc->sample_rate/2)/(enc->sample_rate);
338 flv->sampleCount += mp3SamplesPerFrame;
d4f5d74a
GM
339 }
340#endif
068f2a22
MN
341
342 // We got audio! Make sure we set this to the global flags on closure
343 flv->hasAudio = 1;
344 InsertSorted(flv,frame);
345 }else
346 assert(0);
d4f5d74a
GM
347 Dump(flv,pb,128);
348 put_flush_packet(pb);
349 return 0;
350}
351
352static AVOutputFormat flv_oformat = {
353 "flv",
354 "flv format",
355 "video/x-flashvideo",
356 "flv",
357 sizeof(FLVContext),
358#ifdef CONFIG_MP3LAME
80783dc2 359 CODEC_ID_MP3,
d4f5d74a
GM
360#else // CONFIG_MP3LAME
361 CODEC_ID_NONE,
362#endif // CONFIG_MP3LAME
363 CODEC_ID_FLV1,
364 flv_write_header,
365 flv_write_packet,
366 flv_write_trailer,
367};
368
369int flvenc_init(void)
370{
371 av_register_output_format(&flv_oformat);
372 return 0;
373}