FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cpia.c
Go to the documentation of this file.
1 /*
2  * CPiA video decoder.
3  * Copyright (c) 2010 Hans de Goede <hdegoede@redhat.com>
4  *
5  * This decoder is based on the LGPL code available at
6  * https://v4l4j.googlecode.com/svn/v4l4j/trunk/libvideo/libv4lconvert/cpia1.c
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include "avcodec.h"
26 #include "get_bits.h"
27 
28 
29 #define FRAME_HEADER_SIZE 64
30 #define MAGIC_0 0x19 /**< First header byte */
31 #define MAGIC_1 0x68 /**< Second header byte */
32 #define SUBSAMPLE_420 0
33 #define SUBSAMPLE_422 1
34 #define YUVORDER_YUYV 0
35 #define YUVORDER_UYVY 1
36 #define NOT_COMPRESSED 0
37 #define COMPRESSED 1
38 #define NO_DECIMATION 0
39 #define DECIMATION_ENAB 1
40 #define EOL 0xfd /**< End Of Line marker */
41 #define EOI 0xff /**< End Of Image marker */
42 
43 
44 typedef struct {
46 } CpiaContext;
47 
48 
50  void *data, int *got_frame, AVPacket* avpkt)
51 {
52  CpiaContext* const cpia = avctx->priv_data;
53  int i,j,ret;
54 
55  uint8_t* const header = avpkt->data;
56  uint8_t* src;
57  int src_size;
58  uint16_t linelength;
59  uint8_t skip;
60 
61  AVFrame* const frame = &cpia->frame;
62  uint8_t *y, *u, *v, *y_end, *u_end, *v_end;
63 
64  // Check header
65  if ( avpkt->size < FRAME_HEADER_SIZE
66  || header[0] != MAGIC_0 || header[1] != MAGIC_1
67  || (header[17] != SUBSAMPLE_420 && header[17] != SUBSAMPLE_422)
68  || (header[18] != YUVORDER_YUYV && header[18] != YUVORDER_UYVY)
69  || (header[28] != NOT_COMPRESSED && header[28] != COMPRESSED)
70  || (header[29] != NO_DECIMATION && header[29] != DECIMATION_ENAB)
71  ) {
72  av_log(avctx, AV_LOG_ERROR, "Invalid header!\n");
73  return AVERROR_INVALIDDATA;
74  }
75 
76  // currently unsupported properties
77  if (header[17] == SUBSAMPLE_422) {
78  av_log(avctx, AV_LOG_ERROR, "Unsupported subsample!\n");
79  return AVERROR_PATCHWELCOME;
80  }
81  if (header[18] == YUVORDER_UYVY) {
82  av_log(avctx, AV_LOG_ERROR, "Unsupported YUV byte order!\n");
83  return AVERROR_PATCHWELCOME;
84  }
85  if (header[29] == DECIMATION_ENAB) {
86  av_log(avctx, AV_LOG_ERROR, "Decimation unsupported!\n");
87  return AVERROR_PATCHWELCOME;
88  }
89 
90  src = header + FRAME_HEADER_SIZE;
91  src_size = avpkt->size - FRAME_HEADER_SIZE;
92 
93  if (header[28] == NOT_COMPRESSED) {
95  frame->key_frame = 1;
96  } else {
98  frame->key_frame = 0;
99  }
100 
101  // Get buffer filled with previous frame
102  if ((ret = avctx->reget_buffer(avctx, frame)) < 0) {
103  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed!\n");
104  return ret;
105  }
106 
107 
108  for ( i = 0;
109  i < frame->height;
110  i++, src += linelength, src_size -= linelength
111  ) {
112  // Read line length, two byte little endian
113  linelength = AV_RL16(src);
114  src += 2;
115 
116  if (src_size < linelength) {
118  av_log(avctx, AV_LOG_WARNING, "Frame ended enexpectedly!\n");
119  break;
120  }
121  if (src[linelength - 1] != EOL) {
123  av_log(avctx, AV_LOG_WARNING, "Wrong line length %d or line not terminated properly (found 0x%02x)!\n", linelength, src[linelength - 1]);
124  break;
125  }
126 
127  /* Update the data pointers. Y data is on every line.
128  * U and V data on every second line
129  */
130  y = &frame->data[0][i * frame->linesize[0]];
131  u = &frame->data[1][(i >> 1) * frame->linesize[1]];
132  v = &frame->data[2][(i >> 1) * frame->linesize[2]];
133  y_end = y + frame->linesize[0] - 1;
134  u_end = u + frame->linesize[1] - 1;
135  v_end = v + frame->linesize[2] - 1;
136 
137  if ((i & 1) && header[17] == SUBSAMPLE_420) {
138  /* We are on a odd line and 420 subsample is used.
139  * On this line only Y values are specified, one per pixel.
140  */
141  for (j = 0; j < linelength - 1; j++) {
142  if (y > y_end) {
144  av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
145  break;
146  }
147  if ((src[j] & 1) && header[28] == COMPRESSED) {
148  /* It seems that odd lines are always uncompressed, but
149  * we do it according to specification anyways.
150  */
151  skip = src[j] >> 1;
152  y += skip;
153  } else {
154  *(y++) = src[j];
155  }
156  }
157  } else if (header[17] == SUBSAMPLE_420) {
158  /* We are on an even line and 420 subsample is used.
159  * On this line each pair of pixels is described by four bytes.
160  */
161  for (j = 0; j < linelength - 4; ) {
162  if (y + 1 > y_end || u > u_end || v > v_end) {
164  av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
165  break;
166  }
167  if ((src[j] & 1) && header[28] == COMPRESSED) {
168  // Skip amount of pixels and move forward one byte
169  skip = src[j] >> 1;
170  y += skip;
171  u += skip >> 1;
172  v += skip >> 1;
173  j++;
174  } else {
175  // Set image data as specified and move forward 4 bytes
176  *(y++) = src[j];
177  *(u++) = src[j+1];
178  *(y++) = src[j+2];
179  *(v++) = src[j+3];
180  j += 4;
181  }
182  }
183  }
184  }
185 
186  *got_frame = 1;
187  *(AVFrame*) data = *frame;
188 
189  return avpkt->size;
190 }
191 
193 {
194  // output pixel format
195  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
196 
197  /* The default timebase set by the v4l2 demuxer leads to probing which is buggy.
198  * Set some reasonable time_base to skip this.
199  */
200  if (avctx->time_base.num == 1 && avctx->time_base.den == 1000000) {
201  avctx->time_base.num = 1;
202  avctx->time_base.den = 60;
203  }
204 
205  return 0;
206 }
207 
208 
210  .name = "cpia",
211  .type = AVMEDIA_TYPE_VIDEO,
212  .id = AV_CODEC_ID_CPIA,
213  .priv_data_size = sizeof(CpiaContext),
216  .capabilities = CODEC_CAP_DR1,
217  .long_name = NULL_IF_CONFIG_SMALL("CPiA video format"),
218 };