4 * Copyright (C) 2012 Carl Eugen Hoyos
6 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/intreadwrite.h"
27 static av_cold int zero12v_decode_init(AVCodecContext *avctx)
29 avctx->pix_fmt = PIX_FMT_YUV422P16;
30 avctx->bits_per_raw_sample = 10;
32 avctx->coded_frame = avcodec_alloc_frame();
33 if (!avctx->coded_frame)
34 return AVERROR(ENOMEM);
36 if (avctx->codec_tag == MKTAG('a', '1', '2', 'v'))
37 av_log_ask_for_sample(avctx, "Samples with actual transparency needed\n");
39 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
40 avctx->coded_frame->key_frame = 1;
44 static int zero12v_decode_frame(AVCodecContext *avctx, void *data,
45 int *got_frame, AVPacket *avpkt)
48 const int width = avctx->width;
49 AVFrame *pic = avctx->coded_frame;
51 const uint8_t *line_end, *src = avpkt->data;
52 int stride = avctx->width * 8 / 3;
55 avctx->release_buffer(avctx, pic);
58 av_log(avctx, AV_LOG_ERROR, "Width 1 not supported.\n");
59 return AVERROR_INVALIDDATA;
61 if (avpkt->size < avctx->height * stride) {
62 av_log(avctx, AV_LOG_ERROR, "Packet too small: %d instead of %d\n",
63 avpkt->size, avctx->height * stride);
64 return AVERROR_INVALIDDATA;
68 if ((ret = ff_get_buffer(avctx, pic)) < 0)
71 y = (uint16_t *)pic->data[0];
72 u = (uint16_t *)pic->data[1];
73 v = (uint16_t *)pic->data[2];
74 line_end = avpkt->data + stride;
76 while (line++ < avctx->height) {
78 uint32_t t = AV_RL32(src);
80 *u++ = t << 6 & 0xFFC0;
81 *y++ = t >> 4 & 0xFFC0;
82 *v++ = t >> 14 & 0xFFC0;
84 if (src >= line_end - 1) {
88 y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
89 u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
90 v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
96 *y++ = t << 6 & 0xFFC0;
97 *u++ = t >> 4 & 0xFFC0;
98 *y++ = t >> 14 & 0xFFC0;
99 if (src >= line_end - 2) {
105 y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
106 u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
107 v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
113 *v++ = t << 6 & 0xFFC0;
114 *y++ = t >> 4 & 0xFFC0;
115 *u++ = t >> 14 & 0xFFC0;
117 if (src >= line_end - 1) {
121 y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
122 u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
123 v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
129 *y++ = t << 6 & 0xFFC0;
130 *v++ = t >> 4 & 0xFFC0;
131 *y++ = t >> 14 & 0xFFC0;
133 if (src >= line_end - 2) {
139 y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
140 u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
141 v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
148 *(AVFrame*)data= *avctx->coded_frame;
153 static av_cold int zero12v_decode_close(AVCodecContext *avctx)
155 AVFrame *pic = avctx->coded_frame;
157 avctx->release_buffer(avctx, pic);
158 av_freep(&avctx->coded_frame);
163 AVCodec ff_zero12v_decoder = {
165 .type = AVMEDIA_TYPE_VIDEO,
166 .id = AV_CODEC_ID_012V,
167 .init = zero12v_decode_init,
168 .close = zero12v_decode_close,
169 .decode = zero12v_decode_frame,
170 .capabilities = CODEC_CAP_DR1,
171 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),