libavcodec/y41pdec.c
Go to the documentation of this file.
00001 /*
00002  * y41p decoder
00003  *
00004  * Copyright (c) 2012 Paul B Mahol
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 y41p_decode_init(AVCodecContext *avctx)
00026 {
00027     avctx->pix_fmt             = PIX_FMT_YUV411P;
00028     avctx->bits_per_raw_sample = 12;
00029 
00030     if (avctx->width & 7) {
00031         av_log(avctx, AV_LOG_WARNING, "y41p requires width to be divisible by 8.\n");
00032     }
00033 
00034     avctx->coded_frame = avcodec_alloc_frame();
00035     if (!avctx->coded_frame) {
00036         av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
00037         return AVERROR(ENOMEM);
00038     }
00039 
00040     return 0;
00041 }
00042 
00043 static int y41p_decode_frame(AVCodecContext *avctx, void *data,
00044                              int *data_size, AVPacket *avpkt)
00045 {
00046     AVFrame *pic = avctx->coded_frame;
00047     uint8_t *src = avpkt->data;
00048     uint8_t *y, *u, *v;
00049     int i, j;
00050 
00051     if (pic->data[0])
00052         avctx->release_buffer(avctx, pic);
00053 
00054     if (avpkt->size < 1.5 * avctx->height * avctx->width) {
00055         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
00056         return AVERROR(EINVAL);
00057     }
00058 
00059     pic->reference = 0;
00060 
00061     if (avctx->get_buffer(avctx, pic) < 0) {
00062         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
00063         return AVERROR(ENOMEM);
00064     }
00065 
00066     pic->key_frame = 1;
00067     pic->pict_type = AV_PICTURE_TYPE_I;
00068 
00069     for (i = avctx->height - 1; i >= 0 ; i--) {
00070         y = &pic->data[0][i * pic->linesize[0]];
00071         u = &pic->data[1][i * pic->linesize[1]];
00072         v = &pic->data[2][i * pic->linesize[2]];
00073         for (j = 0; j < avctx->width; j += 8) {
00074             *(u++) = *src++;
00075             *(y++) = *src++;
00076             *(v++) = *src++;
00077             *(y++) = *src++;
00078 
00079             *(u++) = *src++;
00080             *(y++) = *src++;
00081             *(v++) = *src++;
00082             *(y++) = *src++;
00083 
00084             *(y++) = *src++;
00085             *(y++) = *src++;
00086             *(y++) = *src++;
00087             *(y++) = *src++;
00088         }
00089     }
00090 
00091     *data_size = sizeof(AVFrame);
00092     *(AVFrame *)data = *pic;
00093 
00094     return avpkt->size;
00095 }
00096 
00097 static av_cold int y41p_decode_close(AVCodecContext *avctx)
00098 {
00099     if (avctx->coded_frame->data[0])
00100         avctx->release_buffer(avctx, avctx->coded_frame);
00101 
00102     av_freep(&avctx->coded_frame);
00103 
00104     return 0;
00105 }
00106 
00107 AVCodec ff_y41p_decoder = {
00108     .name         = "y41p",
00109     .type         = AVMEDIA_TYPE_VIDEO,
00110     .id           = CODEC_ID_Y41P,
00111     .init         = y41p_decode_init,
00112     .decode       = y41p_decode_frame,
00113     .close        = y41p_decode_close,
00114     .capabilities = CODEC_CAP_DR1,
00115     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed YUV 4:1:1 12-bit"),
00116 };