Statistics
| Branch: | Revision:

ffmpeg / libav / raw.c @ 87bdd3e5

History | View | Annotate | Download (11.5 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
/* 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
                     unsigned char *buf, int size, int force_pts)
30
{
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
    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.width = ap->width;
68
            st->codec.height = ap->height;
69
            break;
70
        default:
71
            return -1;
72
        }
73
    } else {
74
        return -1;
75
    }
76
    return 0;
77
}
78

    
79
#define RAW_PACKET_SIZE 1024
80

    
81
int raw_read_packet(AVFormatContext *s,
82
                    AVPacket *pkt)
83
{
84
    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

    
92
    if (av_new_packet(pkt, size) < 0)
93
        return -EIO;
94

    
95
    pkt->stream_index = 0;
96
    ret = get_buffer(&s->pb, pkt->data, size);
97
    if (ret <= 0) {
98
        av_free_packet(pkt);
99
        return -EIO;
100
    }
101
    /* note: we need to modify the packet size here to handle the last
102
       packet */
103
    pkt->size = ret;
104
    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
    st = av_new_stream(s, 0);
119
    if (!st)
120
        return AVERROR_NOMEM;
121

    
122
    st->codec.codec_type = CODEC_TYPE_AUDIO;
123
    st->codec.codec_id = CODEC_ID_MP2;
124
    /* the parameters will be extracted from the compressed bitstream */
125
    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
    st = av_new_stream(s, 0);
135
    if (!st)
136
        return AVERROR_NOMEM;
137

    
138
    st->codec.codec_type = CODEC_TYPE_VIDEO;
139
    st->codec.codec_id = s->iformat->value;
140
    /* for mjpeg, specify frame rate */
141
    /* 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
        if (ap) {
144
            st->codec.frame_rate = ap->frame_rate;
145
        } else {
146
            st->codec.frame_rate = 25 * FRAME_RATE_BASE;
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, 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
    "MPEG audio",
183
    0,
184
    NULL,
185
    mp3_read_header,
186
    raw_read_packet,
187
    raw_read_close,
188
    .extensions = "mp2,mp3", /* XXX: use probe */
189
};
190

    
191
AVOutputFormat mp2_oformat = {
192
    "mp2",
193
    "MPEG audio layer 2",
194
    "audio/x-mpeg",
195
    "mp2,mp3",
196
    0,
197
    CODEC_ID_MP2,
198
    0,
199
    raw_write_header,
200
    raw_write_packet,
201
    raw_write_trailer,
202
};
203

    
204

    
205
AVInputFormat ac3_iformat = {
206
    "ac3",
207
    "raw ac3",
208
    0,
209
    NULL,
210
    raw_read_header,
211
    raw_read_packet,
212
    raw_read_close,
213
    .extensions = "ac3",
214
    .value = CODEC_ID_AC3,
215
};
216

    
217
AVOutputFormat ac3_oformat = {
218
    "ac3",
219
    "raw ac3",
220
    "audio/x-ac3", 
221
    "ac3",
222
    0,
223
    CODEC_ID_AC3,
224
    0,
225
    raw_write_header,
226
    raw_write_packet,
227
    raw_write_trailer,
228
};
229

    
230
AVOutputFormat h263_oformat = {
231
    "h263",
232
    "raw h263",
233
    "video/x-h263",
234
    "h263",
235
    0,
236
    0,
237
    CODEC_ID_H263,
238
    raw_write_header,
239
    raw_write_packet,
240
    raw_write_trailer,
241
};
242

    
243
AVInputFormat m4v_iformat = {
244
    "m4v",
245
    "raw MPEG4 video format",
246
    0,
247
    NULL /*mpegvideo_probe*/,
248
    video_read_header,
249
    raw_read_packet,
250
    raw_read_close,
251
    .extensions = "m4v", //FIXME remove after writing mpeg4_probe
252
    .value = CODEC_ID_MPEG4,
253
};
254

    
255
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
AVInputFormat mpegvideo_iformat = {
269
    "mpegvideo",
270
    "MPEG video",
271
    0,
272
    mpegvideo_probe,
273
    video_read_header,
274
    raw_read_packet,
275
    raw_read_close,
276
    .value = CODEC_ID_MPEG1VIDEO,
277
};
278

    
279
AVOutputFormat mpeg1video_oformat = {
280
    "mpeg1video",
281
    "MPEG video",
282
    "video/x-mpeg",
283
    "mpg,mpeg",
284
    0,
285
    0,
286
    CODEC_ID_MPEG1VIDEO,
287
    raw_write_header,
288
    raw_write_packet,
289
    raw_write_trailer,
290
};
291

    
292
AVInputFormat mjpeg_iformat = {
293
    "mjpeg",
294
    "MJPEG video",
295
    0,
296
    NULL,
297
    video_read_header,
298
    raw_read_packet,
299
    raw_read_close,
300
    .extensions = "mjpg,mjpeg",
301
    .value = CODEC_ID_MJPEG,
302
};
303

    
304
AVOutputFormat mjpeg_oformat = {
305
    "mjpeg",
306
    "MJPEG video",
307
    "video/x-mjpeg",
308
    "mjpg,mjpeg",
309
    0,
310
    0,
311
    CODEC_ID_MJPEG,
312
    raw_write_header,
313
    raw_write_packet,
314
    raw_write_trailer,
315
};
316

    
317
/* pcm formats */
318

    
319
#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
    .extensions = ext,\
329
    .value = codec,\
330
};\
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
};
344

    
345
#ifdef WORDS_BIGENDIAN
346
#define BE_DEF(s) s
347
#define LE_DEF(s) NULL
348
#else
349
#define BE_DEF(s) NULL
350
#define LE_DEF(s) s
351
#endif
352

    
353

    
354
PCMDEF(s16le, "pcm signed 16 bit little endian format", 
355
       LE_DEF("sw"), CODEC_ID_PCM_S16LE)
356

    
357
PCMDEF(s16be, "pcm signed 16 bit big endian format", 
358
       BE_DEF("sw"), CODEC_ID_PCM_S16BE)
359

    
360
PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
361
       LE_DEF("uw"), CODEC_ID_PCM_U16LE)
362

    
363
PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
364
       BE_DEF("uw"), CODEC_ID_PCM_U16BE)
365

    
366
PCMDEF(s8, "pcm signed 8 bit format", 
367
       "sb", CODEC_ID_PCM_S8)
368

    
369
PCMDEF(u8, "pcm unsigned 8 bit format", 
370
       "ub", CODEC_ID_PCM_U8)
371

    
372
PCMDEF(mulaw, "pcm mu law format", 
373
       "ul", CODEC_ID_PCM_MULAW)
374

    
375
PCMDEF(alaw, "pcm A law format", 
376
       "al", CODEC_ID_PCM_ALAW)
377

    
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
        av_abort();
400
        break;
401
    }
402

    
403
    if (av_new_packet(pkt, packet_size) < 0)
404
        return -EIO;
405

    
406
    pkt->stream_index = 0;
407
#if 0
408
    /* bypass buffered I/O */
409
    ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
410
#else
411
    ret = get_buffer(&s->pb, pkt->data, pkt->size);
412
#endif
413
    if (ret != pkt->size) {
414
        av_free_packet(pkt);
415
        return -EIO;
416
    } else {
417
        return 0;
418
    }
419
}
420

    
421
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
    .extensions = "yuv",
430
    .value = CODEC_ID_RAWVIDEO,
431
};
432

    
433
AVOutputFormat rawvideo_oformat = {
434
    "rawvideo",
435
    "raw video format",
436
    NULL,
437
    "yuv",
438
    0,
439
    CODEC_ID_NONE,
440
    CODEC_ID_RAWVIDEO,
441
    raw_write_header,
442
    raw_write_packet,
443
    raw_write_trailer,
444
};
445

    
446
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
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
    
481
    av_register_input_format(&m4v_iformat);
482
    av_register_output_format(&m4v_oformat);
483

    
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

    
510
    av_register_output_format(&null_oformat);
511
    return 0;
512
}