Statistics
| Branch: | Revision:

ffmpeg / libav / raw.c @ 87bdd3e5

History | View | Annotate | Download (11.5 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
int raw_write_header(struct AVFormatContext *s)
23
{
24
    return 0;
25
}
26
27
int raw_write_packet(struct AVFormatContext *s, 
28
                     int stream_index,
29 10bb7023 Juanjo
                     unsigned char *buf, int size, int force_pts)
30 de6d9b64 Fabrice Bellard
{
31
    put_buffer(&s->pb, buf, size);
32
    put_flush_packet(&s->pb);
33
    return 0;
34
}
35
36
int raw_write_trailer(struct AVFormatContext *s)
37
{
38
    return 0;
39
}
40
41
/* raw input */
42
static int raw_read_header(AVFormatContext *s,
43
                           AVFormatParameters *ap)
44
{
45
    AVStream *st;
46 c9a65ca8 Fabrice Bellard
    int id;
47 de6d9b64 Fabrice Bellard
48 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
49 de6d9b64 Fabrice Bellard
    if (!st)
50 c9a65ca8 Fabrice Bellard
        return AVERROR_NOMEM;
51 de6d9b64 Fabrice Bellard
    if (ap) {
52 c9a65ca8 Fabrice Bellard
        id = s->iformat->value;
53
        if (id == CODEC_ID_RAWVIDEO) {
54 de6d9b64 Fabrice Bellard
            st->codec.codec_type = CODEC_TYPE_VIDEO;
55
        } else {
56 c9a65ca8 Fabrice Bellard
            st->codec.codec_type = CODEC_TYPE_AUDIO;
57 de6d9b64 Fabrice Bellard
        }
58 c9a65ca8 Fabrice Bellard
        st->codec.codec_id = id;
59
60 de6d9b64 Fabrice Bellard
        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.width = ap->width;
68
            st->codec.height = ap->height;
69
            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
int raw_read_packet(AVFormatContext *s,
82
                    AVPacket *pkt)
83
{
84 7866eeff Michael Niedermayer
    int ret, size;
85
    AVStream *st = s->streams[0];
86
    
87
    if(st->codec.codec_id == CODEC_ID_MPEG4)
88
        size= 1024*1024; //cant handle partial frames
89
    else
90
        size= RAW_PACKET_SIZE;
91 de6d9b64 Fabrice Bellard
92 7866eeff Michael Niedermayer
    if (av_new_packet(pkt, size) < 0)
93 de6d9b64 Fabrice Bellard
        return -EIO;
94
95
    pkt->stream_index = 0;
96 7866eeff Michael Niedermayer
    ret = get_buffer(&s->pb, pkt->data, size);
97 2e93e3aa Fabrice Bellard
    if (ret <= 0) {
98 de6d9b64 Fabrice Bellard
        av_free_packet(pkt);
99 2e93e3aa Fabrice Bellard
        return -EIO;
100
    }
101
    /* note: we need to modify the packet size here to handle the last
102
       packet */
103
    pkt->size = ret;
104 de6d9b64 Fabrice Bellard
    return ret;
105
}
106
107
int raw_read_close(AVFormatContext *s)
108
{
109
    return 0;
110
}
111
112
/* mp3 read */
113
static int mp3_read_header(AVFormatContext *s,
114
                           AVFormatParameters *ap)
115
{
116
    AVStream *st;
117
118 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
119 de6d9b64 Fabrice Bellard
    if (!st)
120 c9a65ca8 Fabrice Bellard
        return AVERROR_NOMEM;
121 de6d9b64 Fabrice Bellard
122
    st->codec.codec_type = CODEC_TYPE_AUDIO;
123
    st->codec.codec_id = CODEC_ID_MP2;
124 27e084bd Fabrice Bellard
    /* the parameters will be extracted from the compressed bitstream */
125 de6d9b64 Fabrice Bellard
    return 0;
126
}
127
128
/* mpeg1/h263 input */
129
static int video_read_header(AVFormatContext *s,
130
                             AVFormatParameters *ap)
131
{
132
    AVStream *st;
133
134 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
135 de6d9b64 Fabrice Bellard
    if (!st)
136 c9a65ca8 Fabrice Bellard
        return AVERROR_NOMEM;
137 de6d9b64 Fabrice Bellard
138
    st->codec.codec_type = CODEC_TYPE_VIDEO;
139 c9a65ca8 Fabrice Bellard
    st->codec.codec_id = s->iformat->value;
140 27e084bd Fabrice Bellard
    /* for mjpeg, specify frame rate */
141 7866eeff Michael Niedermayer
    /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
142
    if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
143 27e084bd Fabrice Bellard
        if (ap) {
144
            st->codec.frame_rate = ap->frame_rate;
145
        } else {
146
            st->codec.frame_rate = 25 * FRAME_RATE_BASE;
147
        }
148
    }
149 de6d9b64 Fabrice Bellard
    return 0;
150
}
151
152 c9a65ca8 Fabrice Bellard
#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, c, i;
160
    code = 0xff;
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
    for(i=0;i<p->buf_size;i++) {
166
        c = p->buf[i];
167
        code = (code << 8) | c;
168
        if ((code & 0xffffff00) == 0x100) {
169
            if (code == SEQ_START_CODE ||
170
                code == GOP_START_CODE ||
171
                code == PICTURE_START_CODE)
172
                return AVPROBE_SCORE_MAX - 1;
173
            else
174
                return 0;
175
        }
176
    }
177
    return 0;
178
}
179
180
AVInputFormat mp3_iformat = {
181
    "mp3",
182 de6d9b64 Fabrice Bellard
    "MPEG audio",
183 c9a65ca8 Fabrice Bellard
    0,
184
    NULL,
185
    mp3_read_header,
186
    raw_read_packet,
187
    raw_read_close,
188 bb76a117 Måns Rullgård
    .extensions = "mp2,mp3", /* XXX: use probe */
189 c9a65ca8 Fabrice Bellard
};
190
191
AVOutputFormat mp2_oformat = {
192
    "mp2",
193
    "MPEG audio layer 2",
194 de6d9b64 Fabrice Bellard
    "audio/x-mpeg",
195
    "mp2,mp3",
196 c9a65ca8 Fabrice Bellard
    0,
197 de6d9b64 Fabrice Bellard
    CODEC_ID_MP2,
198
    0,
199
    raw_write_header,
200
    raw_write_packet,
201
    raw_write_trailer,
202 c9a65ca8 Fabrice Bellard
};
203 de6d9b64 Fabrice Bellard
204 c9a65ca8 Fabrice Bellard
205
AVInputFormat ac3_iformat = {
206
    "ac3",
207
    "raw ac3",
208
    0,
209
    NULL,
210
    raw_read_header,
211 de6d9b64 Fabrice Bellard
    raw_read_packet,
212
    raw_read_close,
213 bb76a117 Måns Rullgård
    .extensions = "ac3",
214
    .value = CODEC_ID_AC3,
215 de6d9b64 Fabrice Bellard
};
216
217 c9a65ca8 Fabrice Bellard
AVOutputFormat ac3_oformat = {
218 de6d9b64 Fabrice Bellard
    "ac3",
219
    "raw ac3",
220
    "audio/x-ac3", 
221
    "ac3",
222 c9a65ca8 Fabrice Bellard
    0,
223 de6d9b64 Fabrice Bellard
    CODEC_ID_AC3,
224
    0,
225
    raw_write_header,
226
    raw_write_packet,
227
    raw_write_trailer,
228
};
229
230 c9a65ca8 Fabrice Bellard
AVOutputFormat h263_oformat = {
231 de6d9b64 Fabrice Bellard
    "h263",
232
    "raw h263",
233
    "video/x-h263",
234
    "h263",
235
    0,
236 c9a65ca8 Fabrice Bellard
    0,
237 de6d9b64 Fabrice Bellard
    CODEC_ID_H263,
238
    raw_write_header,
239
    raw_write_packet,
240
    raw_write_trailer,
241 c9a65ca8 Fabrice Bellard
};
242
243 7866eeff Michael Niedermayer
AVInputFormat m4v_iformat = {
244
    "m4v",
245
    "raw MPEG4 video format",
246
    0,
247 2f0f5b20 Michael Niedermayer
    NULL /*mpegvideo_probe*/,
248 7866eeff Michael Niedermayer
    video_read_header,
249
    raw_read_packet,
250
    raw_read_close,
251 bb76a117 Måns Rullgård
    .extensions = "m4v", //FIXME remove after writing mpeg4_probe
252
    .value = CODEC_ID_MPEG4,
253 7866eeff Michael Niedermayer
};
254
255 89b3d7c9 Max Krasnyansky
AVOutputFormat m4v_oformat = {
256
    "m4v",
257
    "raw MPEG4 video format",
258
    NULL,
259
    "m4v",
260
    0,
261
    CODEC_ID_NONE,
262
    CODEC_ID_MPEG4,
263
    raw_write_header,
264
    raw_write_packet,
265
    raw_write_trailer,
266
};
267
268 c9a65ca8 Fabrice Bellard
AVInputFormat mpegvideo_iformat = {
269
    "mpegvideo",
270
    "MPEG video",
271
    0,
272
    mpegvideo_probe,
273 de6d9b64 Fabrice Bellard
    video_read_header,
274
    raw_read_packet,
275
    raw_read_close,
276 bb76a117 Måns Rullgård
    .value = CODEC_ID_MPEG1VIDEO,
277 de6d9b64 Fabrice Bellard
};
278
279 c9a65ca8 Fabrice Bellard
AVOutputFormat mpeg1video_oformat = {
280
    "mpeg1video",
281 de6d9b64 Fabrice Bellard
    "MPEG video",
282
    "video/x-mpeg",
283
    "mpg,mpeg",
284
    0,
285 c9a65ca8 Fabrice Bellard
    0,
286 de6d9b64 Fabrice Bellard
    CODEC_ID_MPEG1VIDEO,
287
    raw_write_header,
288
    raw_write_packet,
289
    raw_write_trailer,
290
};
291
292 c9a65ca8 Fabrice Bellard
AVInputFormat mjpeg_iformat = {
293 27e084bd Fabrice Bellard
    "mjpeg",
294
    "MJPEG video",
295
    0,
296 c9a65ca8 Fabrice Bellard
    NULL,
297 27e084bd Fabrice Bellard
    video_read_header,
298
    raw_read_packet,
299
    raw_read_close,
300 bb76a117 Måns Rullgård
    .extensions = "mjpg,mjpeg",
301
    .value = CODEC_ID_MJPEG,
302 27e084bd Fabrice Bellard
};
303
304 c9a65ca8 Fabrice Bellard
AVOutputFormat mjpeg_oformat = {
305
    "mjpeg",
306
    "MJPEG video",
307
    "video/x-mjpeg",
308
    "mjpg,mjpeg",
309 5ed8fafc Fabrice Bellard
    0,
310
    0,
311 c9a65ca8 Fabrice Bellard
    CODEC_ID_MJPEG,
312 5ed8fafc Fabrice Bellard
    raw_write_header,
313
    raw_write_packet,
314
    raw_write_trailer,
315
};
316
317 c9a65ca8 Fabrice Bellard
/* pcm formats */
318 5ed8fafc Fabrice Bellard
319 c9a65ca8 Fabrice Bellard
#define PCMDEF(name, long_name, ext, codec) \
320
AVInputFormat pcm_ ## name ## _iformat = {\
321
    #name,\
322
    long_name,\
323
    0,\
324
    NULL,\
325
    raw_read_header,\
326
    raw_read_packet,\
327
    raw_read_close,\
328 bb76a117 Måns Rullgård
    .extensions = ext,\
329
    .value = codec,\
330 c9a65ca8 Fabrice Bellard
};\
331
\
332
AVOutputFormat pcm_ ## name ## _oformat = {\
333
    #name,\
334
    long_name,\
335
    NULL,\
336
    ext,\
337
    0,\
338
    codec,\
339
    0,\
340
    raw_write_header,\
341
    raw_write_packet,\
342
    raw_write_trailer,\
343 5ed8fafc Fabrice Bellard
};
344
345
#ifdef WORDS_BIGENDIAN
346 c9a65ca8 Fabrice Bellard
#define BE_DEF(s) s
347
#define LE_DEF(s) NULL
348 5ed8fafc Fabrice Bellard
#else
349 c9a65ca8 Fabrice Bellard
#define BE_DEF(s) NULL
350
#define LE_DEF(s) s
351 5ed8fafc Fabrice Bellard
#endif
352
353
354 c9a65ca8 Fabrice Bellard
PCMDEF(s16le, "pcm signed 16 bit little endian format", 
355
       LE_DEF("sw"), CODEC_ID_PCM_S16LE)
356 5ed8fafc Fabrice Bellard
357 c9a65ca8 Fabrice Bellard
PCMDEF(s16be, "pcm signed 16 bit big endian format", 
358
       BE_DEF("sw"), CODEC_ID_PCM_S16BE)
359 5ed8fafc Fabrice Bellard
360 c9a65ca8 Fabrice Bellard
PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
361
       LE_DEF("uw"), CODEC_ID_PCM_U16LE)
362 5ed8fafc Fabrice Bellard
363 c9a65ca8 Fabrice Bellard
PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
364
       BE_DEF("uw"), CODEC_ID_PCM_U16BE)
365 5ed8fafc Fabrice Bellard
366 c9a65ca8 Fabrice Bellard
PCMDEF(s8, "pcm signed 8 bit format", 
367
       "sb", CODEC_ID_PCM_S8)
368 5ed8fafc Fabrice Bellard
369 c9a65ca8 Fabrice Bellard
PCMDEF(u8, "pcm unsigned 8 bit format", 
370
       "ub", CODEC_ID_PCM_U8)
371 5ed8fafc Fabrice Bellard
372 c9a65ca8 Fabrice Bellard
PCMDEF(mulaw, "pcm mu law format", 
373
       "ul", CODEC_ID_PCM_MULAW)
374 de6d9b64 Fabrice Bellard
375 c9a65ca8 Fabrice Bellard
PCMDEF(alaw, "pcm A law format", 
376
       "al", CODEC_ID_PCM_ALAW)
377 de6d9b64 Fabrice Bellard
378
int rawvideo_read_packet(AVFormatContext *s,
379
                         AVPacket *pkt)
380
{
381
    int packet_size, ret, width, height;
382
    AVStream *st = s->streams[0];
383
384
    width = st->codec.width;
385
    height = st->codec.height;
386
387
    switch(st->codec.pix_fmt) {
388
    case PIX_FMT_YUV420P:
389
        packet_size = (width * height * 3) / 2;
390
        break;
391
    case PIX_FMT_YUV422:
392
        packet_size = (width * height * 2);
393
        break;
394
    case PIX_FMT_BGR24:
395
    case PIX_FMT_RGB24:
396
        packet_size = (width * height * 3);
397
        break;
398
    default:
399 42343f7e Philip Gladstone
        av_abort();
400 de6d9b64 Fabrice Bellard
        break;
401
    }
402
403
    if (av_new_packet(pkt, packet_size) < 0)
404
        return -EIO;
405
406
    pkt->stream_index = 0;
407 c2c2cd2d Michael Niedermayer
#if 0
408 de6d9b64 Fabrice Bellard
    /* bypass buffered I/O */
409
    ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
410 c2c2cd2d Michael Niedermayer
#else
411
    ret = get_buffer(&s->pb, pkt->data, pkt->size);
412
#endif
413 de6d9b64 Fabrice Bellard
    if (ret != pkt->size) {
414
        av_free_packet(pkt);
415
        return -EIO;
416
    } else {
417
        return 0;
418
    }
419
}
420
421 c9a65ca8 Fabrice Bellard
AVInputFormat rawvideo_iformat = {
422
    "rawvideo",
423
    "raw video format",
424
    0,
425
    NULL,
426
    raw_read_header,
427
    rawvideo_read_packet,
428
    raw_read_close,
429 bb76a117 Måns Rullgård
    .extensions = "yuv",
430
    .value = CODEC_ID_RAWVIDEO,
431 c9a65ca8 Fabrice Bellard
};
432
433
AVOutputFormat rawvideo_oformat = {
434 de6d9b64 Fabrice Bellard
    "rawvideo",
435
    "raw video format",
436
    NULL,
437
    "yuv",
438 c9a65ca8 Fabrice Bellard
    0,
439 de6d9b64 Fabrice Bellard
    CODEC_ID_NONE,
440
    CODEC_ID_RAWVIDEO,
441
    raw_write_header,
442
    raw_write_packet,
443
    raw_write_trailer,
444
};
445 c9a65ca8 Fabrice Bellard
446 87bdd3e5 Fabrice Bellard
static int null_write_packet(struct AVFormatContext *s, 
447
                             int stream_index,
448
                             unsigned char *buf, int size, int force_pts)
449
{
450
    return 0;
451
}
452
453
AVOutputFormat null_oformat = {
454
    "null",
455
    "null video format",
456
    NULL,
457
    NULL,
458
    0,
459
#ifdef WORDS_BIGENDIAN
460
    CODEC_ID_PCM_S16BE,
461
#else
462
    CODEC_ID_PCM_S16LE,
463
#endif
464
    CODEC_ID_RAWVIDEO,
465
    raw_write_header,
466
    null_write_packet,
467
    raw_write_trailer,
468
    flags: AVFMT_NOFILE | AVFMT_RAWPICTURE,
469
};
470
471 c9a65ca8 Fabrice Bellard
int raw_init(void)
472
{
473
    av_register_input_format(&mp3_iformat);
474
    av_register_output_format(&mp2_oformat);
475
    
476
    av_register_input_format(&ac3_iformat);
477
    av_register_output_format(&ac3_oformat);
478
479
    av_register_output_format(&h263_oformat);
480 89b3d7c9 Max Krasnyansky
    
481 7866eeff Michael Niedermayer
    av_register_input_format(&m4v_iformat);
482 89b3d7c9 Max Krasnyansky
    av_register_output_format(&m4v_oformat);
483 c9a65ca8 Fabrice Bellard
484
    av_register_input_format(&mpegvideo_iformat);
485
    av_register_output_format(&mpeg1video_oformat);
486
487
    av_register_input_format(&mjpeg_iformat);
488
    av_register_output_format(&mjpeg_oformat);
489
490
    av_register_input_format(&pcm_s16le_iformat);
491
    av_register_output_format(&pcm_s16le_oformat);
492
    av_register_input_format(&pcm_s16be_iformat);
493
    av_register_output_format(&pcm_s16be_oformat);
494
    av_register_input_format(&pcm_u16le_iformat);
495
    av_register_output_format(&pcm_u16le_oformat);
496
    av_register_input_format(&pcm_u16be_iformat);
497
    av_register_output_format(&pcm_u16be_oformat);
498
    av_register_input_format(&pcm_s8_iformat);
499
    av_register_output_format(&pcm_s8_oformat);
500
    av_register_input_format(&pcm_u8_iformat);
501
    av_register_output_format(&pcm_u8_oformat);
502
    av_register_input_format(&pcm_mulaw_iformat);
503
    av_register_output_format(&pcm_mulaw_oformat);
504
    av_register_input_format(&pcm_alaw_iformat);
505
    av_register_output_format(&pcm_alaw_oformat);
506
507
    av_register_input_format(&rawvideo_iformat);
508
    av_register_output_format(&rawvideo_oformat);
509 87bdd3e5 Fabrice Bellard
510
    av_register_output_format(&null_oformat);
511 c9a65ca8 Fabrice Bellard
    return 0;
512
}