Revision b2cac184

View differences:

libav/mpeg.c
17 17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 18
 */
19 19
#include "avformat.h"
20
#include "tick.h"
21 20

  
22 21
#define MAX_PAYLOAD_SIZE 4096
23 22
#define NB_STREAMS 2
......
28 27
    UINT8 id;
29 28
    int max_buffer_size; /* in bytes */
30 29
    int packet_number;
31
    INT64 pts;
32
    Ticker pts_ticker;
33 30
    INT64 start_pts;
34 31
} StreamInfo;
35 32

  
......
77 74
    init_put_bits(&pb, buf, 128, NULL, NULL);
78 75

  
79 76
    put_bits(&pb, 32, PACK_START_CODE);
80
    put_bits(&pb, 4, 0x2);
77
    if (s->is_mpeg2) {
78
        put_bits(&pb, 2, 0x2);
79
    } else {
80
        put_bits(&pb, 4, 0x2);
81
    }
81 82
    put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07));
82 83
    put_bits(&pb, 1, 1);
83 84
    put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff));
84 85
    put_bits(&pb, 1, 1);
85 86
    put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff));
86 87
    put_bits(&pb, 1, 1);
88
    if (s->is_mpeg2) {
89
        /* clock extension */
90
        put_bits(&pb, 9, 0);
91
        put_bits(&pb, 1, 1);
92
    }
87 93
    put_bits(&pb, 1, 1);
88 94
    put_bits(&pb, 22, s->mux_rate);
89 95
    put_bits(&pb, 1, 1);
90

  
96
    if (s->is_mpeg2) {
97
        put_bits(&pb, 5, 0x1f); /* reserved */
98
        put_bits(&pb, 3, 0); /* stuffing length */
99
    }
91 100
    flush_put_bits(&pb);
92 101
    return pbBufPtr(&pb) - pb.buf;
93 102
}
......
217 226
        /* every 2 seconds */
218 227
        s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
219 228
    
220
    if (s->is_vcd)
229
    if (s->is_mpeg2)
230
        /* every 200 packets. Need to look at the spec.  */
231
        s->system_header_freq = s->pack_header_freq * 40;
232
    else if (s->is_vcd)
221 233
        /* every 40 packets, this is my invention */
222 234
        s->system_header_freq = s->pack_header_freq * 40;
223 235
    else
224
        /* every 10 seconds */
225 236
        s->system_header_freq = s->pack_header_freq * 5;
226 237
    
227
    
228 238
    for(i=0;i<ctx->nb_streams;i++) {
229 239
        stream = ctx->streams[i]->priv_data;
230 240
        stream->buffer_ptr = 0;
231 241
        stream->packet_number = 0;
232
        stream->pts = 0;
233 242
        stream->start_pts = -1;
234

  
235
        st = ctx->streams[i];
236
        switch (st->codec.codec_type) {
237
        case CODEC_TYPE_AUDIO:
238
            ticker_init(&stream->pts_ticker,
239
                        st->codec.sample_rate,
240
                        90000 * st->codec.frame_size);
241
            break;
242
        case CODEC_TYPE_VIDEO:
243
            ticker_init(&stream->pts_ticker,
244
                        st->codec.frame_rate,
245
                        90000 * FRAME_RATE_BASE);
246
            break;
247
        default:
248
            av_abort();
249
        }
250 243
    }
251 244
    return 0;
252 245
 fail:
......
354 347
}
355 348

  
356 349
static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
357
                                 UINT8 *buf, int size, int force_pts)
350
                                 UINT8 *buf, int size, int pts)
358 351
{
359 352
    MpegMuxContext *s = ctx->priv_data;
360 353
    AVStream *st = ctx->streams[stream_index];
......
364 357
    while (size > 0) {
365 358
        /* set pts */
366 359
        if (stream->start_pts == -1) {
367
            if (force_pts)
368
                stream->pts = force_pts;
369
            stream->start_pts = stream->pts;
360
            stream->start_pts = pts;
370 361
        }
371 362
        len = s->packet_data_max_size - stream->buffer_ptr;
372 363
        if (len > size)
......
378 369
        while (stream->buffer_ptr >= s->packet_data_max_size) {
379 370
            /* output the packet */
380 371
            if (stream->start_pts == -1)
381
                stream->start_pts = stream->pts;
372
                stream->start_pts = pts;
382 373
            flush_packet(ctx, stream_index, 0);
383 374
        }
384 375
    }
385

  
386
    stream->pts += ticker_tick(&stream->pts_ticker, 1);
387
    //if (st->codec.codec_type == CODEC_TYPE_VIDEO)
388
    //    fprintf(stderr,"\nVideo PTS: %6lld", stream->pts);
389
    //else
390
    //    fprintf(stderr,"\nAudio PTS: %6lld", stream->pts);
391 376
    return 0;
392 377
}
393 378

  
......
510 495
    int len, size, startcode, i, c, flags, header_len, type, codec_id;
511 496
    INT64 pts, dts;
512 497

  
513
    /* next start code (should be immediately after */
498
    /* next start code (should be immediately after) */
514 499
 redo:
515 500
    m->header_state = 0xff;
516 501
    size = MAX_SYNC_SIZE;
......
536 521
        goto redo;
537 522

  
538 523
    len = get_be16(&s->pb);
539
    pts = 0;
540
    dts = 0;
524
    pts = AV_NOPTS_VALUE;
525
    dts = AV_NOPTS_VALUE;
541 526
    /* stuffing */
542 527
    for(;;) {
543 528
        c = get_byte(&s->pb);
......
555 540
    if ((c & 0xf0) == 0x20) {
556 541
        pts = get_pts(&s->pb, c);
557 542
        len -= 4;
558
        dts = pts;
559 543
    } else if ((c & 0xf0) == 0x30) {
560 544
        pts = get_pts(&s->pb, c);
561 545
        dts = get_pts(&s->pb, -1);
......
573 557
            goto redo;
574 558
        if ((flags & 0xc0) == 0x80) {
575 559
            pts = get_pts(&s->pb, -1);
576
            dts = pts;
577 560
            header_len -= 5;
578 561
            len -= 5;
579 562
        } if ((flags & 0xc0) == 0xc0) {

Also available in: Unified diff