FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
012v.c
Go to the documentation of this file.
1 /*
2  * 012v decoder
3  *
4  * Copyright (C) 2012 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 #include "internal.h"
25 #include "libavutil/intreadwrite.h"
26 
28 {
29  avctx->pix_fmt = PIX_FMT_YUV422P16;
30  avctx->bits_per_raw_sample = 10;
31 
33  if (!avctx->coded_frame)
34  return AVERROR(ENOMEM);
35 
36  if (avctx->codec_tag == MKTAG('a', '1', '2', 'v'))
37  av_log_ask_for_sample(avctx, "Samples with actual transparency needed\n");
38 
40  avctx->coded_frame->key_frame = 1;
41  return 0;
42 }
43 
44 static int zero12v_decode_frame(AVCodecContext *avctx, void *data,
45  int *got_frame, AVPacket *avpkt)
46 {
47  int line = 0, ret;
48  const int width = avctx->width;
49  AVFrame *pic = avctx->coded_frame;
50  uint16_t *y, *u, *v;
51  const uint8_t *line_end, *src = avpkt->data;
52  int stride = avctx->width * 8 / 3;
53 
54  if (pic->data[0])
55  avctx->release_buffer(avctx, pic);
56 
57  if (width == 1) {
58  av_log(avctx, AV_LOG_ERROR, "Width 1 not supported.\n");
59  return AVERROR_INVALIDDATA;
60  }
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;
65  }
66 
67  pic->reference = 0;
68  if ((ret = ff_get_buffer(avctx, pic)) < 0)
69  return ret;
70 
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;
75 
76  while (line++ < avctx->height) {
77  while (1) {
78  uint32_t t = AV_RL32(src);
79  src += 4;
80  *u++ = t << 6 & 0xFFC0;
81  *y++ = t >> 4 & 0xFFC0;
82  *v++ = t >> 14 & 0xFFC0;
83 
84  if (src >= line_end - 1) {
85  *y = 0x80;
86  src++;
87  line_end += stride;
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]);
91  break;
92  }
93 
94  t = AV_RL32(src);
95  src += 4;
96  *y++ = t << 6 & 0xFFC0;
97  *u++ = t >> 4 & 0xFFC0;
98  *y++ = t >> 14 & 0xFFC0;
99  if (src >= line_end - 2) {
100  if (!(width & 1)) {
101  *y = 0x80;
102  src += 2;
103  }
104  line_end += stride;
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]);
108  break;
109  }
110 
111  t = AV_RL32(src);
112  src += 4;
113  *v++ = t << 6 & 0xFFC0;
114  *y++ = t >> 4 & 0xFFC0;
115  *u++ = t >> 14 & 0xFFC0;
116 
117  if (src >= line_end - 1) {
118  *y = 0x80;
119  src++;
120  line_end += stride;
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]);
124  break;
125  }
126 
127  t = AV_RL32(src);
128  src += 4;
129  *y++ = t << 6 & 0xFFC0;
130  *v++ = t >> 4 & 0xFFC0;
131  *y++ = t >> 14 & 0xFFC0;
132 
133  if (src >= line_end - 2) {
134  if (width & 1) {
135  *y = 0x80;
136  src += 2;
137  }
138  line_end += stride;
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]);
142  break;
143  }
144  }
145  }
146 
147  *got_frame = 1;
148  *(AVFrame*)data= *avctx->coded_frame;
149 
150  return avpkt->size;
151 }
152 
154 {
155  AVFrame *pic = avctx->coded_frame;
156  if (pic->data[0])
157  avctx->release_buffer(avctx, pic);
158  av_freep(&avctx->coded_frame);
159 
160  return 0;
161 }
162 
164  .name = "012v",
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"),
172 };