find_start_code() optimization (about 2x faster now) this may improve decoding speed...
[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
21#define VIDEO_FIFO_SIZE 512
22
23typedef struct FLVFrame {
24 int type;
25 int timestamp;
26 int flags;
27 uint8_t *data;
28 int size;
29 struct FLVFrame *next;
30} FLVFrame;
31
32typedef struct FLVContext {
33 int hasAudio;
34 int hasVideo;
35#ifdef CONFIG_MP3LAME
36 int audioTime;
37 int audioInPos;
38 int audioOutPos;
39 int audioSize;
40 int audioRate;
41 int initDelay;
42 int soundDelay;
43 uint8_t *audioFifo;
44 int64_t sampleCount;
45#endif // CONFIG_MP3LAME
46 int64_t frameCount;
47 FLVFrame *frames;
48} FLVContext;
49
50
51#ifdef CONFIG_MP3LAME
52
53#define AUDIO_FIFO_SIZE 65536
54
55static const int sSampleRates[3][4] = {
56 {44100, 48000, 32000, 0},
57 {22050, 24000, 16000, 0},
58 {11025, 12000, 8000, 0},
59};
60
61static const int sBitRates[2][3][15] = {
62 { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
63 { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
64 { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
65 },
66 { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
67 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
68 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
69 },
70};
71
72static const int sSamplesPerFrame[3][3] =
73{
74 { 384, 1152, 1152 },
75 { 384, 1152, 576 },
76 { 384, 1152, 576 }
77};
78
79static const int sBitsPerSlot[3] = {
80 32,
81 8,
82 8
83};
84
85static int mp3info(void *data, int *byteSize, int *samplesPerFrame, int *sampleRate, int *isMono )
86{
87 uint8_t *dataTmp = (uint8_t *)data;
88 uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3];
89 int layerID = 3 - ((header >> 17) & 0x03);
90 int bitRateID = ((header >> 12) & 0x0f);
91 int sampleRateID = ((header >> 10) & 0x03);
92 int bitRate = 0;
93 int bitsPerSlot = sBitsPerSlot[layerID];
94 int isPadded = ((header >> 9) & 0x01);
95
96 if ( (( header >> 21 ) & 0x7ff) != 0x7ff ) {
97 return 0;
98 }
99
100 if ( !isPadded ) {
101 printf("Fatal error: mp3 data is not padded!\n");
102 exit(0);
103 }
104
105 *isMono = ((header >> 6) & 0x03) == 0x03;
106
107 if ( (header >> 19 ) & 0x01 ) {
108 *sampleRate = sSampleRates[0][sampleRateID];
109 bitRate = sBitRates[0][layerID][bitRateID] * 1000;
110 *samplesPerFrame = sSamplesPerFrame[0][layerID];
111
112 } else {
113 if ( (header >> 20) & 0x01 ) {
114 *sampleRate = sSampleRates[1][sampleRateID];
115 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
116 *samplesPerFrame = sSamplesPerFrame[1][layerID];
117 } else {
118 *sampleRate = sSampleRates[2][sampleRateID];
119 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
120 *samplesPerFrame = sSamplesPerFrame[2][layerID];
121 }
122 }
123
747a0554 124 *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) );
d4f5d74a
GM
125
126 return 1;
127}
128#endif // CONFIG_MP3LAME
129
130static int flv_write_header(AVFormatContext *s)
131{
132 ByteIOContext *pb = &s->pb;
133 FLVContext *flv = s->priv_data;
134
135 av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
136
137 flv->hasAudio = 0;
138 flv->hasVideo = 0;
139
140#ifdef CONFIG_MP3LAME
141 flv->audioTime = -1;
142 flv->audioFifo = av_malloc(AUDIO_FIFO_SIZE);
143 flv->audioInPos = 0;
144 flv->audioOutPos = 0;
145 flv->audioSize = 0;
146 flv->audioRate = 44100;
147 flv->initDelay = -1;
148 flv->soundDelay = 0;
149#endif // CONFIG_MP3LAME
150
151 flv->frames = 0;
152
153 put_tag(pb,"FLV");
154 put_byte(pb,1);
155 put_byte(pb,0); // delayed write
156 put_be32(pb,9);
157 put_be32(pb,0);
158
159 return 0;
160}
161
162static void put_be24(ByteIOContext *pb, int value)
163{
164 put_byte(pb, (value>>16) & 0xFF );
165 put_byte(pb, (value>> 8) & 0xFF );
166 put_byte(pb, (value>> 0) & 0xFF );
167}
168
169static void InsertSorted(FLVContext *flv, FLVFrame *frame)
170{
171 if ( !flv->frames ) {
172 flv->frames = frame;
173 } else {
174 FLVFrame *trav = flv->frames;
175 FLVFrame *prev = 0;
176 for (;trav;) {
177 if ( trav->timestamp >= frame->timestamp ) {
178 frame->next = trav;
179 if ( prev ) {
180 prev->next = frame;
181 } else {
182 flv->frames = frame;
183 }
184 break;
185 }
186 prev = trav;
187 trav = trav->next;
188 }
189 if ( !trav ) {
190 prev->next = frame;
191 }
192 }
193}
194
195static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
196{
197 put_byte(pb,frame->type); // message type
198 put_be24(pb,frame->size+1); // include flags
199 put_be24(pb,frame->timestamp); // time stamp
200 put_be32(pb,0); // reserved
201 put_byte(pb,frame->flags);
202 put_buffer(pb, frame->data, frame->size);
203 put_be32(pb,frame->size+1+11); // reserved
204 av_free(frame->data);
205}
206
207static void Dump(FLVContext *flv, ByteIOContext *pb, int count)
208{
209 int c=0;
210 FLVFrame *trav = flv->frames;
211 FLVFrame *prev = 0;
212 for (;trav;c++) {
213 trav = trav->next;
214 }
215 trav = flv->frames;
216 for ( ; c >= count; c-- ) {
217 DumpFrame(pb,trav);
218 prev = trav;
219 trav = trav->next;
220 av_free(prev);
221 }
222 flv->frames = trav;
223}
224
225static int flv_write_trailer(AVFormatContext *s)
226{
14b32253
MN
227 int64_t file_size;
228 int flags = 0;
229
d4f5d74a
GM
230 ByteIOContext *pb = &s->pb;
231 FLVContext *flv = s->priv_data;
232
233 Dump(flv,pb,1);
234
14b32253 235 file_size = url_ftell(pb);
d4f5d74a
GM
236 flags |= flv->hasAudio ? 4 : 0;
237 flags |= flv->hasVideo ? 1 : 0;
238 url_fseek(pb, 4, SEEK_SET);
239 put_byte(pb,flags);
240 url_fseek(pb, file_size, SEEK_SET);
241 return 0;
242}
243
244static int flv_write_packet(AVFormatContext *s, int stream_index,
49057904 245 const uint8_t *buf, int size, int64_t timestamp)
d4f5d74a
GM
246{
247 ByteIOContext *pb = &s->pb;
248 AVCodecContext *enc = &s->streams[stream_index]->codec;
249 FLVContext *flv = s->priv_data;
250
251 if (enc->codec_type == CODEC_TYPE_VIDEO) {
252 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
253 frame->next = 0;
254 frame->type = 9;
255 frame->flags = 2; // choose h263
256 frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator
257 frame->timestamp = timestamp;
258 //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
259 //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
260 frame->size = size;
261 frame->data = av_malloc(size);
262 memcpy(frame->data,buf,size);
263 flv->hasVideo = 1;
264
265 InsertSorted(flv,frame);
266
267 flv->frameCount ++;
268 }
269 else if (enc->codec_type == CODEC_TYPE_AUDIO) {
270#ifdef CONFIG_MP3LAME
80783dc2 271 if (enc->codec_id == CODEC_ID_MP3 ) {
d4f5d74a
GM
272 int c=0;
273 for (;c<size;c++) {
274 flv->audioFifo[(flv->audioOutPos+c)%AUDIO_FIFO_SIZE] = buf[c];
275 }
276 flv->audioSize += size;
277 flv->audioOutPos += size;
278 flv->audioOutPos %= AUDIO_FIFO_SIZE;
279
280 if ( flv->initDelay == -1 ) {
281 flv->initDelay = timestamp;
282 }
283
284 if ( flv->audioTime == -1 ) {
285 flv->audioTime = timestamp;
286// flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
287// if ( flv->audioTime < 0 ) {
288// flv->audioTime = 0;
289// }
290 }
291 }
292 for ( ; flv->audioSize >= 4 ; ) {
293
294 int mp3FrameSize = 0;
295 int mp3SampleRate = 0;
296 int mp3IsMono = 0;
297 int mp3SamplesPerFrame = 0;
747a0554
TU
298 int c=0;
299
300 /* copy out mp3 header from ring buffer */
301 uint8_t header[4];
302 for (c=0; c<4; c++) {
2d9aa1e3 303 header[c] = flv->audioFifo[(flv->audioInPos+c) % AUDIO_FIFO_SIZE];
747a0554 304 }
d4f5d74a 305
747a0554 306 if ( mp3info(header,&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono) ) {
d4f5d74a
GM
307 if ( flv->audioSize >= mp3FrameSize ) {
308
309 int soundFormat = 0x22;
310 int c=0;
311 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
312
313 flv->audioRate = mp3SampleRate;
314
315 switch (mp3SampleRate) {
316 case 44100:
317 soundFormat |= 0x0C;
318 break;
319 case 22050:
320 soundFormat |= 0x08;
321 break;
322 case 11025:
323 soundFormat |= 0x04;
324 break;
325 }
326
327 if ( !mp3IsMono ) {
328 soundFormat |= 0x01;
329 }
330
331 frame->next = 0;
332 frame->type = 8;
333 frame->flags = soundFormat;
334 frame->timestamp = flv->audioTime;
335 frame->size = mp3FrameSize;
336 frame->data = av_malloc(mp3FrameSize);
337
338 for (;c<mp3FrameSize;c++) {
339 frame->data[c] = flv->audioFifo[(flv->audioInPos+c)%AUDIO_FIFO_SIZE];
340 }
341
342 flv->audioInPos += mp3FrameSize;
343 flv->audioSize -= mp3FrameSize;
344 flv->audioInPos %= AUDIO_FIFO_SIZE;
345 flv->sampleCount += mp3SamplesPerFrame;
346
347 // Reset audio for next round
348 flv->audioTime = -1;
349 // We got audio! Make sure we set this to the global flags on closure
350 flv->hasAudio = 1;
351
352 InsertSorted(flv,frame);
353 }
354 break;
355 }
356 flv->audioInPos ++;
357 flv->audioSize --;
358 flv->audioInPos %= AUDIO_FIFO_SIZE;
359 // no audio in here!
360 flv->audioTime = -1;
361 }
362#endif
363 }
364 Dump(flv,pb,128);
365 put_flush_packet(pb);
366 return 0;
367}
368
369static AVOutputFormat flv_oformat = {
370 "flv",
371 "flv format",
372 "video/x-flashvideo",
373 "flv",
374 sizeof(FLVContext),
375#ifdef CONFIG_MP3LAME
80783dc2 376 CODEC_ID_MP3,
d4f5d74a
GM
377#else // CONFIG_MP3LAME
378 CODEC_ID_NONE,
379#endif // CONFIG_MP3LAME
380 CODEC_ID_FLV1,
381 flv_write_header,
382 flv_write_packet,
383 flv_write_trailer,
384};
385
386int flvenc_init(void)
387{
388 av_register_output_format(&flv_oformat);
389 return 0;
390}