stream copy pts fix
[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;
92a26775 40// int audioRate;
d4f5d74a
GM
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 ) {
92a26775
MN
101// printf("Fatal error: mp3 data is not padded!\n");
102// exit(0);
d4f5d74a
GM
103 }
104
105 *isMono = ((header >> 6) & 0x03) == 0x03;
106
107 if ( (header >> 19 ) & 0x01 ) {
92a26775 108 //MPEG1
d4f5d74a
GM
109 *sampleRate = sSampleRates[0][sampleRateID];
110 bitRate = sBitRates[0][layerID][bitRateID] * 1000;
111 *samplesPerFrame = sSamplesPerFrame[0][layerID];
d4f5d74a
GM
112 } else {
113 if ( (header >> 20) & 0x01 ) {
92a26775 114 //MPEG2
d4f5d74a
GM
115 *sampleRate = sSampleRates[1][sampleRateID];
116 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
117 *samplesPerFrame = sSamplesPerFrame[1][layerID];
118 } else {
92a26775 119 //MPEG2.5
d4f5d74a
GM
120 *sampleRate = sSampleRates[2][sampleRateID];
121 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
122 *samplesPerFrame = sSamplesPerFrame[2][layerID];
123 }
124 }
92a26775 125
747a0554 126 *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) );
d4f5d74a
GM
127 return 1;
128}
129#endif // CONFIG_MP3LAME
130
131static int flv_write_header(AVFormatContext *s)
132{
133 ByteIOContext *pb = &s->pb;
134 FLVContext *flv = s->priv_data;
135
136 av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
137
138 flv->hasAudio = 0;
139 flv->hasVideo = 0;
140
141#ifdef CONFIG_MP3LAME
142 flv->audioTime = -1;
143 flv->audioFifo = av_malloc(AUDIO_FIFO_SIZE);
144 flv->audioInPos = 0;
145 flv->audioOutPos = 0;
146 flv->audioSize = 0;
92a26775 147// flv->audioRate = 44100;
d4f5d74a
GM
148 flv->initDelay = -1;
149 flv->soundDelay = 0;
150#endif // CONFIG_MP3LAME
151
152 flv->frames = 0;
153
154 put_tag(pb,"FLV");
155 put_byte(pb,1);
156 put_byte(pb,0); // delayed write
157 put_be32(pb,9);
158 put_be32(pb,0);
159
160 return 0;
161}
162
163static void put_be24(ByteIOContext *pb, int value)
164{
165 put_byte(pb, (value>>16) & 0xFF );
166 put_byte(pb, (value>> 8) & 0xFF );
167 put_byte(pb, (value>> 0) & 0xFF );
168}
169
170static void InsertSorted(FLVContext *flv, FLVFrame *frame)
171{
172 if ( !flv->frames ) {
173 flv->frames = frame;
174 } else {
175 FLVFrame *trav = flv->frames;
176 FLVFrame *prev = 0;
177 for (;trav;) {
178 if ( trav->timestamp >= frame->timestamp ) {
179 frame->next = trav;
180 if ( prev ) {
181 prev->next = frame;
182 } else {
183 flv->frames = frame;
184 }
185 break;
186 }
187 prev = trav;
188 trav = trav->next;
189 }
190 if ( !trav ) {
191 prev->next = frame;
192 }
193 }
194}
195
196static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
197{
198 put_byte(pb,frame->type); // message type
199 put_be24(pb,frame->size+1); // include flags
200 put_be24(pb,frame->timestamp); // time stamp
201 put_be32(pb,0); // reserved
202 put_byte(pb,frame->flags);
203 put_buffer(pb, frame->data, frame->size);
204 put_be32(pb,frame->size+1+11); // reserved
205 av_free(frame->data);
206}
207
208static void Dump(FLVContext *flv, ByteIOContext *pb, int count)
209{
210 int c=0;
211 FLVFrame *trav = flv->frames;
212 FLVFrame *prev = 0;
213 for (;trav;c++) {
214 trav = trav->next;
215 }
216 trav = flv->frames;
217 for ( ; c >= count; c-- ) {
218 DumpFrame(pb,trav);
219 prev = trav;
220 trav = trav->next;
221 av_free(prev);
222 }
223 flv->frames = trav;
224}
225
226static int flv_write_trailer(AVFormatContext *s)
227{
14b32253
MN
228 int64_t file_size;
229 int flags = 0;
230
d4f5d74a
GM
231 ByteIOContext *pb = &s->pb;
232 FLVContext *flv = s->priv_data;
233
234 Dump(flv,pb,1);
235
14b32253 236 file_size = url_ftell(pb);
d4f5d74a
GM
237 flags |= flv->hasAudio ? 4 : 0;
238 flags |= flv->hasVideo ? 1 : 0;
239 url_fseek(pb, 4, SEEK_SET);
240 put_byte(pb,flags);
241 url_fseek(pb, file_size, SEEK_SET);
242 return 0;
243}
244
245static int flv_write_packet(AVFormatContext *s, int stream_index,
49057904 246 const uint8_t *buf, int size, int64_t timestamp)
d4f5d74a
GM
247{
248 ByteIOContext *pb = &s->pb;
249 AVCodecContext *enc = &s->streams[stream_index]->codec;
250 FLVContext *flv = s->priv_data;
251
252 if (enc->codec_type == CODEC_TYPE_VIDEO) {
253 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
254 frame->next = 0;
255 frame->type = 9;
256 frame->flags = 2; // choose h263
257 frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator
258 frame->timestamp = timestamp;
259 //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
260 //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
261 frame->size = size;
262 frame->data = av_malloc(size);
263 memcpy(frame->data,buf,size);
264 flv->hasVideo = 1;
265
266 InsertSorted(flv,frame);
267
268 flv->frameCount ++;
269 }
270 else if (enc->codec_type == CODEC_TYPE_AUDIO) {
271#ifdef CONFIG_MP3LAME
80783dc2 272 if (enc->codec_id == CODEC_ID_MP3 ) {
d4f5d74a 273 int c=0;
92a26775 274
d4f5d74a
GM
275 for (;c<size;c++) {
276 flv->audioFifo[(flv->audioOutPos+c)%AUDIO_FIFO_SIZE] = buf[c];
277 }
278 flv->audioSize += size;
279 flv->audioOutPos += size;
280 flv->audioOutPos %= AUDIO_FIFO_SIZE;
281
282 if ( flv->initDelay == -1 ) {
283 flv->initDelay = timestamp;
284 }
285
92a26775 286// if ( flv->audioTime == -1 ) {
d4f5d74a
GM
287 flv->audioTime = timestamp;
288// flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
289// if ( flv->audioTime < 0 ) {
290// flv->audioTime = 0;
291// }
92a26775 292// }
d4f5d74a
GM
293 }
294 for ( ; flv->audioSize >= 4 ; ) {
295
296 int mp3FrameSize = 0;
297 int mp3SampleRate = 0;
298 int mp3IsMono = 0;
299 int mp3SamplesPerFrame = 0;
747a0554
TU
300 int c=0;
301
302 /* copy out mp3 header from ring buffer */
303 uint8_t header[4];
304 for (c=0; c<4; c++) {
2d9aa1e3 305 header[c] = flv->audioFifo[(flv->audioInPos+c) % AUDIO_FIFO_SIZE];
747a0554 306 }
d4f5d74a 307
747a0554 308 if ( mp3info(header,&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono) ) {
d4f5d74a
GM
309 if ( flv->audioSize >= mp3FrameSize ) {
310
311 int soundFormat = 0x22;
312 int c=0;
313 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
314
92a26775 315// flv->audioRate = mp3SampleRate;
d4f5d74a
GM
316
317 switch (mp3SampleRate) {
318 case 44100:
319 soundFormat |= 0x0C;
320 break;
321 case 22050:
322 soundFormat |= 0x08;
323 break;
324 case 11025:
325 soundFormat |= 0x04;
326 break;
327 }
328
329 if ( !mp3IsMono ) {
330 soundFormat |= 0x01;
331 }
332
333 frame->next = 0;
334 frame->type = 8;
335 frame->flags = soundFormat;
336 frame->timestamp = flv->audioTime;
92a26775 337 frame->timestamp = (1000*flv->sampleCount + mp3SampleRate/2)/(mp3SampleRate);
d4f5d74a
GM
338 frame->size = mp3FrameSize;
339 frame->data = av_malloc(mp3FrameSize);
340
341 for (;c<mp3FrameSize;c++) {
342 frame->data[c] = flv->audioFifo[(flv->audioInPos+c)%AUDIO_FIFO_SIZE];
343 }
344
345 flv->audioInPos += mp3FrameSize;
346 flv->audioSize -= mp3FrameSize;
347 flv->audioInPos %= AUDIO_FIFO_SIZE;
348 flv->sampleCount += mp3SamplesPerFrame;
349
92a26775 350 flv->audioTime += 1000*mp3SamplesPerFrame/mp3SampleRate;
d4f5d74a
GM
351 // We got audio! Make sure we set this to the global flags on closure
352 flv->hasAudio = 1;
353
354 InsertSorted(flv,frame);
92a26775
MN
355// av_log(NULL,AV_LOG_DEBUG, "insert sound\n");
356 continue;
d4f5d74a 357 }
92a26775 358// av_log(NULL,AV_LOG_DEBUG, "insuficent data\n");
d4f5d74a
GM
359 break;
360 }
92a26775 361 av_log(NULL,AV_LOG_DEBUG, "head trashed\n");
d4f5d74a
GM
362 flv->audioInPos ++;
363 flv->audioSize --;
364 flv->audioInPos %= AUDIO_FIFO_SIZE;
365 // no audio in here!
366 flv->audioTime = -1;
367 }
368#endif
369 }
370 Dump(flv,pb,128);
371 put_flush_packet(pb);
372 return 0;
373}
374
375static AVOutputFormat flv_oformat = {
376 "flv",
377 "flv format",
378 "video/x-flashvideo",
379 "flv",
380 sizeof(FLVContext),
381#ifdef CONFIG_MP3LAME
80783dc2 382 CODEC_ID_MP3,
d4f5d74a
GM
383#else // CONFIG_MP3LAME
384 CODEC_ID_NONE,
385#endif // CONFIG_MP3LAME
386 CODEC_ID_FLV1,
387 flv_write_header,
388 flv_write_packet,
389 flv_write_trailer,
390};
391
392int flvenc_init(void)
393{
394 av_register_output_format(&flv_oformat);
395 return 0;
396}