~ubuntu-branches/ubuntu/hardy/avidemux/hardy

« back to all changes in this revision

Viewing changes to avidemux/ADM_lavformat/ADM_lavformat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-12-15 17:13:20 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215171320-w79pvpehxx2fr217
Tags: 1:2.3.0-0.0ubuntu1
* Merge from debian-multimedia.org, remaining Ubuntu change:
  - desktop file,
  - no support for ccache and make -j.
* Closes Ubuntu: #69614.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#undef realloc
36
36
#undef free
37
37
#include <ADM_assert.h>
38
 
 
39
38
#include "ADM_library/default.h"
40
39
#include "ADM_toolkit/toolkit.hxx"
41
40
 
82
81
        video_st=NULL;
83
82
        _fps1000=0;
84
83
        _audioByterate=0;
85
 
        _total=0;
 
84
        _lastAudioDts=0;
86
85
        _frameNo=0;
87
86
        _running=0;
88
87
        _curDTS=0;
 
88
        _audioFq=0;
89
89
}
90
90
//___________________________________________________________________________
91
91
lavMuxer::~lavMuxer()
93
93
        close();
94
94
}
95
95
//___________________________________________________________________________
96
 
uint8_t lavMuxer::open(const char *filename, uint32_t inbitrate,ADM_MUXER_TYPE type, aviInfo *info, WAVHeader *audioheader)
 
96
uint8_t lavMuxer::open(const char *filename, uint32_t inbitrate,ADM_MUXER_TYPE type, aviInfo *info, WAVHeader *audioheader) 
 
97
{
 
98
        return open(filename,inbitrate,type,info,0,NULL,audioheader,0,NULL);
 
99
}
 
100
 
 
101
 
 
102
uint8_t lavMuxer::open(const char *filename,uint32_t inbitrate, ADM_MUXER_TYPE type, aviInfo *info,
 
103
              uint32_t videoExtraDataSize, uint8_t *videoExtraData, WAVHeader *audioheader,
 
104
              uint32_t audioextraSize,uint8_t *audioextraData)
97
105
{
98
106
 AVCodecContext *c;
99
107
        _type=type;
112
120
                case MUXER_SVCD:
113
121
                        fmt = guess_format("svcd", NULL, NULL);
114
122
                        break;
 
123
                case MUXER_MP4:
 
124
                        fmt = guess_format("mp4", NULL, NULL);
 
125
                        break;
 
126
                case MUXER_PSP:
 
127
                        fmt = guess_format("psp", NULL, NULL);
 
128
                        break;                        
115
129
                default:
116
130
                        fmt=NULL;
117
131
        }
141
155
        c = video_st->codec;
142
156
        switch(_type)
143
157
        {
 
158
                case MUXER_MP4:
 
159
                case MUXER_PSP:
 
160
                        if(isMpeg4Compatible(info->fcc))
 
161
                        {
 
162
                                c->codec_id = CODEC_ID_MPEG4;
 
163
                                c->has_b_frames=1; // in doubt...
 
164
                        }else
 
165
                        {
 
166
                                if(isH264Compatible(info->fcc))
 
167
                                {
 
168
                                        c->has_b_frames=1; // in doubt...
 
169
                                        c->codec_id = CODEC_ID_H264;
 
170
                                        c->codec=new AVCodec;
 
171
                                        memset(c->codec,0,sizeof(AVCodec));
 
172
                                        c->codec->name=ADM_strdup("H264");
 
173
                                }
 
174
                                else
 
175
                                {
 
176
                                         c->codec_id = CODEC_ID_MPEG4; // Default value
 
177
                                        printf("Ooops, cant mux that...\n");
 
178
                                        printf("Ooops, cant mux that...\n");
 
179
                                        printf("Ooops, cant mux that...\n");
 
180
                                        //return 0;
 
181
                                }
 
182
                        }
 
183
                        if(videoExtraDataSize)
 
184
                        {
 
185
                                c->extradata=videoExtraData;
 
186
                                c->extradata_size= videoExtraDataSize;
 
187
                        }
 
188
                        if(MUXER_PSP==_type)
 
189
                        {
 
190
                            c->rc_buffer_size=8*1024*224;
 
191
                            c->rc_max_rate=768*1000;
 
192
                            c->rc_min_rate=0;
 
193
                            c->bit_rate=768*1000;
 
194
                        }
 
195
                        else
 
196
                        {
 
197
                            c->rc_buffer_size=8*1024*224;
 
198
                            c->rc_max_rate=9500*1000;
 
199
                            c->rc_min_rate=0;
 
200
                            if(!inbitrate)
 
201
                                    c->bit_rate=9000*1000;
 
202
                            else
 
203
                                    c->bit_rate=inbitrate;
 
204
                        }
 
205
                        break;
144
206
                case MUXER_TS:
145
207
                        c->codec_id = CODEC_ID_MPEG2VIDEO;
146
208
                        c->rc_buffer_size=8*1024*224;
198
260
        switch(_fps1000)
199
261
        {
200
262
                case 25000:
201
 
                         c->time_base= (AVRational){25025,1001};
 
263
                         c->time_base= (AVRational){1001,25025};
202
264
                        //c->frame_rate = 25025;  
203
265
                        //c->frame_rate_base = 1001;    
204
266
                        break;
208
270
                        c->frame_rate_base = 1001;      
209
271
                        break;
210
272
*/
 
273
                        if(_type==MUXER_MP4 || _type==MUXER_PSP)
 
274
                        {
 
275
                                 c->time_base= (AVRational){1001,24000};
 
276
                                break;
 
277
                        }
211
278
                case  29970:
212
 
                         c->time_base= (AVRational){30000,1001};
 
279
                         c->time_base= (AVRational){1001,30000};
213
280
                        //c->frame_rate = 30000;  
214
281
                        //c->frame_rate_base = 1001;    
215
282
                        break;
216
283
                default:
217
 
                        GUI_Error_HIG("Incompatible frame rate", NULL);
218
 
                        return 0;
 
284
                        if(_type==MUXER_MP4 || _type==MUXER_PSP)
 
285
                        {
 
286
                                c->time_base= (AVRational){1000,_fps1000};
 
287
                                break;
 
288
                        }
 
289
                        else
 
290
                        {
 
291
                          GUI_Error_HIG(_("Incompatible frame rate"), NULL);
 
292
                            return 0;
 
293
                        }
219
294
        }
220
295
 
221
296
                        
226
301
        
227
302
        // Audio
228
303
        //________
 
304
        if(audioheader)
 
305
        {
229
306
        audio_st = av_new_stream(oc, 1);
230
307
        if (!audio_st) 
231
308
        {
235
312
 
236
313
                
237
314
        c = audio_st->codec;
238
 
        if(audioheader->encoding==WAV_AC3)
239
 
                c->codec_id = CODEC_ID_AC3;
240
 
        else
241
 
                c->codec_id = CODEC_ID_MP2;
 
315
        c->frame_size=1024; //For AAC mainly, sample per frame
 
316
        printf("[LavFormat] Bitrate %u\n",(audioheader->byterate*8)/1000);
 
317
        switch(audioheader->encoding)
 
318
        {
 
319
                case WAV_AC3: c->codec_id = CODEC_ID_AC3;break;
 
320
                case WAV_MP2: c->codec_id = CODEC_ID_MP2;break;
 
321
                case WAV_MP3:
 
322
#warning FIXME : Probe deeper
 
323
                            c->frame_size=1152;
 
324
                            c->codec_id = CODEC_ID_MP3;
 
325
                            break;
 
326
                case WAV_PCM: 
 
327
                                // One chunk is 10 ms (1/100 of fq)
 
328
                                c->frame_size=4;
 
329
                                c->codec_id = CODEC_ID_PCM_S16LE;break;
 
330
                case WAV_AAC: 
 
331
                                c->extradata=audioextraData;
 
332
                                c->extradata_size= audioextraSize;
 
333
                                c->codec_id = CODEC_ID_AAC;
 
334
                                break;
 
335
                default:
 
336
                        printf("Cant mux that ! audio\n"); 
 
337
                        printf("Cant mux that ! audio\n");
 
338
                        c->codec_id = CODEC_ID_MP2;
 
339
                        return 0;
 
340
                        break;
 
341
        }
242
342
        c->codec_type = CODEC_TYPE_AUDIO;
243
343
        
244
344
        c->bit_rate = audioheader->byterate*8;
245
 
        c->sample_rate = audioheader->frequency;
 
345
        c->rc_buffer_size=(c->bit_rate/(2*8)); // 500 ms worth
 
346
        _audioFq=c->sample_rate = audioheader->frequency;
246
347
        c->channels = audioheader->channels;
247
 
        
 
348
        _audioByterate=audioheader->byterate;
 
349
        }
 
350
        // /audio
248
351
        
249
352
        
250
353
//----------------------
251
354
        switch(_type)
252
355
        {
 
356
                case MUXER_PSP:
 
357
                case MUXER_MP4:
 
358
                        oc->mux_rate=10080*1000; // Needed ?
 
359
                        break;
 
360
 
253
361
                case MUXER_TS:
254
362
                        oc->mux_rate=10080*1000;
255
363
                        break;
292
400
        printf("lavformat mpeg muxer initialized\n");
293
401
        
294
402
        _running=1;
295
 
        _audioByterate=audioheader->byterate;
296
 
        one=(1000*1000*1000)/_fps1000;
 
403
 
 
404
        one=(1000*1000*1000)/_fps1000; 
297
405
        _curDTS=one;
298
406
 
299
407
        return 1;
300
408
}
301
409
//___________________________________________________________________________
302
 
uint8_t lavMuxer::writeAudioPacket(uint32_t len, uint8_t *buf)
 
410
uint8_t lavMuxer::writeAudioPacket(uint32_t len, uint8_t *buf,uint32_t sample)
303
411
{
304
412
        
305
 
        int ret;
306
 
        AVPacket pkt;
307
 
        double f;
308
 
        
 
413
        int ret;
 
414
        AVPacket pkt;
 
415
        double f;
 
416
 
 
417
           if(!audio_st) return 0;
 
418
           if(!len) return 1;
309
419
            av_init_packet(&pkt);
310
 
            
311
 
            f=_total;
312
 
            f*=1000.*1000.;
313
 
            f/=_audioByterate;
314
 
            _total+=len;
315
 
            
316
 
            pkt.dts=pkt.pts=(int64_t)floor(f+2000);
317
 
           
318
 
            pkt.stream_index=1;
319
 
           
320
 
          //  pkt.flags |= PKT_FLAG_KEY; 
 
420
 
 
421
 
 
422
            pkt.dts=pkt.pts=(int64_t)sample2time_us(sample);
 
423
 
 
424
            pkt.flags |= PKT_FLAG_KEY; 
321
425
            pkt.data= buf;
322
426
            pkt.size= len;
323
 
            aprintf("A: frame  pts%d\n",pkt.pts); 
 
427
            pkt.stream_index=1;
 
428
 
 
429
            aprintf("A: sample: %d frame_pts: %d fq: %d\n",(int32_t )sample,(int32_t )pkt.dts,audio_st->codec->sample_rate); 
 
430
 
 
431
            ret = av_write_frame(oc, &pkt);
 
432
            _lastAudioDts=pkt.dts;
 
433
            if(ret) 
 
434
            {
 
435
                        printf("Error writing audio packet\n");
 
436
                        return 0;
 
437
            }
 
438
            return 1;
 
439
}
 
440
//________________________________________________________________________
 
441
uint64_t  lavMuxer::sample2time_us( uint32_t sample )
 
442
{
 
443
double f;
 
444
 
 
445
                f=sample;
 
446
                f*=1000.*1000.;
 
447
                f/=_audioFq;              // Sample / Frequency = time in seconds *10E6 to get in in us
324
448
                
325
 
            ret = av_write_frame(oc, &pkt);
326
 
            if(ret) 
327
 
                        {
328
 
                                printf("Error writing audio packet\n");
329
 
                                return 0;
330
 
                        }
331
 
        
332
 
        return 1;
 
449
                return (uint64_t)floor(f);
 
450
 
333
451
}
334
452
//___________________________________________________________________________
335
453
uint8_t lavMuxer::needAudio( void )
336
454
{
337
455
        
 
456
        if(!audio_st) return 0;
 
457
 
338
458
        double f;
339
 
        uint64_t dts;
340
 
                f=_total;
341
 
                f*=1000.*1000.;
342
 
                f/=_audioByterate;
343
 
                f+=2000;
344
 
                dts=(uint64_t)floor(f);
 
459
        uint64_t dts=_lastAudioDts;  // Last audio dts
 
460
 
 
461
                
345
462
                aprintf("Need audio  ?: %llu / %llu : %llu\n ",dts,_curDTS,_curDTS+one);
346
 
                if((dts>=_curDTS) && (dts<=_curDTS+one)) return 1;
 
463
                if((dts+5000>=_curDTS) && (dts<=_curDTS+one)) return 1;
347
464
                if(dts<=_curDTS)
348
465
                {
349
466
                        printf("LavMuxer:Audio DTS is too low %llu / %llu!\n",dts,_curDTS);
352
469
                return 0;
353
470
}
354
471
//___________________________________________________________________________
355
 
uint8_t lavMuxer::writeVideoPacket(uint32_t len, uint8_t *buf,uint32_t frameno,uint32_t displayframe)
 
472
uint8_t lavMuxer::writeVideoPacket(ADMBitstream *bitstream)
356
473
{
357
474
int ret;
358
475
 
360
477
        AVPacket pkt;
361
478
            av_init_packet(&pkt);
362
479
            
363
 
        p=displayframe+2;      // Pts
 
480
        p=bitstream->ptsFrame+1;      // Pts           // Time p/fps1000=out/den  out=p*den*1000/fps1000
364
481
        p=(p*1000*1000*1000);
365
 
        p=p/_fps1000;
 
482
        p=p/_fps1000;                  // in us
366
483
        
367
 
        d=frameno;              // dts
 
484
        d=bitstream->dtsFrame;          // dts
368
485
        d=(d*1000*1000*1000);
369
486
        d=d/_fps1000;
370
487
        
371
488
        
372
 
        _curDTS=(int64_t)floor(d);
373
 
        
 
489
        _curDTS=(int64_t)floor(d);      
 
490
        
 
491
        // Rescale
 
492
#define RESCALE(x) x=x*video_st->codec->time_base.den*1000.;\
 
493
                   x=x/_fps1000;
 
494
        
 
495
        p=bitstream->ptsFrame+1;
 
496
        RESCALE(p);
 
497
        
 
498
        d=bitstream->dtsFrame;
 
499
        RESCALE(d);
 
500
        
374
501
        pkt.dts=(int64_t)floor(d);
375
502
        pkt.pts=(int64_t)floor(p);
376
 
        
 
503
        
 
504
       // printf("Lavformat : Pts :%u dts:%u",displayframe,frameno);
377
505
        aprintf("Lavformat : Pts :%llu dts:%llu",pkt.pts,pkt.dts);
378
506
        pkt.stream_index=0;
379
507
           
380
 
        pkt.data= buf;
381
 
        pkt.size= len;
 
508
        pkt.data= bitstream->data;
 
509
        pkt.size= bitstream->len;
382
510
        // Look if it is a gop start or seq start
383
 
        if(!buf[0] &&  !buf[1] && buf[2]==1)
 
511
        if(_type==MUXER_MP4 || _type==MUXER_PSP)
 
512
        {
 
513
            if(bitstream->flags & AVI_KEY_FRAME) 
 
514
                        pkt.flags |= PKT_FLAG_KEY;
 
515
        }else
 
516
            if(!bitstream->data[0] &&  !bitstream->data[1] && bitstream->data[2]==1)
384
517
        {
385
 
                if(buf[3]==0xb3 || buf[3]==0xb8 ) // Seq start or gop start
 
518
            if(bitstream->data[3]==0xb3 || bitstream->data[3]==0xb8 ) // Seq start or gop start
386
519
                pkt.flags |= PKT_FLAG_KEY;
387
520
                //printf("Intra\n"); 
388
521
        }
393
526
                printf("Error writing video packet\n");
394
527
                return 0;
395
528
        }
396
 
        aprintf("V: frame %lu pts%d\n",frameno,pkt.pts);
 
529
        aprintf("V: frame %lu pts%d\n",bitstream->dtsFrame,pkt.pts);
397
530
        
398
531
        return 1;
399
532
}
434
567
{
435
568
        return 0;
436
569
}
 
570
extern "C"
 
571
{
 
572
     extern  int        mpegps_init(void );
 
573
     extern  int        movenc_init(void );
 
574
};
 
575
extern URLProtocol file_protocol ;
 
576
extern AVInputFormat matroska_demuxer;
 
577
uint8_t lavformat_init(void)
 
578
{
 
579
                mpegps_init();
 
580
                movenc_init();
 
581
                av_register_input_format(&matroska_demuxer);
 
582
                register_protocol(&file_protocol);
 
583
}
 
584
 
437
585
//___________________________________________________________________________
438
586
//EOF
439
587