3 * Copyright (c) 2003 The FFmpeg Project.
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.
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.
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
21 #define VIDEO_FIFO_SIZE 512
23 typedef struct FLVFrame {
29 struct FLVFrame *next;
32 typedef struct FLVContext {
45 #endif // CONFIG_MP3LAME
53 #define AUDIO_FIFO_SIZE 65536
55 static const int sSampleRates[3][4] = {
56 {44100, 48000, 32000, 0},
57 {22050, 24000, 16000, 0},
58 {11025, 12000, 8000, 0},
61 static 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}
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}
72 static const int sSamplesPerFrame[3][3] =
79 static const int sBitsPerSlot[3] = {
85 static int mp3info(void *data, int *byteSize, int *samplesPerFrame, int *sampleRate, int *isMono )
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);
93 int bitsPerSlot = sBitsPerSlot[layerID];
94 int isPadded = ((header >> 9) & 0x01);
96 if ( (( header >> 21 ) & 0x7ff) != 0x7ff ) {
101 printf("Fatal error: mp3 data is not padded!\n");
105 *isMono = ((header >> 6) & 0x03) == 0x03;
107 if ( (header >> 19 ) & 0x01 ) {
108 *sampleRate = sSampleRates[0][sampleRateID];
109 bitRate = sBitRates[0][layerID][bitRateID] * 1000;
110 *samplesPerFrame = sSamplesPerFrame[0][layerID];
113 if ( (header >> 20) & 0x01 ) {
114 *sampleRate = sSampleRates[1][sampleRateID];
115 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
116 *samplesPerFrame = sSamplesPerFrame[1][layerID];
118 *sampleRate = sSampleRates[2][sampleRateID];
119 bitRate = sBitRates[1][layerID][bitRateID] * 1000;
120 *samplesPerFrame = sSamplesPerFrame[2][layerID];
124 *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) * bitsPerSlot);
128 #endif // CONFIG_MP3LAME
130 static int flv_write_header(AVFormatContext *s)
132 ByteIOContext *pb = &s->pb;
133 FLVContext *flv = s->priv_data;
135 av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
140 #ifdef CONFIG_MP3LAME
142 flv->audioFifo = av_malloc(AUDIO_FIFO_SIZE);
144 flv->audioOutPos = 0;
146 flv->audioRate = 44100;
149 #endif // CONFIG_MP3LAME
155 put_byte(pb,0); // delayed write
162 static void put_be24(ByteIOContext *pb, int value)
164 put_byte(pb, (value>>16) & 0xFF );
165 put_byte(pb, (value>> 8) & 0xFF );
166 put_byte(pb, (value>> 0) & 0xFF );
169 static void InsertSorted(FLVContext *flv, FLVFrame *frame)
171 if ( !flv->frames ) {
174 FLVFrame *trav = flv->frames;
177 if ( trav->timestamp >= frame->timestamp ) {
195 static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
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);
207 static void Dump(FLVContext *flv, ByteIOContext *pb, int count)
210 FLVFrame *trav = flv->frames;
216 for ( ; c >= count; c-- ) {
225 static int flv_write_trailer(AVFormatContext *s)
227 ByteIOContext *pb = &s->pb;
228 FLVContext *flv = s->priv_data;
232 int64_t file_size = url_ftell(pb);
234 flags |= flv->hasAudio ? 4 : 0;
235 flags |= flv->hasVideo ? 1 : 0;
236 url_fseek(pb, 4, SEEK_SET);
238 url_fseek(pb, file_size, SEEK_SET);
242 static int flv_write_packet(AVFormatContext *s, int stream_index,
243 uint8_t *buf, int size, int timestamp)
245 ByteIOContext *pb = &s->pb;
246 AVCodecContext *enc = &s->streams[stream_index]->codec;
247 FLVContext *flv = s->priv_data;
249 if (enc->codec_type == CODEC_TYPE_VIDEO) {
250 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
253 frame->flags = 2; // choose h263
254 frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator
255 frame->timestamp = timestamp;
256 //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
257 //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
259 frame->data = av_malloc(size);
260 memcpy(frame->data,buf,size);
263 InsertSorted(flv,frame);
267 else if (enc->codec_type == CODEC_TYPE_AUDIO) {
268 #ifdef CONFIG_MP3LAME
269 if (enc->codec_id == CODEC_ID_MP3LAME ) {
272 flv->audioFifo[(flv->audioOutPos+c)%AUDIO_FIFO_SIZE] = buf[c];
274 flv->audioSize += size;
275 flv->audioOutPos += size;
276 flv->audioOutPos %= AUDIO_FIFO_SIZE;
278 if ( flv->initDelay == -1 ) {
279 flv->initDelay = timestamp;
282 if ( flv->audioTime == -1 ) {
283 flv->audioTime = timestamp;
284 // flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
285 // if ( flv->audioTime < 0 ) {
286 // flv->audioTime = 0;
290 for ( ; flv->audioSize >= 4 ; ) {
292 int mp3FrameSize = 0;
293 int mp3SampleRate = 0;
295 int mp3SamplesPerFrame = 0;
297 if ( mp3info(&flv->audioFifo[flv->audioInPos],&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono) ) {
298 if ( flv->audioSize >= mp3FrameSize ) {
300 int soundFormat = 0x22;
302 FLVFrame *frame = av_malloc(sizeof(FLVFrame));
304 flv->audioRate = mp3SampleRate;
306 switch (mp3SampleRate) {
324 frame->flags = soundFormat;
325 frame->timestamp = flv->audioTime;
326 frame->size = mp3FrameSize;
327 frame->data = av_malloc(mp3FrameSize);
329 for (;c<mp3FrameSize;c++) {
330 frame->data[c] = flv->audioFifo[(flv->audioInPos+c)%AUDIO_FIFO_SIZE];
333 flv->audioInPos += mp3FrameSize;
334 flv->audioSize -= mp3FrameSize;
335 flv->audioInPos %= AUDIO_FIFO_SIZE;
336 flv->sampleCount += mp3SamplesPerFrame;
338 // Reset audio for next round
340 // We got audio! Make sure we set this to the global flags on closure
343 InsertSorted(flv,frame);
349 flv->audioInPos %= AUDIO_FIFO_SIZE;
356 put_flush_packet(pb);
360 static AVOutputFormat flv_oformat = {
363 "video/x-flashvideo",
366 #ifdef CONFIG_MP3LAME
368 #else // CONFIG_MP3LAME
370 #endif // CONFIG_MP3LAME
377 int flvenc_init(void)
379 av_register_output_format(&flv_oformat);