Tizen 2.1 base
[platform/upstream/ffmpeg.git] / libavcodec / yuv4dec.c
1 /*
2  * libquicktime yuv4 decoder
3  *
4  * Copyright (c) 2011 Carl Eugen Hoyos
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "avcodec.h"
24
25 static av_cold int yuv4_decode_init(AVCodecContext *avctx)
26 {
27     avctx->pix_fmt = PIX_FMT_YUV420P;
28
29     avctx->coded_frame = avcodec_alloc_frame();
30
31     if (!avctx->coded_frame) {
32         av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
33         return AVERROR(ENOMEM);
34     }
35
36     return 0;
37 }
38
39 static int yuv4_decode_frame(AVCodecContext *avctx, void *data,
40                              int *data_size, AVPacket *avpkt)
41 {
42     AVFrame *pic = avctx->coded_frame;
43     const uint8_t *src = avpkt->data;
44     uint8_t *y, *u, *v;
45     int i, j;
46
47     if (pic->data[0])
48         avctx->release_buffer(avctx, pic);
49
50     if (avpkt->size < 6 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1)) {
51         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
52         return AVERROR(EINVAL);
53     }
54
55     pic->reference = 0;
56
57     if (avctx->get_buffer(avctx, pic) < 0) {
58         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
59         return AVERROR(ENOMEM);
60     }
61
62     pic->key_frame = 1;
63     pic->pict_type = AV_PICTURE_TYPE_I;
64
65     y = pic->data[0];
66     u = pic->data[1];
67     v = pic->data[2];
68
69     for (i = 0; i < (avctx->height + 1) >> 1; i++) {
70         for (j = 0; j < (avctx->width + 1) >> 1; j++) {
71             u[j] = *src++ ^ 0x80;
72             v[j] = *src++ ^ 0x80;
73             y[                   2 * j    ] = *src++;
74             y[                   2 * j + 1] = *src++;
75             y[pic->linesize[0] + 2 * j    ] = *src++;
76             y[pic->linesize[0] + 2 * j + 1] = *src++;
77         }
78
79         y += 2 * pic->linesize[0];
80         u +=     pic->linesize[1];
81         v +=     pic->linesize[2];
82     }
83
84     *data_size = sizeof(AVFrame);
85     *(AVFrame *)data = *pic;
86
87     return avpkt->size;
88 }
89
90 static av_cold int yuv4_decode_close(AVCodecContext *avctx)
91 {
92     if (avctx->coded_frame->data[0])
93         avctx->release_buffer(avctx, avctx->coded_frame);
94
95     av_freep(&avctx->coded_frame);
96
97     return 0;
98 }
99
100 AVCodec ff_yuv4_decoder = {
101     .name         = "yuv4",
102     .type         = AVMEDIA_TYPE_VIDEO,
103     .id           = AV_CODEC_ID_YUV4,
104     .init         = yuv4_decode_init,
105     .decode       = yuv4_decode_frame,
106     .close        = yuv4_decode_close,
107     .capabilities = CODEC_CAP_DR1,
108     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
109 };