raw.c 16.7 KB
Newer Older
F
Fabrice Bellard 已提交
1 2
/* 
 * RAW encoder and decoder
F
Fabrice Bellard 已提交
3
 * Copyright (c) 2001 Fabrice Bellard.
F
Fabrice Bellard 已提交
4
 *
F
Fabrice Bellard 已提交
5 6 7 8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
F
Fabrice Bellard 已提交
9
 *
F
Fabrice Bellard 已提交
10
 * This library is distributed in the hope that it will be useful,
F
Fabrice Bellard 已提交
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
F
Fabrice Bellard 已提交
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
F
Fabrice Bellard 已提交
14
 *
F
Fabrice Bellard 已提交
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
F
Fabrice Bellard 已提交
18 19 20
 */
#include "avformat.h"

21
#ifdef CONFIG_ENCODERS
F
Fabrice Bellard 已提交
22
/* simple formats */
23
static int raw_write_header(struct AVFormatContext *s)
F
Fabrice Bellard 已提交
24 25 26 27
{
    return 0;
}

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

35
static int raw_write_trailer(struct AVFormatContext *s)
F
Fabrice Bellard 已提交
36 37 38
{
    return 0;
}
39
#endif //CONFIG_ENCODERS
F
Fabrice Bellard 已提交
40 41

/* raw input */
42
static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
F
Fabrice Bellard 已提交
43 44
{
    AVStream *st;
F
Fabrice Bellard 已提交
45
    int id;
F
Fabrice Bellard 已提交
46

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

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

F
Fabrice Bellard 已提交
82
#define RAW_PACKET_SIZE 1024
F
Fabrice Bellard 已提交
83

84
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
F
Fabrice Bellard 已提交
85
{
M
Michael Niedermayer 已提交
86
    int ret, size;
F
Fabrice Bellard 已提交
87
    //    AVStream *st = s->streams[0];
M
Michael Niedermayer 已提交
88
    
89
    size= RAW_PACKET_SIZE;
F
Fabrice Bellard 已提交
90

M
Michael Niedermayer 已提交
91
    ret= av_get_packet(&s->pb, pkt, size);
F
Fabrice Bellard 已提交
92 93

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

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

    size = RAW_PACKET_SIZE;

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

123
static int raw_read_close(AVFormatContext *s)
F
Fabrice Bellard 已提交
124 125 126 127
{
    return 0;
}

F
Fabrice Bellard 已提交
128
int pcm_read_seek(AVFormatContext *s, 
129
                  int stream_index, int64_t timestamp, int flags)
F
Fabrice Bellard 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
    AVStream *st;
    int block_align, byte_rate;
    int64_t pos;

    st = s->streams[0];
    switch(st->codec.codec_id) {
    case CODEC_ID_PCM_S16LE:
    case CODEC_ID_PCM_S16BE:
    case CODEC_ID_PCM_U16LE:
    case CODEC_ID_PCM_U16BE:
        block_align = 2 * st->codec.channels;
        byte_rate = block_align * st->codec.sample_rate;
        break;
    case CODEC_ID_PCM_S8:
    case CODEC_ID_PCM_U8:
    case CODEC_ID_PCM_MULAW:
    case CODEC_ID_PCM_ALAW:
        block_align = st->codec.channels;
        byte_rate = block_align * st->codec.sample_rate;
        break;
    default:
        block_align = st->codec.block_align;
        byte_rate = st->codec.bit_rate / 8;
        break;
    }
    
    if (block_align <= 0 || byte_rate <= 0)
        return -1;

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

    /* recompute exact position */
M
Michael Niedermayer 已提交
168
    st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
F
Fabrice Bellard 已提交
169 170 171 172
    url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
    return 0;
}

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

    st = av_new_stream(s, 0);
    if (!st)
        return AVERROR_NOMEM;

    st->codec.codec_type = CODEC_TYPE_AUDIO;
    st->codec.codec_id = CODEC_ID_AC3;
F
Fabrice Bellard 已提交
185
    st->need_parsing = 1;
186 187 188 189
    /* the parameters will be extracted from the compressed bitstream */
    return 0;
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
static int shorten_read_header(AVFormatContext *s,
                               AVFormatParameters *ap)
{
    AVStream *st;

    st = av_new_stream(s, 0);
    if (!st)
        return AVERROR_NOMEM;
    st->codec.codec_type = CODEC_TYPE_AUDIO;
    st->codec.codec_id = CODEC_ID_SHORTEN;
    st->need_parsing = 1;
    /* the parameters will be extracted from the compressed bitstream */
    return 0;
}

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/* dts read */
static int dts_read_header(AVFormatContext *s,
                           AVFormatParameters *ap)
{
    AVStream *st;

    st = av_new_stream(s, 0);
    if (!st)
        return AVERROR_NOMEM;

    st->codec.codec_type = CODEC_TYPE_AUDIO;
    st->codec.codec_id = CODEC_ID_DTS;
    st->need_parsing = 1;
    /* the parameters will be extracted from the compressed bitstream */
    return 0;
}

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

F
Fabrice Bellard 已提交
228
    st = av_new_stream(s, 0);
F
Fabrice Bellard 已提交
229
    if (!st)
F
Fabrice Bellard 已提交
230
        return AVERROR_NOMEM;
F
Fabrice Bellard 已提交
231 232

    st->codec.codec_type = CODEC_TYPE_VIDEO;
F
Fabrice Bellard 已提交
233
    st->codec.codec_id = s->iformat->value;
F
Fabrice Bellard 已提交
234 235
    st->need_parsing = 1;

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

F
Fabrice Bellard 已提交
246 247 248
    return 0;
}

F
Fabrice Bellard 已提交
249 250 251 252 253 254 255
#define SEQ_START_CODE		0x000001b3
#define GOP_START_CODE		0x000001b8
#define PICTURE_START_CODE	0x00000100

/* XXX: improve that by looking at several start codes */
static int mpegvideo_probe(AVProbeData *p)
{
256 257
    int code;
    const uint8_t *d;
F
Fabrice Bellard 已提交
258 259 260 261

    /* we search the first start code. If it is a sequence, gop or
       picture start code then we decide it is an mpeg video
       stream. We do not send highest value to give a chance to mpegts */
262 263 264 265 266 267 268 269 270 271 272 273 274 275
    /* NOTE: the search range was restricted to avoid too many false
       detections */

    if (p->buf_size < 6)
        return 0;
    d = p->buf;
    code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
    if ((code & 0xffffff00) == 0x100) {
        if (code == SEQ_START_CODE ||
            code == GOP_START_CODE ||
            code == PICTURE_START_CODE)
            return 50 - 1;
        else
            return 0;
F
Fabrice Bellard 已提交
276 277 278 279
    }
    return 0;
}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static int h263_probe(AVProbeData *p)
{
    int code;
    const uint8_t *d;

    if (p->buf_size < 6)
        return 0;
    d = p->buf;
    code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
    if (code == 0x20) {
        return 50;
    }
    return 0;
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
static int h261_probe(AVProbeData *p)
{
    int code;
    const uint8_t *d;

    if (p->buf_size < 6)
        return 0;
    d = p->buf;
    code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
    if (code == 0x10) {
        return 50;
    }
    return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

F
Fabrice Bellard 已提交
534
/* pcm formats */
535

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

F
Fabrice Bellard 已提交
550 551 552 553 554
#if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)

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

555
#else
F
Fabrice Bellard 已提交
556

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

#ifdef WORDS_BIGENDIAN
F
Fabrice Bellard 已提交
575 576
#define BE_DEF(s) s
#define LE_DEF(s) NULL
F
Fabrice Bellard 已提交
577
#else
F
Fabrice Bellard 已提交
578 579
#define BE_DEF(s) NULL
#define LE_DEF(s) s
F
Fabrice Bellard 已提交
580 581 582
#endif


F
Fabrice Bellard 已提交
583 584
PCMDEF(s16le, "pcm signed 16 bit little endian format", 
       LE_DEF("sw"), CODEC_ID_PCM_S16LE)
F
Fabrice Bellard 已提交
585

F
Fabrice Bellard 已提交
586 587
PCMDEF(s16be, "pcm signed 16 bit big endian format", 
       BE_DEF("sw"), CODEC_ID_PCM_S16BE)
F
Fabrice Bellard 已提交
588

F
Fabrice Bellard 已提交
589 590
PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
       LE_DEF("uw"), CODEC_ID_PCM_U16LE)
F
Fabrice Bellard 已提交
591

F
Fabrice Bellard 已提交
592 593
PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
       BE_DEF("uw"), CODEC_ID_PCM_U16BE)
F
Fabrice Bellard 已提交
594

F
Fabrice Bellard 已提交
595 596
PCMDEF(s8, "pcm signed 8 bit format", 
       "sb", CODEC_ID_PCM_S8)
F
Fabrice Bellard 已提交
597

F
Fabrice Bellard 已提交
598 599
PCMDEF(u8, "pcm unsigned 8 bit format", 
       "ub", CODEC_ID_PCM_U8)
F
Fabrice Bellard 已提交
600

F
Fabrice Bellard 已提交
601 602
PCMDEF(mulaw, "pcm mu law format", 
       "ul", CODEC_ID_PCM_MULAW)
F
Fabrice Bellard 已提交
603

F
Fabrice Bellard 已提交
604 605
PCMDEF(alaw, "pcm A law format", 
       "al", CODEC_ID_PCM_ALAW)
F
Fabrice Bellard 已提交
606

607
static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
F
Fabrice Bellard 已提交
608 609 610 611 612 613 614
{
    int packet_size, ret, width, height;
    AVStream *st = s->streams[0];

    width = st->codec.width;
    height = st->codec.height;

615 616
    packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
    if (packet_size < 0)
M
Michael Niedermayer 已提交
617
        return -1;
F
Fabrice Bellard 已提交
618

M
Michael Niedermayer 已提交
619
    ret= av_get_packet(&s->pb, pkt, packet_size);
F
Fabrice Bellard 已提交
620 621

    pkt->stream_index = 0;
M
Michael Niedermayer 已提交
622
    if (ret != packet_size) {
623
        return AVERROR_IO;
F
Fabrice Bellard 已提交
624 625 626 627 628
    } else {
        return 0;
    }
}

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

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

656
#ifdef CONFIG_ENCODERS
657
static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
{
    return 0;
}

AVOutputFormat null_oformat = {
    "null",
    "null video format",
    NULL,
    NULL,
    0,
#ifdef WORDS_BIGENDIAN
    CODEC_ID_PCM_S16BE,
#else
    CODEC_ID_PCM_S16LE,
#endif
    CODEC_ID_RAWVIDEO,
    raw_write_header,
    null_write_packet,
    raw_write_trailer,
677
    .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
678
};
679 680 681 682 683 684 685 686
#endif //CONFIG_ENCODERS

#ifndef CONFIG_ENCODERS
#define av_register_output_format(format)
#endif
#ifndef CONFIG_DECODERS
#define av_register_input_format(format)
#endif
687

F
Fabrice Bellard 已提交
688 689
int raw_init(void)
{
690 691 692

    av_register_input_format(&shorten_iformat);

F
Fabrice Bellard 已提交
693 694 695
    av_register_input_format(&ac3_iformat);
    av_register_output_format(&ac3_oformat);

696 697
    av_register_input_format(&dts_iformat);

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

701
    av_register_input_format(&h263_iformat);
F
Fabrice Bellard 已提交
702
    av_register_output_format(&h263_oformat);
703
    
M
Michael Niedermayer 已提交
704
    av_register_input_format(&m4v_iformat);
705
    av_register_output_format(&m4v_oformat);
M
Michael Niedermayer 已提交
706 707 708
    
    av_register_input_format(&h264_iformat);
    av_register_output_format(&h264_oformat);
F
Fabrice Bellard 已提交
709 710 711 712

    av_register_input_format(&mpegvideo_iformat);
    av_register_output_format(&mpeg1video_oformat);

713 714
    av_register_output_format(&mpeg2video_oformat);

F
Fabrice Bellard 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    av_register_input_format(&mjpeg_iformat);
    av_register_output_format(&mjpeg_oformat);

    av_register_input_format(&pcm_s16le_iformat);
    av_register_output_format(&pcm_s16le_oformat);
    av_register_input_format(&pcm_s16be_iformat);
    av_register_output_format(&pcm_s16be_oformat);
    av_register_input_format(&pcm_u16le_iformat);
    av_register_output_format(&pcm_u16le_oformat);
    av_register_input_format(&pcm_u16be_iformat);
    av_register_output_format(&pcm_u16be_oformat);
    av_register_input_format(&pcm_s8_iformat);
    av_register_output_format(&pcm_s8_oformat);
    av_register_input_format(&pcm_u8_iformat);
    av_register_output_format(&pcm_u8_oformat);
    av_register_input_format(&pcm_mulaw_iformat);
    av_register_output_format(&pcm_mulaw_oformat);
    av_register_input_format(&pcm_alaw_iformat);
    av_register_output_format(&pcm_alaw_oformat);

    av_register_input_format(&rawvideo_iformat);
    av_register_output_format(&rawvideo_oformat);
737 738

    av_register_output_format(&null_oformat);
F
Fabrice Bellard 已提交
739 740
    return 0;
}