Statistics
| Branch: | Revision:

ffmpeg / libavformat / raw.c @ 8c653280

History | View | Annotate | Download (12 KB)

1 de6d9b64 Fabrice Bellard
/* 
2
 * RAW encoder and decoder
3 19720f15 Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 */
19
#include "avformat.h"
20
21
/* simple formats */
22 5c91a675 Zdenek Kabelac
static int raw_write_header(struct AVFormatContext *s)
23 de6d9b64 Fabrice Bellard
{
24
    return 0;
25
}
26
27 5c91a675 Zdenek Kabelac
static int raw_write_packet(struct AVFormatContext *s, int stream_index,
28
                            unsigned char *buf, int size, int force_pts)
29 de6d9b64 Fabrice Bellard
{
30
    put_buffer(&s->pb, buf, size);
31
    put_flush_packet(&s->pb);
32
    return 0;
33
}
34
35 5c91a675 Zdenek Kabelac
static int raw_write_trailer(struct AVFormatContext *s)
36 de6d9b64 Fabrice Bellard
{
37
    return 0;
38
}
39
40
/* raw input */
41 0c1a9eda Zdenek Kabelac
static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
42 de6d9b64 Fabrice Bellard
{
43
    AVStream *st;
44 c9a65ca8 Fabrice Bellard
    int id;
45 de6d9b64 Fabrice Bellard
46 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
47 de6d9b64 Fabrice Bellard
    if (!st)
48 c9a65ca8 Fabrice Bellard
        return AVERROR_NOMEM;
49 de6d9b64 Fabrice Bellard
    if (ap) {
50 c9a65ca8 Fabrice Bellard
        id = s->iformat->value;
51
        if (id == CODEC_ID_RAWVIDEO) {
52 de6d9b64 Fabrice Bellard
            st->codec.codec_type = CODEC_TYPE_VIDEO;
53
        } else {
54 c9a65ca8 Fabrice Bellard
            st->codec.codec_type = CODEC_TYPE_AUDIO;
55 de6d9b64 Fabrice Bellard
        }
56 c9a65ca8 Fabrice Bellard
        st->codec.codec_id = id;
57
58 de6d9b64 Fabrice Bellard
        switch(st->codec.codec_type) {
59
        case CODEC_TYPE_AUDIO:
60
            st->codec.sample_rate = ap->sample_rate;
61
            st->codec.channels = ap->channels;
62
            break;
63
        case CODEC_TYPE_VIDEO:
64 14bea432 Michael Niedermayer
            st->codec.frame_rate      = ap->frame_rate;
65
            st->codec.frame_rate_base = ap->frame_rate_base;
66 de6d9b64 Fabrice Bellard
            st->codec.width = ap->width;
67
            st->codec.height = ap->height;
68 63167088 Roman Shaposhnik
            st->codec.pix_fmt = ap->pix_fmt;
69 de6d9b64 Fabrice Bellard
            break;
70
        default:
71 27e084bd Fabrice Bellard
            return -1;
72 de6d9b64 Fabrice Bellard
        }
73
    } else {
74 27e084bd Fabrice Bellard
        return -1;
75 de6d9b64 Fabrice Bellard
    }
76
    return 0;
77
}
78
79 2e93e3aa Fabrice Bellard
#define RAW_PACKET_SIZE 1024
80 de6d9b64 Fabrice Bellard
81 5c91a675 Zdenek Kabelac
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
82 de6d9b64 Fabrice Bellard
{
83 7866eeff Michael Niedermayer
    int ret, size;
84 2a6874fd Fabrice Bellard
    //    AVStream *st = s->streams[0];
85 7866eeff Michael Niedermayer
    
86 231dd3f3 Fabrice Bellard
    size= RAW_PACKET_SIZE;
87 de6d9b64 Fabrice Bellard
88 7866eeff Michael Niedermayer
    if (av_new_packet(pkt, size) < 0)
89 de6d9b64 Fabrice Bellard
        return -EIO;
90
91
    pkt->stream_index = 0;
92 7866eeff Michael Niedermayer
    ret = get_buffer(&s->pb, pkt->data, size);
93 2e93e3aa Fabrice Bellard
    if (ret <= 0) {
94 de6d9b64 Fabrice Bellard
        av_free_packet(pkt);
95 2e93e3aa Fabrice Bellard
        return -EIO;
96
    }
97
    /* note: we need to modify the packet size here to handle the last
98
       packet */
99
    pkt->size = ret;
100 de6d9b64 Fabrice Bellard
    return ret;
101
}
102
103 5c91a675 Zdenek Kabelac
static int raw_read_close(AVFormatContext *s)
104 de6d9b64 Fabrice Bellard
{
105
    return 0;
106
}
107
108 cd66005d Fabrice Bellard
/* ac3 read */
109
static int ac3_read_header(AVFormatContext *s,
110
                           AVFormatParameters *ap)
111
{
112
    AVStream *st;
113
114
    st = av_new_stream(s, 0);
115
    if (!st)
116
        return AVERROR_NOMEM;
117
118
    st->codec.codec_type = CODEC_TYPE_AUDIO;
119
    st->codec.codec_id = CODEC_ID_AC3;
120
    /* the parameters will be extracted from the compressed bitstream */
121
    return 0;
122
}
123
124 de6d9b64 Fabrice Bellard
/* mpeg1/h263 input */
125
static int video_read_header(AVFormatContext *s,
126
                             AVFormatParameters *ap)
127
{
128
    AVStream *st;
129
130 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
131 de6d9b64 Fabrice Bellard
    if (!st)
132 c9a65ca8 Fabrice Bellard
        return AVERROR_NOMEM;
133 de6d9b64 Fabrice Bellard
134
    st->codec.codec_type = CODEC_TYPE_VIDEO;
135 c9a65ca8 Fabrice Bellard
    st->codec.codec_id = s->iformat->value;
136 27e084bd Fabrice Bellard
    /* for mjpeg, specify frame rate */
137 7866eeff Michael Niedermayer
    /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
138
    if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
139 27e084bd Fabrice Bellard
        if (ap) {
140 14bea432 Michael Niedermayer
            st->codec.frame_rate      = ap->frame_rate;
141
            st->codec.frame_rate_base = ap->frame_rate_base;
142 27e084bd Fabrice Bellard
        } else {
143 14bea432 Michael Niedermayer
            st->codec.frame_rate      = 25;
144
            st->codec.frame_rate_base = 1;
145 27e084bd Fabrice Bellard
        }
146
    }
147 de6d9b64 Fabrice Bellard
    return 0;
148
}
149
150 c9a65ca8 Fabrice Bellard
#define SEQ_START_CODE                0x000001b3
151
#define GOP_START_CODE                0x000001b8
152
#define PICTURE_START_CODE        0x00000100
153
154
/* XXX: improve that by looking at several start codes */
155
static int mpegvideo_probe(AVProbeData *p)
156
{
157 fa777321 Fabrice Bellard
    int code;
158
    const uint8_t *d;
159 c9a65ca8 Fabrice Bellard
160
    /* we search the first start code. If it is a sequence, gop or
161
       picture start code then we decide it is an mpeg video
162
       stream. We do not send highest value to give a chance to mpegts */
163 fa777321 Fabrice Bellard
    /* NOTE: the search range was restricted to avoid too many false
164
       detections */
165
166
    if (p->buf_size < 6)
167
        return 0;
168
    d = p->buf;
169
    code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
170
    if ((code & 0xffffff00) == 0x100) {
171
        if (code == SEQ_START_CODE ||
172
            code == GOP_START_CODE ||
173
            code == PICTURE_START_CODE)
174
            return 50 - 1;
175
        else
176
            return 0;
177 c9a65ca8 Fabrice Bellard
    }
178
    return 0;
179
}
180
181 d07f9043 Michael Niedermayer
static int h263_probe(AVProbeData *p)
182
{
183
    int code;
184
    const uint8_t *d;
185
186
    if (p->buf_size < 6)
187
        return 0;
188
    d = p->buf;
189
    code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
190
    if (code == 0x20) {
191
        return 50;
192
    }
193
    return 0;
194
}
195
196 c9a65ca8 Fabrice Bellard
AVInputFormat ac3_iformat = {
197
    "ac3",
198
    "raw ac3",
199
    0,
200
    NULL,
201 cd66005d Fabrice Bellard
    ac3_read_header,
202 de6d9b64 Fabrice Bellard
    raw_read_packet,
203
    raw_read_close,
204 bb76a117 Måns Rullgård
    .extensions = "ac3",
205 de6d9b64 Fabrice Bellard
};
206
207 c9a65ca8 Fabrice Bellard
AVOutputFormat ac3_oformat = {
208 de6d9b64 Fabrice Bellard
    "ac3",
209
    "raw ac3",
210
    "audio/x-ac3", 
211
    "ac3",
212 c9a65ca8 Fabrice Bellard
    0,
213 de6d9b64 Fabrice Bellard
    CODEC_ID_AC3,
214
    0,
215
    raw_write_header,
216
    raw_write_packet,
217
    raw_write_trailer,
218
};
219
220 d07f9043 Michael Niedermayer
AVInputFormat h263_iformat = {
221
    "h263",
222
    "raw h263",
223
    0,
224
    h263_probe,
225
    video_read_header,
226
    raw_read_packet,
227
    raw_read_close,
228
//    .extensions = "h263", //FIXME remove after writing mpeg4_probe
229
    .value = CODEC_ID_H263,
230
};
231
232 c9a65ca8 Fabrice Bellard
AVOutputFormat h263_oformat = {
233 de6d9b64 Fabrice Bellard
    "h263",
234
    "raw h263",
235
    "video/x-h263",
236
    "h263",
237
    0,
238 c9a65ca8 Fabrice Bellard
    0,
239 de6d9b64 Fabrice Bellard
    CODEC_ID_H263,
240
    raw_write_header,
241
    raw_write_packet,
242
    raw_write_trailer,
243 c9a65ca8 Fabrice Bellard
};
244
245 7866eeff Michael Niedermayer
AVInputFormat m4v_iformat = {
246
    "m4v",
247
    "raw MPEG4 video format",
248
    0,
249 2f0f5b20 Michael Niedermayer
    NULL /*mpegvideo_probe*/,
250 7866eeff Michael Niedermayer
    video_read_header,
251
    raw_read_packet,
252
    raw_read_close,
253 bb76a117 Måns Rullgård
    .extensions = "m4v", //FIXME remove after writing mpeg4_probe
254
    .value = CODEC_ID_MPEG4,
255 7866eeff Michael Niedermayer
};
256
257 89b3d7c9 Max Krasnyansky
AVOutputFormat m4v_oformat = {
258
    "m4v",
259
    "raw MPEG4 video format",
260
    NULL,
261
    "m4v",
262
    0,
263
    CODEC_ID_NONE,
264
    CODEC_ID_MPEG4,
265
    raw_write_header,
266
    raw_write_packet,
267
    raw_write_trailer,
268
};
269
270 0da71265 Michael Niedermayer
AVInputFormat h264_iformat = {
271
    "h264",
272
    "raw H264 video format",
273
    0,
274
    NULL /*mpegvideo_probe*/,
275
    video_read_header,
276
    raw_read_packet,
277
    raw_read_close,
278
    .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
279
    .value = CODEC_ID_H264,
280
};
281
282
AVOutputFormat h264_oformat = {
283
    "h264",
284
    "raw H264 video format",
285
    NULL,
286
    "h264",
287
    0,
288
    CODEC_ID_NONE,
289
    CODEC_ID_H264,
290
    raw_write_header,
291
    raw_write_packet,
292
    raw_write_trailer,
293
};
294
295 c9a65ca8 Fabrice Bellard
AVInputFormat mpegvideo_iformat = {
296
    "mpegvideo",
297
    "MPEG video",
298
    0,
299
    mpegvideo_probe,
300 de6d9b64 Fabrice Bellard
    video_read_header,
301
    raw_read_packet,
302
    raw_read_close,
303 bb76a117 Måns Rullgård
    .value = CODEC_ID_MPEG1VIDEO,
304 de6d9b64 Fabrice Bellard
};
305
306 c9a65ca8 Fabrice Bellard
AVOutputFormat mpeg1video_oformat = {
307
    "mpeg1video",
308 de6d9b64 Fabrice Bellard
    "MPEG video",
309
    "video/x-mpeg",
310
    "mpg,mpeg",
311
    0,
312 c9a65ca8 Fabrice Bellard
    0,
313 de6d9b64 Fabrice Bellard
    CODEC_ID_MPEG1VIDEO,
314
    raw_write_header,
315
    raw_write_packet,
316
    raw_write_trailer,
317
};
318
319 c9a65ca8 Fabrice Bellard
AVInputFormat mjpeg_iformat = {
320 27e084bd Fabrice Bellard
    "mjpeg",
321
    "MJPEG video",
322
    0,
323 c9a65ca8 Fabrice Bellard
    NULL,
324 27e084bd Fabrice Bellard
    video_read_header,
325
    raw_read_packet,
326
    raw_read_close,
327 bb76a117 Måns Rullgård
    .extensions = "mjpg,mjpeg",
328
    .value = CODEC_ID_MJPEG,
329 27e084bd Fabrice Bellard
};
330
331 c9a65ca8 Fabrice Bellard
AVOutputFormat mjpeg_oformat = {
332
    "mjpeg",
333
    "MJPEG video",
334
    "video/x-mjpeg",
335
    "mjpg,mjpeg",
336 5ed8fafc Fabrice Bellard
    0,
337
    0,
338 c9a65ca8 Fabrice Bellard
    CODEC_ID_MJPEG,
339 5ed8fafc Fabrice Bellard
    raw_write_header,
340
    raw_write_packet,
341
    raw_write_trailer,
342
};
343
344 c9a65ca8 Fabrice Bellard
/* pcm formats */
345 5ed8fafc Fabrice Bellard
346 c9a65ca8 Fabrice Bellard
#define PCMDEF(name, long_name, ext, codec) \
347
AVInputFormat pcm_ ## name ## _iformat = {\
348
    #name,\
349
    long_name,\
350
    0,\
351
    NULL,\
352
    raw_read_header,\
353
    raw_read_packet,\
354
    raw_read_close,\
355 bb76a117 Måns Rullgård
    .extensions = ext,\
356
    .value = codec,\
357 c9a65ca8 Fabrice Bellard
};\
358
\
359
AVOutputFormat pcm_ ## name ## _oformat = {\
360
    #name,\
361
    long_name,\
362
    NULL,\
363
    ext,\
364
    0,\
365
    codec,\
366
    0,\
367
    raw_write_header,\
368
    raw_write_packet,\
369
    raw_write_trailer,\
370 5ed8fafc Fabrice Bellard
};
371
372
#ifdef WORDS_BIGENDIAN
373 c9a65ca8 Fabrice Bellard
#define BE_DEF(s) s
374
#define LE_DEF(s) NULL
375 5ed8fafc Fabrice Bellard
#else
376 c9a65ca8 Fabrice Bellard
#define BE_DEF(s) NULL
377
#define LE_DEF(s) s
378 5ed8fafc Fabrice Bellard
#endif
379
380
381 c9a65ca8 Fabrice Bellard
PCMDEF(s16le, "pcm signed 16 bit little endian format", 
382
       LE_DEF("sw"), CODEC_ID_PCM_S16LE)
383 5ed8fafc Fabrice Bellard
384 c9a65ca8 Fabrice Bellard
PCMDEF(s16be, "pcm signed 16 bit big endian format", 
385
       BE_DEF("sw"), CODEC_ID_PCM_S16BE)
386 5ed8fafc Fabrice Bellard
387 c9a65ca8 Fabrice Bellard
PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
388
       LE_DEF("uw"), CODEC_ID_PCM_U16LE)
389 5ed8fafc Fabrice Bellard
390 c9a65ca8 Fabrice Bellard
PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
391
       BE_DEF("uw"), CODEC_ID_PCM_U16BE)
392 5ed8fafc Fabrice Bellard
393 c9a65ca8 Fabrice Bellard
PCMDEF(s8, "pcm signed 8 bit format", 
394
       "sb", CODEC_ID_PCM_S8)
395 5ed8fafc Fabrice Bellard
396 c9a65ca8 Fabrice Bellard
PCMDEF(u8, "pcm unsigned 8 bit format", 
397
       "ub", CODEC_ID_PCM_U8)
398 5ed8fafc Fabrice Bellard
399 c9a65ca8 Fabrice Bellard
PCMDEF(mulaw, "pcm mu law format", 
400
       "ul", CODEC_ID_PCM_MULAW)
401 de6d9b64 Fabrice Bellard
402 c9a65ca8 Fabrice Bellard
PCMDEF(alaw, "pcm A law format", 
403
       "al", CODEC_ID_PCM_ALAW)
404 de6d9b64 Fabrice Bellard
405 5c91a675 Zdenek Kabelac
static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
406 de6d9b64 Fabrice Bellard
{
407
    int packet_size, ret, width, height;
408
    AVStream *st = s->streams[0];
409
410
    width = st->codec.width;
411
    height = st->codec.height;
412
413 63167088 Roman Shaposhnik
    packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
414
    if (packet_size < 0)
415 42343f7e Philip Gladstone
        av_abort();
416 de6d9b64 Fabrice Bellard
417
    if (av_new_packet(pkt, packet_size) < 0)
418
        return -EIO;
419
420
    pkt->stream_index = 0;
421 c2c2cd2d Michael Niedermayer
#if 0
422 de6d9b64 Fabrice Bellard
    /* bypass buffered I/O */
423
    ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
424 c2c2cd2d Michael Niedermayer
#else
425
    ret = get_buffer(&s->pb, pkt->data, pkt->size);
426
#endif
427 de6d9b64 Fabrice Bellard
    if (ret != pkt->size) {
428
        av_free_packet(pkt);
429
        return -EIO;
430
    } else {
431
        return 0;
432
    }
433
}
434
435 c9a65ca8 Fabrice Bellard
AVInputFormat rawvideo_iformat = {
436
    "rawvideo",
437
    "raw video format",
438
    0,
439
    NULL,
440
    raw_read_header,
441
    rawvideo_read_packet,
442
    raw_read_close,
443 bb76a117 Måns Rullgård
    .extensions = "yuv",
444
    .value = CODEC_ID_RAWVIDEO,
445 c9a65ca8 Fabrice Bellard
};
446
447
AVOutputFormat rawvideo_oformat = {
448 de6d9b64 Fabrice Bellard
    "rawvideo",
449
    "raw video format",
450
    NULL,
451
    "yuv",
452 c9a65ca8 Fabrice Bellard
    0,
453 de6d9b64 Fabrice Bellard
    CODEC_ID_NONE,
454
    CODEC_ID_RAWVIDEO,
455
    raw_write_header,
456
    raw_write_packet,
457
    raw_write_trailer,
458
};
459 c9a65ca8 Fabrice Bellard
460 87bdd3e5 Fabrice Bellard
static int null_write_packet(struct AVFormatContext *s, 
461
                             int stream_index,
462
                             unsigned char *buf, int size, int force_pts)
463
{
464
    return 0;
465
}
466
467
AVOutputFormat null_oformat = {
468
    "null",
469
    "null video format",
470
    NULL,
471
    NULL,
472
    0,
473
#ifdef WORDS_BIGENDIAN
474
    CODEC_ID_PCM_S16BE,
475
#else
476
    CODEC_ID_PCM_S16LE,
477
#endif
478
    CODEC_ID_RAWVIDEO,
479
    raw_write_header,
480
    null_write_packet,
481
    raw_write_trailer,
482 b1d89f82 Måns Rullgård
    .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
483 87bdd3e5 Fabrice Bellard
};
484
485 c9a65ca8 Fabrice Bellard
int raw_init(void)
486
{
487
    av_register_input_format(&ac3_iformat);
488
    av_register_output_format(&ac3_oformat);
489
490 d07f9043 Michael Niedermayer
    av_register_input_format(&h263_iformat);
491 c9a65ca8 Fabrice Bellard
    av_register_output_format(&h263_oformat);
492 89b3d7c9 Max Krasnyansky
    
493 7866eeff Michael Niedermayer
    av_register_input_format(&m4v_iformat);
494 89b3d7c9 Max Krasnyansky
    av_register_output_format(&m4v_oformat);
495 0da71265 Michael Niedermayer
    
496
    av_register_input_format(&h264_iformat);
497
    av_register_output_format(&h264_oformat);
498 c9a65ca8 Fabrice Bellard
499
    av_register_input_format(&mpegvideo_iformat);
500
    av_register_output_format(&mpeg1video_oformat);
501
502
    av_register_input_format(&mjpeg_iformat);
503
    av_register_output_format(&mjpeg_oformat);
504
505
    av_register_input_format(&pcm_s16le_iformat);
506
    av_register_output_format(&pcm_s16le_oformat);
507
    av_register_input_format(&pcm_s16be_iformat);
508
    av_register_output_format(&pcm_s16be_oformat);
509
    av_register_input_format(&pcm_u16le_iformat);
510
    av_register_output_format(&pcm_u16le_oformat);
511
    av_register_input_format(&pcm_u16be_iformat);
512
    av_register_output_format(&pcm_u16be_oformat);
513
    av_register_input_format(&pcm_s8_iformat);
514
    av_register_output_format(&pcm_s8_oformat);
515
    av_register_input_format(&pcm_u8_iformat);
516
    av_register_output_format(&pcm_u8_oformat);
517
    av_register_input_format(&pcm_mulaw_iformat);
518
    av_register_output_format(&pcm_mulaw_oformat);
519
    av_register_input_format(&pcm_alaw_iformat);
520
    av_register_output_format(&pcm_alaw_oformat);
521
522
    av_register_input_format(&rawvideo_iformat);
523
    av_register_output_format(&rawvideo_oformat);
524 87bdd3e5 Fabrice Bellard
525
    av_register_output_format(&null_oformat);
526 c9a65ca8 Fabrice Bellard
    return 0;
527
}