libavcodec/yuv4dec.c
Go to the documentation of this file.
00001 /*
00002  * libquicktime yuv4 decoder
00003  *
00004  * Copyright (c) 2011 Carl Eugen Hoyos
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "avcodec.h"
00024 
00025 static av_cold int yuv4_decode_init(AVCodecContext *avctx)
00026 {
00027     avctx->pix_fmt = PIX_FMT_YUV420P;
00028 
00029     avctx->coded_frame = avcodec_alloc_frame();
00030 
00031     if (!avctx->coded_frame) {
00032         av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
00033         return AVERROR(ENOMEM);
00034     }
00035 
00036     return 0;
00037 }
00038 
00039 static int yuv4_decode_frame(AVCodecContext *avctx, void *data,
00040                              int *data_size, AVPacket *avpkt)
00041 {
00042     AVFrame *pic = avctx->coded_frame;
00043     const uint8_t *src = avpkt->data;
00044     uint8_t *y, *u, *v;
00045     int i, j;
00046 
00047     if (pic->data[0])
00048         avctx->release_buffer(avctx, pic);
00049 
00050     if (avpkt->size < 6 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1)) {
00051         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
00052         return AVERROR(EINVAL);
00053     }
00054 
00055     pic->reference = 0;
00056 
00057     if (avctx->get_buffer(avctx, pic) < 0) {
00058         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
00059         return AVERROR(ENOMEM);
00060     }
00061 
00062     pic->key_frame = 1;
00063     pic->pict_type = AV_PICTURE_TYPE_I;
00064 
00065     y = pic->data[0];
00066     u = pic->data[1];
00067     v = pic->data[2];
00068 
00069     for (i = 0; i < (avctx->height + 1) >> 1; i++) {
00070         for (j = 0; j < (avctx->width + 1) >> 1; j++) {
00071             u[j] = *src++ ^ 0x80;
00072             v[j] = *src++ ^ 0x80;
00073             y[                   2 * j    ] = *src++;
00074             y[                   2 * j + 1] = *src++;
00075             y[pic->linesize[0] + 2 * j    ] = *src++;
00076             y[pic->linesize[0] + 2 * j + 1] = *src++;
00077         }
00078 
00079         y += 2 * pic->linesize[0];
00080         u +=     pic->linesize[1];
00081         v +=     pic->linesize[2];
00082     }
00083 
00084     *data_size = sizeof(AVFrame);
00085     *(AVFrame *)data = *pic;
00086 
00087     return avpkt->size;
00088 }
00089 
00090 static av_cold int yuv4_decode_close(AVCodecContext *avctx)
00091 {
00092     if (avctx->coded_frame->data[0])
00093         avctx->release_buffer(avctx, avctx->coded_frame);
00094 
00095     av_freep(&avctx->coded_frame);
00096 
00097     return 0;
00098 }
00099 
00100 AVCodec ff_yuv4_decoder = {
00101     .name         = "yuv4",
00102     .type         = AVMEDIA_TYPE_VIDEO,
00103     .id           = CODEC_ID_YUV4,
00104     .init         = yuv4_decode_init,
00105     .decode       = yuv4_decode_frame,
00106     .close        = yuv4_decode_close,
00107     .capabilities = CODEC_CAP_DR1,
00108     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
00109 };