Revision 75293f05 libavformat/flvenc.c

View differences:

libavformat/flvenc.c
21 21
#undef NDEBUG
22 22
#include <assert.h>
23 23

  
24
#define VIDEO_FIFO_SIZE 512
25

  
26
typedef struct FLVFrame {
27
    int type;
28
    int timestamp;
29
    int reserved;
30
    int flags;
31
    uint8_t *data;
32
    int size;
33
    struct FLVFrame *next;
34
} FLVFrame;
35

  
36 24
typedef struct FLVContext {
37 25
    int hasAudio;
38 26
    int hasVideo;
39
    int initDelay;
40
    int64_t sampleCount;
41
    int64_t frameCount;
42 27
    int reserved;
43
    FLVFrame *frames;
44 28
} FLVContext;
45 29

  
46
#ifdef CONFIG_MP3LAME
47

  
48
static const int sSampleRates[3][4] = {
49
    {44100, 48000, 32000, 0},
50
    {22050, 24000, 16000, 0},
51
    {11025, 12000,  8000, 0},
52
};
53

  
54
static 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

  
65
static const int sSamplesPerFrame[3][3] =
66
{
67
    {  384,     1152,    1152 },
68
    {  384,     1152,     576 },
69
    {  384,     1152,     576 }
70
};
71

  
72
static const int sBitsPerSlot[3] = {
73
    32,
74
    8,
75
    8
76
};
77

  
78
static 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 ) {
94
//        printf("Fatal error: mp3 data is not padded!\n");
95
//        exit(0);
96
    }
97

  
98
    *isMono = ((header >>  6) & 0x03) == 0x03;
99

  
100
    if ( (header >> 19 ) & 0x01 ) {
101
        //MPEG1
102
        *sampleRate = sSampleRates[0][sampleRateID];
103
        bitRate = sBitRates[0][layerID][bitRateID] * 1000;
104
        *samplesPerFrame = sSamplesPerFrame[0][layerID];
105
    } else {
106
        if ( (header >> 20) & 0x01 ) {
107
            //MPEG2
108
            *sampleRate = sSampleRates[1][sampleRateID];
109
            bitRate = sBitRates[1][layerID][bitRateID] * 1000;
110
            *samplesPerFrame = sSamplesPerFrame[1][layerID];
111
        } else {
112
            //MPEG2.5
113
            *sampleRate = sSampleRates[2][sampleRateID];
114
            bitRate = sBitRates[1][layerID][bitRateID] * 1000;
115
            *samplesPerFrame = sSamplesPerFrame[2][layerID];
116
        }
117
    }
118
    
119
    *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) );
120
    return 1;
121
}
122
#endif // CONFIG_MP3LAME
123

  
124 30
static void put_be24(ByteIOContext *pb, int value)
125 31
{
126 32
    put_byte(pb, (value>>16) & 0xFF );
......
137 43
    flv->hasAudio = 0;
138 44
    flv->hasVideo = 0;
139 45

  
140
    flv->initDelay = -1;
141

  
142
    flv->frames = 0;
143

  
144 46
    put_tag(pb,"FLV");
145 47
    put_byte(pb,1);
146 48
    put_byte(pb,0); // delayed write
......
163 65
    return 0;
164 66
}
165 67

  
166
static 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;) {
174
            if ( trav->timestamp > frame->timestamp) {
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

  
192
static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
193
{
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);
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
198
    put_be32(pb,frame->reserved); // reserved
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

  
205
static 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

  
223 68
static int flv_write_trailer(AVFormatContext *s)
224 69
{
225 70
    int64_t file_size;
......
228 73
    ByteIOContext *pb = &s->pb;
229 74
    FLVContext *flv = s->priv_data;
230 75

  
231
    Dump(flv,pb,1);
232

  
233 76
    file_size = url_ftell(pb);
234 77
    flags |= flv->hasAudio ? 4 : 0;
235 78
    flags |= flv->hasVideo ? 1 : 0;
......
244 87
    ByteIOContext *pb = &s->pb;
245 88
    AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
246 89
    FLVContext *flv = s->priv_data;
247
    FLVFrame *frame = av_malloc(sizeof(FLVFrame));
248 90
    int size= pkt->size;
249
    uint8_t *buf= pkt->data;
91
    int flags;
250 92

  
251
    frame->next = 0;
252
    frame->size = size;
253
    frame->data = av_malloc(size);
254
    frame->timestamp = pkt->pts;
255
    frame->reserved= flv->reserved;
256
    memcpy(frame->data,buf,size);
257
    
258 93
//    av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
259 94
    
260 95
    if (enc->codec_type == CODEC_TYPE_VIDEO) {
261
        frame->type = 9;
262
        frame->flags = 2; // choose h263
263
        frame->flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
264
        //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
265
        //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
96
        put_byte(pb, 9);
97
        flags = 2; // choose h263
98
        flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
266 99
        flv->hasVideo = 1;
267

  
268
        InsertSorted(flv,frame);
269

  
270
        flv->frameCount ++;
271
    }
272
    else if (enc->codec_type == CODEC_TYPE_AUDIO) {
273
        int soundFormat = 0x02;
100
    } else {
101
        assert(enc->codec_type == CODEC_TYPE_AUDIO);
102
        flags = 0x02;
274 103

  
275 104
        switch (enc->sample_rate) {
276 105
            case    44100:
277
                soundFormat |= 0x0C;
106
                flags |= 0x0C;
278 107
                break;
279 108
            case    22050:
280
                soundFormat |= 0x08;
109
                flags |= 0x08;
281 110
                break;
282 111
            case    11025:
283
                soundFormat |= 0x04;
112
                flags |= 0x04;
284 113
                break;
285 114
            case     8000: //nellymoser only
286 115
            case     5512: //not mp3
287
                soundFormat |= 0x00;
116
                flags |= 0x00;
288 117
                break;
289 118
            default:
290 119
                assert(0);
291 120
        }
292 121

  
293 122
        if (enc->channels > 1) {
294
            soundFormat |= 0x01;
123
            flags |= 0x01;
295 124
        }
296 125
        
297 126
        switch(enc->codec_id){
298 127
        case CODEC_ID_MP3:
299
            soundFormat |= 0x20;
128
            flags |= 0x20;
300 129
            break;
301 130
        case 0:
302
            soundFormat |= enc->codec_tag<<4;
131
            flags |= enc->codec_tag<<4;
303 132
            break;
304 133
        default:
305 134
            assert(0);
306 135
        }
307 136

  
308 137
        assert(size);
309
        if ( flv->initDelay == -1 ) {
310
            flv->initDelay = pkt->pts;
311
        }
312

  
313
        frame->type = 8;
314
        frame->flags = soundFormat;
315

  
316
//            if ( flv->audioTime == -1 ) {
317
//                flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
318
//                if ( flv->audioTime < 0 ) {
319
//                    flv->audioTime = 0;
320
//                }
321
//            }
322

  
323
#ifdef CONFIG_MP3LAME
324
        if (enc->codec_id == CODEC_ID_MP3 ) {
325
            int mp3FrameSize = 0;
326
            int mp3SampleRate = 0;
327
            int mp3IsMono = 0;
328
            int mp3SamplesPerFrame = 0;
329

  
330
            /* copy out mp3 header from ring buffer */
331
            if(!mp3info(buf,&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono))
332
                assert(0);
333
            assert ( size == mp3FrameSize );
334
            assert(enc->sample_rate == mp3SampleRate);
335
//            assert(enc->frame_size == mp3SamplesPerFrame);
336
//av_log(NULL, AV_LOG_DEBUG, "sizes: %d %d\n", enc->frame_size, mp3SamplesPerFrame);
337 138

  
338
            frame->timestamp = (1000*flv->sampleCount + enc->sample_rate/2)/(enc->sample_rate);
339
            flv->sampleCount += mp3SamplesPerFrame;
340
        }
341
#endif
139
        put_byte(pb, 8);
342 140

  
343 141
        // We got audio! Make sure we set this to the global flags on closure
344 142
        flv->hasAudio = 1;
345
        InsertSorted(flv,frame);
346
    }else
347
        assert(0);
348
    Dump(flv,pb,128);
143
    }
144

  
145
    put_be24(pb,size+1); // include flags
146
    put_be24(pb,pkt->pts);
147
    put_be32(pb,flv->reserved);
148
    put_byte(pb,flags);
149
    put_buffer(pb, pkt->data, size);
150
    put_be32(pb,size+1+11); // reserved
151

  
349 152
    put_flush_packet(pb);
350 153
    return 0;
351 154
}

Also available in: Unified diff