Statistics
| Branch: | Revision:

ffmpeg / libavformat / raw.c @ 764ef400

History | View | Annotate | Download (12.9 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, int stream_index,
29
                            const uint8_t *buf, int size, int64_t pts)
30
{
31
    put_buffer(&s->pb, buf, size);
32
    put_flush_packet(&s->pb);
33
    return 0;
34
}
35

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

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

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

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

    
81
#define RAW_PACKET_SIZE 1024
82

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

    
90
    if (av_new_packet(pkt, size) < 0)
91
        return -EIO;
92

    
93
    pkt->stream_index = 0;
94
    ret = get_buffer(&s->pb, pkt->data, size);
95
    if (ret <= 0) {
96
        av_free_packet(pkt);
97
        return -EIO;
98
    }
99
    /* note: we need to modify the packet size here to handle the last
100
       packet */
101
    pkt->size = ret;
102
    return ret;
103
}
104

    
105
static int raw_read_close(AVFormatContext *s)
106
{
107
    return 0;
108
}
109

    
110
/* ac3 read */
111
static int ac3_read_header(AVFormatContext *s,
112
                           AVFormatParameters *ap)
113
{
114
    AVStream *st;
115

    
116
    st = av_new_stream(s, 0);
117
    if (!st)
118
        return AVERROR_NOMEM;
119

    
120
    st->codec.codec_type = CODEC_TYPE_AUDIO;
121
    st->codec.codec_id = CODEC_ID_AC3;
122
    /* the parameters will be extracted from the compressed bitstream */
123
    return 0;
124
}
125

    
126
/* mpeg1/h263 input */
127
static int video_read_header(AVFormatContext *s,
128
                             AVFormatParameters *ap)
129
{
130
    AVStream *st;
131

    
132
    st = av_new_stream(s, 0);
133
    if (!st)
134
        return AVERROR_NOMEM;
135

    
136
    st->codec.codec_type = CODEC_TYPE_VIDEO;
137
    st->codec.codec_id = s->iformat->value;
138
    /* for mjpeg, specify frame rate */
139
    /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
140
    if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
141
        if (ap) {
142
            st->codec.frame_rate      = ap->frame_rate;
143
            st->codec.frame_rate_base = ap->frame_rate_base;
144
        } else {
145
            st->codec.frame_rate      = 25;
146
            st->codec.frame_rate_base = 1;
147
        }
148
    }
149
    return 0;
150
}
151

    
152
#define SEQ_START_CODE                0x000001b3
153
#define GOP_START_CODE                0x000001b8
154
#define PICTURE_START_CODE        0x00000100
155

    
156
/* XXX: improve that by looking at several start codes */
157
static int mpegvideo_probe(AVProbeData *p)
158
{
159
    int code;
160
    const uint8_t *d;
161

    
162
    /* we search the first start code. If it is a sequence, gop or
163
       picture start code then we decide it is an mpeg video
164
       stream. We do not send highest value to give a chance to mpegts */
165
    /* NOTE: the search range was restricted to avoid too many false
166
       detections */
167

    
168
    if (p->buf_size < 6)
169
        return 0;
170
    d = p->buf;
171
    code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
172
    if ((code & 0xffffff00) == 0x100) {
173
        if (code == SEQ_START_CODE ||
174
            code == GOP_START_CODE ||
175
            code == PICTURE_START_CODE)
176
            return 50 - 1;
177
        else
178
            return 0;
179
    }
180
    return 0;
181
}
182

    
183
static int h263_probe(AVProbeData *p)
184
{
185
    int code;
186
    const uint8_t *d;
187

    
188
    if (p->buf_size < 6)
189
        return 0;
190
    d = p->buf;
191
    code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
192
    if (code == 0x20) {
193
        return 50;
194
    }
195
    return 0;
196
}
197

    
198
AVInputFormat ac3_iformat = {
199
    "ac3",
200
    "raw ac3",
201
    0,
202
    NULL,
203
    ac3_read_header,
204
    raw_read_packet,
205
    raw_read_close,
206
    .extensions = "ac3",
207
};
208

    
209
#ifdef CONFIG_ENCODERS
210
AVOutputFormat ac3_oformat = {
211
    "ac3",
212
    "raw ac3",
213
    "audio/x-ac3", 
214
    "ac3",
215
    0,
216
    CODEC_ID_AC3,
217
    0,
218
    raw_write_header,
219
    raw_write_packet,
220
    raw_write_trailer,
221
};
222
#endif //CONFIG_ENCODERS
223

    
224
AVInputFormat h263_iformat = {
225
    "h263",
226
    "raw h263",
227
    0,
228
    h263_probe,
229
    video_read_header,
230
    raw_read_packet,
231
    raw_read_close,
232
//    .extensions = "h263", //FIXME remove after writing mpeg4_probe
233
    .value = CODEC_ID_H263,
234
};
235

    
236
#ifdef CONFIG_ENCODERS
237
AVOutputFormat h263_oformat = {
238
    "h263",
239
    "raw h263",
240
    "video/x-h263",
241
    "h263",
242
    0,
243
    0,
244
    CODEC_ID_H263,
245
    raw_write_header,
246
    raw_write_packet,
247
    raw_write_trailer,
248
};
249
#endif //CONFIG_ENCODERS
250

    
251
AVInputFormat m4v_iformat = {
252
    "m4v",
253
    "raw MPEG4 video format",
254
    0,
255
    NULL /*mpegvideo_probe*/,
256
    video_read_header,
257
    raw_read_packet,
258
    raw_read_close,
259
    .extensions = "m4v", //FIXME remove after writing mpeg4_probe
260
    .value = CODEC_ID_MPEG4,
261
};
262

    
263
#ifdef CONFIG_ENCODERS
264
AVOutputFormat m4v_oformat = {
265
    "m4v",
266
    "raw MPEG4 video format",
267
    NULL,
268
    "m4v",
269
    0,
270
    CODEC_ID_NONE,
271
    CODEC_ID_MPEG4,
272
    raw_write_header,
273
    raw_write_packet,
274
    raw_write_trailer,
275
};
276
#endif //CONFIG_ENCODERS
277

    
278
AVInputFormat h264_iformat = {
279
    "h264",
280
    "raw H264 video format",
281
    0,
282
    NULL /*mpegvideo_probe*/,
283
    video_read_header,
284
    raw_read_packet,
285
    raw_read_close,
286
    .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
287
    .value = CODEC_ID_H264,
288
};
289

    
290
#ifdef CONFIG_ENCODERS
291
AVOutputFormat h264_oformat = {
292
    "h264",
293
    "raw H264 video format",
294
    NULL,
295
    "h264",
296
    0,
297
    CODEC_ID_NONE,
298
    CODEC_ID_H264,
299
    raw_write_header,
300
    raw_write_packet,
301
    raw_write_trailer,
302
};
303
#endif //CONFIG_ENCODERS
304

    
305
AVInputFormat mpegvideo_iformat = {
306
    "mpegvideo",
307
    "MPEG video",
308
    0,
309
    mpegvideo_probe,
310
    video_read_header,
311
    raw_read_packet,
312
    raw_read_close,
313
    .value = CODEC_ID_MPEG1VIDEO,
314
};
315

    
316
#ifdef CONFIG_ENCODERS
317
AVOutputFormat mpeg1video_oformat = {
318
    "mpeg1video",
319
    "MPEG video",
320
    "video/x-mpeg",
321
    "mpg,mpeg",
322
    0,
323
    0,
324
    CODEC_ID_MPEG1VIDEO,
325
    raw_write_header,
326
    raw_write_packet,
327
    raw_write_trailer,
328
};
329
#endif //CONFIG_ENCODERS
330

    
331
AVInputFormat mjpeg_iformat = {
332
    "mjpeg",
333
    "MJPEG video",
334
    0,
335
    NULL,
336
    video_read_header,
337
    raw_read_packet,
338
    raw_read_close,
339
    .extensions = "mjpg,mjpeg",
340
    .value = CODEC_ID_MJPEG,
341
};
342

    
343
#ifdef CONFIG_ENCODERS
344
AVOutputFormat mjpeg_oformat = {
345
    "mjpeg",
346
    "MJPEG video",
347
    "video/x-mjpeg",
348
    "mjpg,mjpeg",
349
    0,
350
    0,
351
    CODEC_ID_MJPEG,
352
    raw_write_header,
353
    raw_write_packet,
354
    raw_write_trailer,
355
};
356
#endif //CONFIG_ENCODERS
357

    
358
/* pcm formats */
359
#if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
360

    
361
#define PCMDEF(name, long_name, ext, codec) \
362
AVInputFormat pcm_ ## name ## _iformat = {\
363
    #name,\
364
    long_name,\
365
    0,\
366
    NULL,\
367
    raw_read_header,\
368
    raw_read_packet,\
369
    raw_read_close,\
370
    .extensions = ext,\
371
    .value = codec,\
372
};
373

    
374
#else
375

    
376
#define PCMDEF(name, long_name, ext, codec) \
377
AVInputFormat pcm_ ## name ## _iformat = {\
378
    #name,\
379
    long_name,\
380
    0,\
381
    NULL,\
382
    raw_read_header,\
383
    raw_read_packet,\
384
    raw_read_close,\
385
    .extensions = ext,\
386
    .value = codec,\
387
};\
388
\
389
AVOutputFormat pcm_ ## name ## _oformat = {\
390
    #name,\
391
    long_name,\
392
    NULL,\
393
    ext,\
394
    0,\
395
    codec,\
396
    0,\
397
    raw_write_header,\
398
    raw_write_packet,\
399
    raw_write_trailer,\
400
};
401
#endif //CONFIG_ENCODERS
402

    
403
#ifdef WORDS_BIGENDIAN
404
#define BE_DEF(s) s
405
#define LE_DEF(s) NULL
406
#else
407
#define BE_DEF(s) NULL
408
#define LE_DEF(s) s
409
#endif
410

    
411

    
412
PCMDEF(s16le, "pcm signed 16 bit little endian format", 
413
       LE_DEF("sw"), CODEC_ID_PCM_S16LE)
414

    
415
PCMDEF(s16be, "pcm signed 16 bit big endian format", 
416
       BE_DEF("sw"), CODEC_ID_PCM_S16BE)
417

    
418
PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
419
       LE_DEF("uw"), CODEC_ID_PCM_U16LE)
420

    
421
PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
422
       BE_DEF("uw"), CODEC_ID_PCM_U16BE)
423

    
424
PCMDEF(s8, "pcm signed 8 bit format", 
425
       "sb", CODEC_ID_PCM_S8)
426

    
427
PCMDEF(u8, "pcm unsigned 8 bit format", 
428
       "ub", CODEC_ID_PCM_U8)
429

    
430
PCMDEF(mulaw, "pcm mu law format", 
431
       "ul", CODEC_ID_PCM_MULAW)
432

    
433
PCMDEF(alaw, "pcm A law format", 
434
       "al", CODEC_ID_PCM_ALAW)
435

    
436
static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
437
{
438
    int packet_size, ret, width, height;
439
    AVStream *st = s->streams[0];
440

    
441
    width = st->codec.width;
442
    height = st->codec.height;
443

    
444
    packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
445
    if (packet_size < 0)
446
        av_abort();
447

    
448
    if (av_new_packet(pkt, packet_size) < 0)
449
        return -EIO;
450

    
451
    pkt->stream_index = 0;
452
#if 0
453
    /* bypass buffered I/O */
454
    ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
455
#else
456
    ret = get_buffer(&s->pb, pkt->data, pkt->size);
457
#endif
458
    if (ret != pkt->size) {
459
        av_free_packet(pkt);
460
        return -EIO;
461
    } else {
462
        return 0;
463
    }
464
}
465

    
466
AVInputFormat rawvideo_iformat = {
467
    "rawvideo",
468
    "raw video format",
469
    0,
470
    NULL,
471
    raw_read_header,
472
    rawvideo_read_packet,
473
    raw_read_close,
474
    .extensions = "yuv",
475
    .value = CODEC_ID_RAWVIDEO,
476
};
477

    
478
#ifdef CONFIG_ENCODERS
479
AVOutputFormat rawvideo_oformat = {
480
    "rawvideo",
481
    "raw video format",
482
    NULL,
483
    "yuv",
484
    0,
485
    CODEC_ID_NONE,
486
    CODEC_ID_RAWVIDEO,
487
    raw_write_header,
488
    raw_write_packet,
489
    raw_write_trailer,
490
};
491
#endif //CONFIG_ENCODERS
492

    
493
#ifdef CONFIG_ENCODERS
494
static int null_write_packet(struct AVFormatContext *s, 
495
                             int stream_index,
496
                             const uint8_t *buf, int size, int64_t pts)
497
{
498
    return 0;
499
}
500

    
501
AVOutputFormat null_oformat = {
502
    "null",
503
    "null video format",
504
    NULL,
505
    NULL,
506
    0,
507
#ifdef WORDS_BIGENDIAN
508
    CODEC_ID_PCM_S16BE,
509
#else
510
    CODEC_ID_PCM_S16LE,
511
#endif
512
    CODEC_ID_RAWVIDEO,
513
    raw_write_header,
514
    null_write_packet,
515
    raw_write_trailer,
516
    .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
517
};
518
#endif //CONFIG_ENCODERS
519

    
520
#ifndef CONFIG_ENCODERS
521
#define av_register_output_format(format)
522
#endif
523
#ifndef CONFIG_DECODERS
524
#define av_register_input_format(format)
525
#endif
526

    
527
int raw_init(void)
528
{
529
    av_register_input_format(&ac3_iformat);
530
    av_register_output_format(&ac3_oformat);
531

    
532
    av_register_input_format(&h263_iformat);
533
    av_register_output_format(&h263_oformat);
534
    
535
    av_register_input_format(&m4v_iformat);
536
    av_register_output_format(&m4v_oformat);
537
    
538
    av_register_input_format(&h264_iformat);
539
    av_register_output_format(&h264_oformat);
540

    
541
    av_register_input_format(&mpegvideo_iformat);
542
    av_register_output_format(&mpeg1video_oformat);
543

    
544
    av_register_input_format(&mjpeg_iformat);
545
    av_register_output_format(&mjpeg_oformat);
546

    
547
    av_register_input_format(&pcm_s16le_iformat);
548
    av_register_output_format(&pcm_s16le_oformat);
549
    av_register_input_format(&pcm_s16be_iformat);
550
    av_register_output_format(&pcm_s16be_oformat);
551
    av_register_input_format(&pcm_u16le_iformat);
552
    av_register_output_format(&pcm_u16le_oformat);
553
    av_register_input_format(&pcm_u16be_iformat);
554
    av_register_output_format(&pcm_u16be_oformat);
555
    av_register_input_format(&pcm_s8_iformat);
556
    av_register_output_format(&pcm_s8_oformat);
557
    av_register_input_format(&pcm_u8_iformat);
558
    av_register_output_format(&pcm_u8_oformat);
559
    av_register_input_format(&pcm_mulaw_iformat);
560
    av_register_output_format(&pcm_mulaw_oformat);
561
    av_register_input_format(&pcm_alaw_iformat);
562
    av_register_output_format(&pcm_alaw_oformat);
563

    
564
    av_register_input_format(&rawvideo_iformat);
565
    av_register_output_format(&rawvideo_oformat);
566

    
567
    av_register_output_format(&null_oformat);
568
    return 0;
569
}