Statistics
| Branch: | Revision:

ffmpeg / libavformat / raw.c @ 01f4895c

History | View | Annotate | Download (16.8 KB)

1
/* 
2
 * RAW encoder and decoder
3
 * Copyright (c) 2001 Fabrice Bellard.
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
#ifdef CONFIG_ENCODERS
22
/* simple formats */
23
static int raw_write_header(struct AVFormatContext *s)
24
{
25
    return 0;
26
}
27

    
28
static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
29
{
30
    put_buffer(&s->pb, pkt->data, pkt->size);
31
    put_flush_packet(&s->pb);
32
    return 0;
33
}
34

    
35
static int raw_write_trailer(struct AVFormatContext *s)
36
{
37
    return 0;
38
}
39
#endif //CONFIG_ENCODERS
40

    
41
/* raw input */
42
static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
43
{
44
    AVStream *st;
45
    int id;
46

    
47
    st = av_new_stream(s, 0);
48
    if (!st)
49
        return AVERROR_NOMEM;
50
    if (ap) {
51
        id = s->iformat->value;
52
        if (id == CODEC_ID_RAWVIDEO) {
53
            st->codec->codec_type = CODEC_TYPE_VIDEO;
54
        } else {
55
            st->codec->codec_type = CODEC_TYPE_AUDIO;
56
        }
57
        st->codec->codec_id = id;
58

    
59
        switch(st->codec->codec_type) {
60
        case CODEC_TYPE_AUDIO:
61
            st->codec->sample_rate = ap->sample_rate;
62
            st->codec->channels = ap->channels;
63
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
64
            break;
65
        case CODEC_TYPE_VIDEO:
66
            av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
67
            st->codec->width = ap->width;
68
            st->codec->height = ap->height;
69
            st->codec->pix_fmt = ap->pix_fmt;
70
            if(st->codec->pix_fmt == PIX_FMT_NONE)
71
                st->codec->pix_fmt= PIX_FMT_YUV420P;
72
            break;
73
        default:
74
            return -1;
75
        }
76
    } else {
77
        return -1;
78
    }
79
    return 0;
80
}
81

    
82
#define RAW_PACKET_SIZE 1024
83

    
84
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
85
{
86
    int ret, size;
87
    //    AVStream *st = s->streams[0];
88
    
89
    size= RAW_PACKET_SIZE;
90

    
91
    ret= av_get_packet(&s->pb, pkt, size);
92

    
93
    pkt->stream_index = 0;
94
    if (ret <= 0) {
95
        return AVERROR_IO;
96
    }
97
    /* note: we need to modify the packet size here to handle the last
98
       packet */
99
    pkt->size = ret;
100
    return ret;
101
}
102

    
103
static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
104
{
105
    int ret, size;
106

    
107
    size = RAW_PACKET_SIZE;
108

    
109
    if (av_new_packet(pkt, size) < 0)
110
        return AVERROR_IO;
111
    
112
    pkt->pos= url_ftell(&s->pb);
113
    pkt->stream_index = 0;
114
    ret = get_partial_buffer(&s->pb, pkt->data, size);
115
    if (ret <= 0) {
116
        av_free_packet(pkt);
117
        return AVERROR_IO;
118
    }
119
    pkt->size = ret;
120
    return ret;
121
}
122

    
123
static int raw_read_close(AVFormatContext *s)
124
{
125
    return 0;
126
}
127

    
128
int pcm_read_seek(AVFormatContext *s, 
129
                  int stream_index, int64_t timestamp, int flags)
130
{
131
    AVStream *st;
132
    int block_align, byte_rate;
133
    int64_t pos;
134

    
135
    st = s->streams[0];
136
    switch(st->codec->codec_id) {
137
    case CODEC_ID_PCM_S16LE:
138
    case CODEC_ID_PCM_S16BE:
139
    case CODEC_ID_PCM_U16LE:
140
    case CODEC_ID_PCM_U16BE:
141
        block_align = 2 * st->codec->channels;
142
        byte_rate = block_align * st->codec->sample_rate;
143
        break;
144
    case CODEC_ID_PCM_S8:
145
    case CODEC_ID_PCM_U8:
146
    case CODEC_ID_PCM_MULAW:
147
    case CODEC_ID_PCM_ALAW:
148
        block_align = st->codec->channels;
149
        byte_rate = block_align * st->codec->sample_rate;
150
        break;
151
    default:
152
        block_align = st->codec->block_align;
153
        byte_rate = st->codec->bit_rate / 8;
154
        break;
155
    }
156
    
157
    if (block_align <= 0 || byte_rate <= 0)
158
        return -1;
159

    
160
    /* compute the position by aligning it to block_align */
161
    pos = av_rescale_rnd(timestamp * byte_rate, 
162
                         st->time_base.num, 
163
                         st->time_base.den * (int64_t)block_align,
164
                         (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
165
    pos *= block_align;
166

    
167
    /* recompute exact position */
168
    st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
169
    url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
170
    return 0;
171
}
172

    
173
/* ac3 read */
174
static int ac3_read_header(AVFormatContext *s,
175
                           AVFormatParameters *ap)
176
{
177
    AVStream *st;
178

    
179
    st = av_new_stream(s, 0);
180
    if (!st)
181
        return AVERROR_NOMEM;
182

    
183
    st->codec->codec_type = CODEC_TYPE_AUDIO;
184
    st->codec->codec_id = CODEC_ID_AC3;
185
    st->need_parsing = 1;
186
    /* the parameters will be extracted from the compressed bitstream */
187
    return 0;
188
}
189

    
190
static int shorten_read_header(AVFormatContext *s,
191
                               AVFormatParameters *ap)
192
{
193
    AVStream *st;
194

    
195
    st = av_new_stream(s, 0);
196
    if (!st)
197
        return AVERROR_NOMEM;
198
    st->codec->codec_type = CODEC_TYPE_AUDIO;
199
    st->codec->codec_id = CODEC_ID_SHORTEN;
200
    st->need_parsing = 1;
201
    /* the parameters will be extracted from the compressed bitstream */
202
    return 0;
203
}
204

    
205
/* dts read */
206
static int dts_read_header(AVFormatContext *s,
207
                           AVFormatParameters *ap)
208
{
209
    AVStream *st;
210

    
211
    st = av_new_stream(s, 0);
212
    if (!st)
213
        return AVERROR_NOMEM;
214

    
215
    st->codec->codec_type = CODEC_TYPE_AUDIO;
216
    st->codec->codec_id = CODEC_ID_DTS;
217
    st->need_parsing = 1;
218
    /* the parameters will be extracted from the compressed bitstream */
219
    return 0;
220
}
221

    
222
/* mpeg1/h263 input */
223
static int video_read_header(AVFormatContext *s,
224
                             AVFormatParameters *ap)
225
{
226
    AVStream *st;
227

    
228
    st = av_new_stream(s, 0);
229
    if (!st)
230
        return AVERROR_NOMEM;
231

    
232
    st->codec->codec_type = CODEC_TYPE_VIDEO;
233
    st->codec->codec_id = s->iformat->value;
234
    st->need_parsing = 1;
235

    
236
    /* for mjpeg, specify frame rate */
237
    /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
238
    if (ap && ap->time_base.num) {
239
        av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
240
    } else if ( st->codec->codec_id == CODEC_ID_MJPEG || 
241
                st->codec->codec_id == CODEC_ID_MPEG4 ||
242
                st->codec->codec_id == CODEC_ID_H264) {
243
        av_set_pts_info(st, 64, 1, 25);
244
    }
245

    
246
    return 0;
247
}
248

    
249
#define SEQ_START_CODE                0x000001b3
250
#define GOP_START_CODE                0x000001b8
251
#define PICTURE_START_CODE        0x00000100
252

    
253
/* XXX: improve that by looking at several start codes */
254
static int mpegvideo_probe(AVProbeData *p)
255
{
256
    int code;
257
    const uint8_t *d;
258

    
259
    /* we search the first start code. If it is a sequence, gop or
260
       picture start code then we decide it is an mpeg video
261
       stream. We do not send highest value to give a chance to mpegts */
262
    /* NOTE: the search range was restricted to avoid too many false
263
       detections */
264

    
265
    if (p->buf_size < 6)
266
        return 0;
267
    d = p->buf;
268
    code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
269
    if ((code & 0xffffff00) == 0x100) {
270
        if (code == SEQ_START_CODE ||
271
            code == GOP_START_CODE ||
272
            code == PICTURE_START_CODE)
273
            return 50 - 1;
274
        else
275
            return 0;
276
    }
277
    return 0;
278
}
279

    
280
static int h263_probe(AVProbeData *p)
281
{
282
    int code;
283
    const uint8_t *d;
284

    
285
    if (p->buf_size < 6)
286
        return 0;
287
    d = p->buf;
288
    code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
289
    if (code == 0x20) {
290
        return 50;
291
    }
292
    return 0;
293
}
294

    
295
static int h261_probe(AVProbeData *p)
296
{
297
    int code;
298
    const uint8_t *d;
299

    
300
    if (p->buf_size < 6)
301
        return 0;
302
    d = p->buf;
303
    code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
304
    if (code == 0x10) {
305
        return 50;
306
    }
307
    return 0;
308
}
309

    
310
AVInputFormat shorten_iformat = {
311
    "shn",
312
    "raw shn",
313
    0,
314
    NULL,
315
    shorten_read_header,
316
    raw_read_partial_packet,
317
    raw_read_close,
318
    .extensions = "shn",
319
};
320

    
321
AVInputFormat ac3_iformat = {
322
    "ac3",
323
    "raw ac3",
324
    0,
325
    NULL,
326
    ac3_read_header,
327
    raw_read_partial_packet,
328
    raw_read_close,
329
    .extensions = "ac3",
330
};
331

    
332
#ifdef CONFIG_ENCODERS
333
AVOutputFormat ac3_oformat = {
334
    "ac3",
335
    "raw ac3",
336
    "audio/x-ac3", 
337
    "ac3",
338
    0,
339
    CODEC_ID_AC3,
340
    0,
341
    raw_write_header,
342
    raw_write_packet,
343
    raw_write_trailer,
344
};
345
#endif //CONFIG_ENCODERS
346

    
347
AVInputFormat dts_iformat = {
348
    "dts",
349
    "raw dts",
350
    0,
351
    NULL,
352
    dts_read_header,
353
    raw_read_partial_packet,
354
    raw_read_close,
355
    .extensions = "dts",
356
};
357

    
358
AVInputFormat h261_iformat = {
359
    "h261",
360
    "raw h261",
361
    0,
362
    h261_probe,
363
    video_read_header,
364
    raw_read_partial_packet,
365
    raw_read_close,
366
    .extensions = "h261",
367
    .value = CODEC_ID_H261,
368
};
369

    
370
#ifdef CONFIG_ENCODERS
371
AVOutputFormat h261_oformat = {
372
    "h261",
373
    "raw h261",
374
    "video/x-h261",
375
    "h261",
376
    0,
377
    0,
378
    CODEC_ID_H261,
379
    raw_write_header,
380
    raw_write_packet,
381
    raw_write_trailer,
382
};
383
#endif //CONFIG_ENCODERS
384

    
385
AVInputFormat h263_iformat = {
386
    "h263",
387
    "raw h263",
388
    0,
389
    h263_probe,
390
    video_read_header,
391
    raw_read_partial_packet,
392
    raw_read_close,
393
//    .extensions = "h263", //FIXME remove after writing mpeg4_probe
394
    .value = CODEC_ID_H263,
395
};
396

    
397
#ifdef CONFIG_ENCODERS
398
AVOutputFormat h263_oformat = {
399
    "h263",
400
    "raw h263",
401
    "video/x-h263",
402
    "h263",
403
    0,
404
    0,
405
    CODEC_ID_H263,
406
    raw_write_header,
407
    raw_write_packet,
408
    raw_write_trailer,
409
};
410
#endif //CONFIG_ENCODERS
411

    
412
AVInputFormat m4v_iformat = {
413
    "m4v",
414
    "raw MPEG4 video format",
415
    0,
416
    NULL /*mpegvideo_probe*/,
417
    video_read_header,
418
    raw_read_partial_packet,
419
    raw_read_close,
420
    .extensions = "m4v", //FIXME remove after writing mpeg4_probe
421
    .value = CODEC_ID_MPEG4,
422
};
423

    
424
#ifdef CONFIG_ENCODERS
425
AVOutputFormat m4v_oformat = {
426
    "m4v",
427
    "raw MPEG4 video format",
428
    NULL,
429
    "m4v",
430
    0,
431
    CODEC_ID_NONE,
432
    CODEC_ID_MPEG4,
433
    raw_write_header,
434
    raw_write_packet,
435
    raw_write_trailer,
436
};
437
#endif //CONFIG_ENCODERS
438

    
439
AVInputFormat h264_iformat = {
440
    "h264",
441
    "raw H264 video format",
442
    0,
443
    NULL /*mpegvideo_probe*/,
444
    video_read_header,
445
    raw_read_partial_packet,
446
    raw_read_close,
447
    .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
448
    .value = CODEC_ID_H264,
449
};
450

    
451
#ifdef CONFIG_ENCODERS
452
AVOutputFormat h264_oformat = {
453
    "h264",
454
    "raw H264 video format",
455
    NULL,
456
    "h264",
457
    0,
458
    CODEC_ID_NONE,
459
    CODEC_ID_H264,
460
    raw_write_header,
461
    raw_write_packet,
462
    raw_write_trailer,
463
};
464
#endif //CONFIG_ENCODERS
465

    
466
AVInputFormat mpegvideo_iformat = {
467
    "mpegvideo",
468
    "MPEG video",
469
    0,
470
    mpegvideo_probe,
471
    video_read_header,
472
    raw_read_partial_packet,
473
    raw_read_close,
474
    .value = CODEC_ID_MPEG1VIDEO,
475
};
476

    
477
#ifdef CONFIG_ENCODERS
478
AVOutputFormat mpeg1video_oformat = {
479
    "mpeg1video",
480
    "MPEG video",
481
    "video/x-mpeg",
482
    "mpg,mpeg,m1v",
483
    0,
484
    0,
485
    CODEC_ID_MPEG1VIDEO,
486
    raw_write_header,
487
    raw_write_packet,
488
    raw_write_trailer,
489
};
490
#endif //CONFIG_ENCODERS
491

    
492
#ifdef CONFIG_ENCODERS
493
AVOutputFormat mpeg2video_oformat = {
494
    "mpeg2video",
495
    "MPEG2 video",
496
    NULL,
497
    "m2v",
498
    0,
499
    0,
500
    CODEC_ID_MPEG2VIDEO,
501
    raw_write_header,
502
    raw_write_packet,
503
    raw_write_trailer,
504
};
505
#endif //CONFIG_ENCODERS
506

    
507
AVInputFormat mjpeg_iformat = {
508
    "mjpeg",
509
    "MJPEG video",
510
    0,
511
    NULL,
512
    video_read_header,
513
    raw_read_partial_packet,
514
    raw_read_close,
515
    .extensions = "mjpg,mjpeg",
516
    .value = CODEC_ID_MJPEG,
517
};
518

    
519
#ifdef CONFIG_ENCODERS
520
AVOutputFormat mjpeg_oformat = {
521
    "mjpeg",
522
    "MJPEG video",
523
    "video/x-mjpeg",
524
    "mjpg,mjpeg",
525
    0,
526
    0,
527
    CODEC_ID_MJPEG,
528
    raw_write_header,
529
    raw_write_packet,
530
    raw_write_trailer,
531
};
532
#endif //CONFIG_ENCODERS
533

    
534
/* pcm formats */
535

    
536
#define PCMINPUTDEF(name, long_name, ext, codec) \
537
AVInputFormat pcm_ ## name ## _iformat = {\
538
    #name,\
539
    long_name,\
540
    0,\
541
    NULL,\
542
    raw_read_header,\
543
    raw_read_packet,\
544
    raw_read_close,\
545
    pcm_read_seek,\
546
    .extensions = ext,\
547
    .value = codec,\
548
};
549

    
550
#if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
551

    
552
#define PCMDEF(name, long_name, ext, codec) \
553
    PCMINPUTDEF(name, long_name, ext, codec)
554

    
555
#else
556

    
557
#define PCMDEF(name, long_name, ext, codec) \
558
    PCMINPUTDEF(name, long_name, ext, codec)\
559
\
560
AVOutputFormat pcm_ ## name ## _oformat = {\
561
    #name,\
562
    long_name,\
563
    NULL,\
564
    ext,\
565
    0,\
566
    codec,\
567
    0,\
568
    raw_write_header,\
569
    raw_write_packet,\
570
    raw_write_trailer,\
571
};
572
#endif //CONFIG_ENCODERS
573

    
574
#ifdef WORDS_BIGENDIAN
575
#define BE_DEF(s) s
576
#define LE_DEF(s) NULL
577
#else
578
#define BE_DEF(s) NULL
579
#define LE_DEF(s) s
580
#endif
581

    
582

    
583
PCMDEF(s16le, "pcm signed 16 bit little endian format", 
584
       LE_DEF("sw"), CODEC_ID_PCM_S16LE)
585

    
586
PCMDEF(s16be, "pcm signed 16 bit big endian format", 
587
       BE_DEF("sw"), CODEC_ID_PCM_S16BE)
588

    
589
PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
590
       LE_DEF("uw"), CODEC_ID_PCM_U16LE)
591

    
592
PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
593
       BE_DEF("uw"), CODEC_ID_PCM_U16BE)
594

    
595
PCMDEF(s8, "pcm signed 8 bit format", 
596
       "sb", CODEC_ID_PCM_S8)
597

    
598
PCMDEF(u8, "pcm unsigned 8 bit format", 
599
       "ub", CODEC_ID_PCM_U8)
600

    
601
PCMDEF(mulaw, "pcm mu law format", 
602
       "ul", CODEC_ID_PCM_MULAW)
603

    
604
PCMDEF(alaw, "pcm A law format", 
605
       "al", CODEC_ID_PCM_ALAW)
606

    
607
static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
608
{
609
    int packet_size, ret, width, height;
610
    AVStream *st = s->streams[0];
611

    
612
    width = st->codec->width;
613
    height = st->codec->height;
614

    
615
    packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
616
    if (packet_size < 0)
617
        return -1;
618

    
619
    ret= av_get_packet(&s->pb, pkt, packet_size);
620

    
621
    pkt->stream_index = 0;
622
    if (ret != packet_size) {
623
        return AVERROR_IO;
624
    } else {
625
        return 0;
626
    }
627
}
628

    
629
AVInputFormat rawvideo_iformat = {
630
    "rawvideo",
631
    "raw video format",
632
    0,
633
    NULL,
634
    raw_read_header,
635
    rawvideo_read_packet,
636
    raw_read_close,
637
    .extensions = "yuv,cif,qcif",
638
    .value = CODEC_ID_RAWVIDEO,
639
};
640

    
641
#ifdef CONFIG_ENCODERS
642
AVOutputFormat rawvideo_oformat = {
643
    "rawvideo",
644
    "raw video format",
645
    NULL,
646
    "yuv",
647
    0,
648
    CODEC_ID_NONE,
649
    CODEC_ID_RAWVIDEO,
650
    raw_write_header,
651
    raw_write_packet,
652
    raw_write_trailer,
653
};
654
#endif //CONFIG_ENCODERS
655

    
656
#ifdef CONFIG_ENCODERS
657
static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
658
{
659
    return 0;
660
}
661

    
662
AVOutputFormat null_oformat = {
663
    "null",
664
    "null video format",
665
    NULL,
666
    NULL,
667
    0,
668
#ifdef WORDS_BIGENDIAN
669
    CODEC_ID_PCM_S16BE,
670
#else
671
    CODEC_ID_PCM_S16LE,
672
#endif
673
    CODEC_ID_RAWVIDEO,
674
    raw_write_header,
675
    null_write_packet,
676
    raw_write_trailer,
677
    .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
678
};
679
#endif //CONFIG_ENCODERS
680

    
681
#ifndef CONFIG_ENCODERS
682
#define av_register_output_format(format)
683
#endif
684
#ifndef CONFIG_DECODERS
685
#define av_register_input_format(format)
686
#endif
687

    
688
int raw_init(void)
689
{
690

    
691
    av_register_input_format(&shorten_iformat);
692

    
693
    av_register_input_format(&ac3_iformat);
694
    av_register_output_format(&ac3_oformat);
695

    
696
    av_register_input_format(&dts_iformat);
697

    
698
    av_register_input_format(&h261_iformat);
699
    av_register_output_format(&h261_oformat);
700

    
701
    av_register_input_format(&h263_iformat);
702
    av_register_output_format(&h263_oformat);
703
    
704
    av_register_input_format(&m4v_iformat);
705
    av_register_output_format(&m4v_oformat);
706
    
707
    av_register_input_format(&h264_iformat);
708
    av_register_output_format(&h264_oformat);
709

    
710
    av_register_input_format(&mpegvideo_iformat);
711
    av_register_output_format(&mpeg1video_oformat);
712

    
713
    av_register_output_format(&mpeg2video_oformat);
714

    
715
    av_register_input_format(&mjpeg_iformat);
716
    av_register_output_format(&mjpeg_oformat);
717

    
718
    av_register_input_format(&pcm_s16le_iformat);
719
    av_register_output_format(&pcm_s16le_oformat);
720
    av_register_input_format(&pcm_s16be_iformat);
721
    av_register_output_format(&pcm_s16be_oformat);
722
    av_register_input_format(&pcm_u16le_iformat);
723
    av_register_output_format(&pcm_u16le_oformat);
724
    av_register_input_format(&pcm_u16be_iformat);
725
    av_register_output_format(&pcm_u16be_oformat);
726
    av_register_input_format(&pcm_s8_iformat);
727
    av_register_output_format(&pcm_s8_oformat);
728
    av_register_input_format(&pcm_u8_iformat);
729
    av_register_output_format(&pcm_u8_oformat);
730
    av_register_input_format(&pcm_mulaw_iformat);
731
    av_register_output_format(&pcm_mulaw_oformat);
732
    av_register_input_format(&pcm_alaw_iformat);
733
    av_register_output_format(&pcm_alaw_oformat);
734

    
735
    av_register_input_format(&rawvideo_iformat);
736
    av_register_output_format(&rawvideo_oformat);
737

    
738
    av_register_output_format(&null_oformat);
739
    return 0;
740
}