FFmpeg
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 "codec_internal.h"
27 #include "get_bits.h"
28 #include "internal.h"
29 
30 
31 #define FRAME_HEADER_SIZE 64
32 #define MAGIC_0 0x19 /**< First header byte */
33 #define MAGIC_1 0x68 /**< Second header byte */
34 #define SUBSAMPLE_420 0
35 #define SUBSAMPLE_422 1
36 #define YUVORDER_YUYV 0
37 #define YUVORDER_UYVY 1
38 #define NOT_COMPRESSED 0
39 #define COMPRESSED 1
40 #define NO_DECIMATION 0
41 #define DECIMATION_ENAB 1
42 #define EOL 0xfd /**< End Of Line marker */
43 #define EOI 0xff /**< End Of Image marker */
44 
45 
46 typedef struct {
48 } CpiaContext;
49 
50 
51 static int cpia_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
52  int *got_frame, AVPacket* avpkt)
53 {
54  CpiaContext* const cpia = avctx->priv_data;
55  int i,j,ret;
56 
57  uint8_t* const header = avpkt->data;
58  uint8_t* src;
59  int src_size;
60  uint16_t linelength;
61  uint8_t skip;
62 
63  AVFrame *frame = cpia->frame;
64  uint8_t *y, *u, *v, *y_end, *u_end, *v_end;
65 
66  // Check header
67  if ( avpkt->size < FRAME_HEADER_SIZE + avctx->height * 3
68  || header[0] != MAGIC_0 || header[1] != MAGIC_1
69  || (header[17] != SUBSAMPLE_420 && header[17] != SUBSAMPLE_422)
70  || (header[18] != YUVORDER_YUYV && header[18] != YUVORDER_UYVY)
71  || (header[28] != NOT_COMPRESSED && header[28] != COMPRESSED)
72  || (header[29] != NO_DECIMATION && header[29] != DECIMATION_ENAB)
73  ) {
74  av_log(avctx, AV_LOG_ERROR, "Invalid header!\n");
75  return AVERROR_INVALIDDATA;
76  }
77 
78  // currently unsupported properties
79  if (header[17] == SUBSAMPLE_422) {
80  avpriv_report_missing_feature(avctx, "4:2:2 subsampling");
81  return AVERROR_PATCHWELCOME;
82  }
83  if (header[18] == YUVORDER_UYVY) {
84  avpriv_report_missing_feature(avctx, "YUV byte order UYVY");
85  return AVERROR_PATCHWELCOME;
86  }
87  if (header[29] == DECIMATION_ENAB) {
88  avpriv_report_missing_feature(avctx, "Decimation");
89  return AVERROR_PATCHWELCOME;
90  }
91 
93  src_size = avpkt->size - FRAME_HEADER_SIZE;
94 
95  if (header[28] == NOT_COMPRESSED) {
96  frame->pict_type = AV_PICTURE_TYPE_I;
97  frame->key_frame = 1;
98  } else {
99  frame->pict_type = AV_PICTURE_TYPE_P;
100  frame->key_frame = 0;
101  }
102 
103  // Get buffer filled with previous frame
104  if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
105  return ret;
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  src_size -= 2;
116 
117  if (src_size < linelength) {
118  frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
119  av_log(avctx, AV_LOG_WARNING, "Frame ended unexpectedly!\n");
120  break;
121  }
122  if (src[linelength - 1] != EOL) {
123  frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
124  av_log(avctx, AV_LOG_WARNING, "Wrong line length %d or line not terminated properly (found 0x%02x)!\n", linelength, src[linelength - 1]);
125  break;
126  }
127 
128  /* Update the data pointers. Y data is on every line.
129  * U and V data on every second line
130  */
131  y = &frame->data[0][i * frame->linesize[0]];
132  u = &frame->data[1][(i >> 1) * frame->linesize[1]];
133  v = &frame->data[2][(i >> 1) * frame->linesize[2]];
134  y_end = y + frame->linesize[0] - 1;
135  u_end = u + frame->linesize[1] - 1;
136  v_end = v + frame->linesize[2] - 1;
137 
138  if ((i & 1) && header[17] == SUBSAMPLE_420) {
139  /* We are on an odd line and 420 subsample is used.
140  * On this line only Y values are specified, one per pixel.
141  */
142  for (j = 0; j < linelength - 1; j++) {
143  if (y > y_end) {
144  frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
145  av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
146  break;
147  }
148  if ((src[j] & 1) && header[28] == COMPRESSED) {
149  /* It seems that odd lines are always uncompressed, but
150  * we do it according to specification anyways.
151  */
152  skip = src[j] >> 1;
153  y += skip;
154  } else {
155  *(y++) = src[j];
156  }
157  }
158  } else if (header[17] == SUBSAMPLE_420) {
159  /* We are on an even line and 420 subsample is used.
160  * On this line each pair of pixels is described by four bytes.
161  */
162  for (j = 0; j < linelength - 4; ) {
163  if (y + 1 > y_end || u > u_end || v > v_end) {
164  frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
165  av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
166  break;
167  }
168  if ((src[j] & 1) && header[28] == COMPRESSED) {
169  // Skip amount of pixels and move forward one byte
170  skip = src[j] >> 1;
171  y += skip;
172  u += skip >> 1;
173  v += skip >> 1;
174  j++;
175  } else {
176  // Set image data as specified and move forward 4 bytes
177  *(y++) = src[j];
178  *(u++) = src[j+1];
179  *(y++) = src[j+2];
180  *(v++) = src[j+3];
181  j += 4;
182  }
183  }
184  }
185  }
186 
187  *got_frame = 1;
188  if ((ret = av_frame_ref(rframe, cpia->frame)) < 0)
189  return ret;
190 
191  return avpkt->size;
192 }
193 
195 {
196  CpiaContext *s = avctx->priv_data;
197 
198  // output pixel format
199  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
200 
201  /* The default timebase set by the v4l2 demuxer leads to probing which is buggy.
202  * Set some reasonable time_base to skip this.
203  */
204  if (avctx->time_base.num == 1 && avctx->time_base.den == 1000000) {
205  avctx->time_base.num = 1;
206  avctx->time_base.den = 60;
207  }
208 
209  s->frame = av_frame_alloc();
210  if (!s->frame)
211  return AVERROR(ENOMEM);
212 
213  return 0;
214 }
215 
217 {
218  CpiaContext *s = avctx->priv_data;
219 
220  av_frame_free(&s->frame);
221 
222  return 0;
223 }
224 
226  .p.name = "cpia",
227  .p.long_name = NULL_IF_CONFIG_SMALL("CPiA video format"),
228  .p.type = AVMEDIA_TYPE_VIDEO,
229  .p.id = AV_CODEC_ID_CPIA,
230  .priv_data_size = sizeof(CpiaContext),
232  .close = cpia_decode_end,
234  .p.capabilities = AV_CODEC_CAP_DR1,
235  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
236 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
NOT_COMPRESSED
#define NOT_COMPRESSED
Definition: cpia.c:38
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
CpiaContext::frame
AVFrame * frame
Definition: cpia.c:47
SUBSAMPLE_420
#define SUBSAMPLE_420
Definition: cpia.c:34
FFCodec
Definition: codec_internal.h:112
MAGIC_1
#define MAGIC_1
Second header byte.
Definition: cpia.c:33
YUVORDER_YUYV
#define YUVORDER_YUYV
Definition: cpia.c:36
cpia_decode_init
static av_cold int cpia_decode_init(AVCodecContext *avctx)
Definition: cpia.c:194
CpiaContext
Definition: cpia.c:46
init
static int init
Definition: av_tx.c:47
cpia_decode_frame
static int cpia_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: cpia.c:51
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
AVRational::num
int num
Numerator.
Definition: rational.h:59
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
s
#define s(width, name)
Definition: cbs_vp9.c:256
DECIMATION_ENAB
#define DECIMATION_ENAB
Definition: cpia.c:41
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
FF_DECODE_ERROR_INVALID_BITSTREAM
#define FF_DECODE_ERROR_INVALID_BITSTREAM
Definition: frame.h:630
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AV_CODEC_ID_CPIA
@ AV_CODEC_ID_CPIA
Definition: codec_id.h:260
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
NO_DECIMATION
#define NO_DECIMATION
Definition: cpia.c:40
YUVORDER_UYVY
#define YUVORDER_UYVY
Definition: cpia.c:37
FRAME_HEADER_SIZE
#define FRAME_HEADER_SIZE
Definition: cpia.c:31
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:512
ff_cpia_decoder
const FFCodec ff_cpia_decoder
Definition: cpia.c:225
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:343
codec_internal.h
SUBSAMPLE_422
#define SUBSAMPLE_422
Definition: cpia.c:35
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
header
static const uint8_t header[24]
Definition: sdr2.c:67
cpia_decode_end
static av_cold int cpia_decode_end(AVCodecContext *avctx)
Definition: cpia.c:216
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
avcodec.h
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1521
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:389
EOL
#define EOL
End Of Line marker.
Definition: cpia.c:42
AVRational::den
int den
Denominator.
Definition: rational.h:60
COMPRESSED
#define COMPRESSED
Definition: cpia.c:39
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVPacket
This structure stores compressed data.
Definition: packet.h:351
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
MAGIC_0
#define MAGIC_0
First header byte.
Definition: cpia.c:32
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61