Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ ce992c69

History | View | Annotate | Download (11.2 KB)

1
/*
2
 * utils for libavcodec
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 "avcodec.h"
20
#include "dsputil.h"
21
#include "mpegvideo.h"
22

    
23
void *av_mallocz(int size)
24
{
25
    void *ptr;
26
    ptr = av_malloc(size);
27
    if (!ptr)
28
        return NULL;
29
    memset(ptr, 0, size);
30
    return ptr;
31
}
32

    
33
/* cannot call it directly because of 'void **' casting is not automatic */
34
void __av_freep(void **ptr)
35
{
36
    av_free(*ptr);
37
    *ptr = NULL;
38
}
39

    
40
/* encoder management */
41
AVCodec *first_avcodec;
42

    
43
void register_avcodec(AVCodec *format)
44
{
45
    AVCodec **p;
46
    p = &first_avcodec;
47
    while (*p != NULL) p = &(*p)->next;
48
    *p = format;
49
    format->next = NULL;
50
}
51

    
52
int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
53
{
54
    int ret;
55

    
56
    avctx->codec = codec;
57
    avctx->frame_number = 0;
58
    if (codec->priv_data_size > 0) {
59
        avctx->priv_data = av_mallocz(codec->priv_data_size);
60
        if (!avctx->priv_data) 
61
            return -ENOMEM;
62
    } else {
63
        avctx->priv_data = NULL;
64
    }
65
    ret = avctx->codec->init(avctx);
66
    if (ret < 0) {
67
        av_freep(&avctx->priv_data);
68
        return ret;
69
    }
70
    return 0;
71
}
72

    
73
int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size, 
74
                         const short *samples)
75
{
76
    int ret;
77

    
78
    ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
79
    avctx->frame_number++;
80
    return ret;
81
}
82

    
83
int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size, 
84
                         const AVPicture *pict)
85
{
86
    int ret;
87

    
88
    ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
89
    avctx->frame_number++;
90
    return ret;
91
}
92

    
93
/* decode a frame. return -1 if error, otherwise return the number of
94
   bytes used. If no frame could be decompressed, *got_picture_ptr is
95
   zero. Otherwise, it is non zero */
96
int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture, 
97
                         int *got_picture_ptr,
98
                         UINT8 *buf, int buf_size)
99
{
100
    int ret;
101

    
102
    ret = avctx->codec->decode(avctx, picture, got_picture_ptr, 
103
                               buf, buf_size);
104
    if (*got_picture_ptr)                           
105
        avctx->frame_number++;
106
    return ret;
107
}
108

    
109
/* decode an audio frame. return -1 if error, otherwise return the
110
   *number of bytes used. If no frame could be decompressed,
111
   *frame_size_ptr is zero. Otherwise, it is the decompressed frame
112
   *size in BYTES. */
113
int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples, 
114
                         int *frame_size_ptr,
115
                         UINT8 *buf, int buf_size)
116
{
117
    int ret;
118

    
119
    ret = avctx->codec->decode(avctx, samples, frame_size_ptr, 
120
                               buf, buf_size);
121
    avctx->frame_number++;
122
    return ret;
123
}
124

    
125
int avcodec_close(AVCodecContext *avctx)
126
{
127
    if (avctx->codec->close)
128
        avctx->codec->close(avctx);
129
    av_freep(&avctx->priv_data);
130
    avctx->codec = NULL;
131
    return 0;
132
}
133

    
134
AVCodec *avcodec_find_encoder(enum CodecID id)
135
{
136
    AVCodec *p;
137
    p = first_avcodec;
138
    while (p) {
139
        if (p->encode != NULL && p->id == id)
140
            return p;
141
        p = p->next;
142
    }
143
    return NULL;
144
}
145

    
146
AVCodec *avcodec_find_encoder_by_name(const char *name)
147
{
148
    AVCodec *p;
149
    p = first_avcodec;
150
    while (p) {
151
        if (p->encode != NULL && strcmp(name,p->name) == 0)
152
            return p;
153
        p = p->next;
154
    }
155
    return NULL;
156
}
157

    
158
AVCodec *avcodec_find_decoder(enum CodecID id)
159
{
160
    AVCodec *p;
161
    p = first_avcodec;
162
    while (p) {
163
        if (p->decode != NULL && p->id == id)
164
            return p;
165
        p = p->next;
166
    }
167
    return NULL;
168
}
169

    
170
AVCodec *avcodec_find_decoder_by_name(const char *name)
171
{
172
    AVCodec *p;
173
    p = first_avcodec;
174
    while (p) {
175
        if (p->decode != NULL && strcmp(name,p->name) == 0)
176
            return p;
177
        p = p->next;
178
    }
179
    return NULL;
180
}
181

    
182
AVCodec *avcodec_find(enum CodecID id)
183
{
184
    AVCodec *p;
185
    p = first_avcodec;
186
    while (p) {
187
        if (p->id == id)
188
            return p;
189
        p = p->next;
190
    }
191
    return NULL;
192
}
193

    
194
const char *pix_fmt_str[] = {
195
    "??",
196
    "yuv420p",
197
    "yuv422",
198
    "rgb24",
199
    "bgr24",
200
    "yuv422p",
201
    "yuv444p",
202
};
203
    
204
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
205
{
206
    const char *codec_name;
207
    AVCodec *p;
208
    char buf1[32];
209
    char channels_str[100];
210
    int bitrate;
211

    
212
    if (encode)
213
        p = avcodec_find_encoder(enc->codec_id);
214
    else
215
        p = avcodec_find_decoder(enc->codec_id);
216

    
217
    if (p) {
218
        codec_name = p->name;
219
    } else if (enc->codec_name[0] != '\0') {
220
        codec_name = enc->codec_name;
221
    } else {
222
        /* output avi tags */
223
        if (enc->codec_type == CODEC_TYPE_VIDEO) {
224
            snprintf(buf1, sizeof(buf1), "%c%c%c%c", 
225
                     enc->codec_tag & 0xff,
226
                     (enc->codec_tag >> 8) & 0xff,
227
                     (enc->codec_tag >> 16) & 0xff,
228
                     (enc->codec_tag >> 24) & 0xff);
229
        } else {
230
            snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
231
        }
232
        codec_name = buf1;
233
    }
234

    
235
    switch(enc->codec_type) {
236
    case CODEC_TYPE_VIDEO:
237
        snprintf(buf, buf_size,
238
                 "Video: %s%s",
239
                 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
240
        if (enc->codec_id == CODEC_ID_RAWVIDEO) {
241
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
242
                     ", %s",
243
                     pix_fmt_str[enc->pix_fmt]);
244
        }
245
        if (enc->width) {
246
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
247
                     ", %dx%d, %0.2f fps",
248
                     enc->width, enc->height, 
249
                     (float)enc->frame_rate / FRAME_RATE_BASE);
250
        }
251
        snprintf(buf + strlen(buf), buf_size - strlen(buf),
252
                ", q=%d-%d", enc->qmin, enc->qmax);
253

    
254
        bitrate = enc->bit_rate;
255
        break;
256
    case CODEC_TYPE_AUDIO:
257
        snprintf(buf, buf_size,
258
                 "Audio: %s",
259
                 codec_name);
260
        switch (enc->channels) {
261
            case 1:
262
                strcpy(channels_str, "mono");
263
                break;
264
            case 2:
265
                strcpy(channels_str, "stereo");
266
                break;
267
            case 6:
268
                strcpy(channels_str, "5:1");
269
                break;
270
            default:
271
                sprintf(channels_str, "%d channels", enc->channels);
272
                break;
273
        }
274
        if (enc->sample_rate) {
275
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
276
                     ", %d Hz, %s",
277
                     enc->sample_rate,
278
                     channels_str);
279
        }
280
        
281
        /* for PCM codecs, compute bitrate directly */
282
        switch(enc->codec_id) {
283
        case CODEC_ID_PCM_S16LE:
284
        case CODEC_ID_PCM_S16BE:
285
        case CODEC_ID_PCM_U16LE:
286
        case CODEC_ID_PCM_U16BE:
287
            bitrate = enc->sample_rate * enc->channels * 16;
288
            break;
289
        case CODEC_ID_PCM_S8:
290
        case CODEC_ID_PCM_U8:
291
        case CODEC_ID_PCM_ALAW:
292
        case CODEC_ID_PCM_MULAW:
293
            bitrate = enc->sample_rate * enc->channels * 8;
294
            break;
295
        default:
296
            bitrate = enc->bit_rate;
297
            break;
298
        }
299
        break;
300
    default:
301
        abort();
302
    }
303
    if (bitrate != 0) {
304
        snprintf(buf + strlen(buf), buf_size - strlen(buf), 
305
                 ", %d kb/s", bitrate / 1000);
306
    }
307
}
308

    
309
/* Picture field are filled with 'ptr' addresses */
310
void avpicture_fill(AVPicture *picture, UINT8 *ptr,
311
                    int pix_fmt, int width, int height)
312
{
313
    int size;
314

    
315
    size = width * height;
316
    switch(pix_fmt) {
317
    case PIX_FMT_YUV420P:
318
        picture->data[0] = ptr;
319
        picture->data[1] = picture->data[0] + size;
320
        picture->data[2] = picture->data[1] + size / 4;
321
        picture->linesize[0] = width;
322
        picture->linesize[1] = width / 2;
323
        picture->linesize[2] = width / 2;
324
        break;
325
    case PIX_FMT_YUV422P:
326
        picture->data[0] = ptr;
327
        picture->data[1] = picture->data[0] + size;
328
        picture->data[2] = picture->data[1] + size / 2;
329
        picture->linesize[0] = width;
330
        picture->linesize[1] = width / 2;
331
        picture->linesize[2] = width / 2;
332
        break;
333
    case PIX_FMT_YUV444P:
334
        picture->data[0] = ptr;
335
        picture->data[1] = picture->data[0] + size;
336
        picture->data[2] = picture->data[1] + size;
337
        picture->linesize[0] = width;
338
        picture->linesize[1] = width;
339
        picture->linesize[2] = width;
340
        break;
341
    case PIX_FMT_RGB24:
342
    case PIX_FMT_BGR24:
343
        picture->data[0] = ptr;
344
        picture->data[1] = NULL;
345
        picture->data[2] = NULL;
346
        picture->linesize[0] = width * 3;
347
        break;
348
    case PIX_FMT_YUV422:
349
        picture->data[0] = ptr;
350
        picture->data[1] = NULL;
351
        picture->data[2] = NULL;
352
        picture->linesize[0] = width * 2;
353
        break;
354
    default:
355
        picture->data[0] = NULL;
356
        picture->data[1] = NULL;
357
        picture->data[2] = NULL;
358
        break;
359
    }
360
}
361

    
362
int avpicture_get_size(int pix_fmt, int width, int height)
363
{
364
    int size;
365

    
366
    size = width * height;
367
    switch(pix_fmt) {
368
    case PIX_FMT_YUV420P:
369
        size = (size * 3) / 2;
370
        break;
371
    case PIX_FMT_YUV422P:
372
        size = (size * 2);
373
        break;
374
    case PIX_FMT_YUV444P:
375
        size = (size * 3);
376
        break;
377
    case PIX_FMT_RGB24:
378
    case PIX_FMT_BGR24:
379
        size = (size * 3);
380
        break;
381
    case PIX_FMT_YUV422:
382
        size = (size * 2);
383
        break;
384
    default:
385
        size = -1;
386
        break;
387
    }
388
    return size;
389
}
390

    
391
unsigned avcodec_version( void )
392
{
393
  return LIBAVCODEC_VERSION_INT;
394
}
395

    
396
unsigned avcodec_build( void )
397
{
398
  return LIBAVCODEC_BUILD;
399
}
400

    
401
/* must be called before any other functions */
402
void avcodec_init(void)
403
{
404
    static int inited = 0;
405

    
406
    if (inited != 0)
407
        return;
408
    inited = 1;
409

    
410
    dsputil_init();
411
}
412

    
413
/* this should be called after seeking and before trying to decode the next frame */
414
void avcodec_flush_buffers(AVCodecContext *avctx)
415
{
416
    MpegEncContext *s = avctx->priv_data;
417
    s->num_available_buffers=0;
418
}
419

    
420

    
421
static int raw_encode_init(AVCodecContext *s)
422
{
423
    return 0;
424
}
425

    
426
static int raw_decode_frame(AVCodecContext *avctx,
427
                            void *data, int *data_size,
428
                            UINT8 *buf, int buf_size)
429
{
430
    return -1;
431
}
432

    
433
static int raw_encode_frame(AVCodecContext *avctx,
434
                            unsigned char *frame, int buf_size, void *data)
435
{
436
    return -1;
437
}
438

    
439
AVCodec rawvideo_codec = {
440
    "rawvideo",
441
    CODEC_TYPE_VIDEO,
442
    CODEC_ID_RAWVIDEO,
443
    0,
444
    raw_encode_init,
445
    raw_encode_frame,
446
    NULL,
447
    raw_decode_frame,
448
};