FFmpeg
yop.c
Go to the documentation of this file.
1 /*
2  * Psygnosis YOP decoder
3  *
4  * Copyright (C) 2010 Mohamed Naufal Basheer <naufal11@gmail.com>
5  * derived from the code by
6  * Copyright (C) 2009 Thomas P. Higdon <thomas.p.higdon@gmail.com>
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 <string.h>
26 
27 #include "libavutil/imgutils.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/intreadwrite.h"
30 
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "internal.h"
34 
35 typedef struct YopDecContext {
38 
40  int first_color[2];
42 
43  const uint8_t *low_nibble;
44  const uint8_t *srcptr;
45  const uint8_t *src_end;
46  uint8_t *dstptr;
47  uint8_t *dstbuf;
49 
50 // These tables are taken directly from:
51 // http://wiki.multimedia.cx/index.php?title=Psygnosis_YOP
52 
53 /**
54  * Lookup table for painting macroblocks. Bytes 0-2 of each entry contain
55  * the macroblock positions to be painted (taken as (0, B0, B1, B2)).
56  * Byte 3 contains the number of bytes consumed on the input,
57  * equal to max(bytes 0-2) + 1.
58  */
59 static const uint8_t paint_lut[15][4] =
60  {{1, 2, 3, 4}, {1, 2, 0, 3},
61  {1, 2, 1, 3}, {1, 2, 2, 3},
62  {1, 0, 2, 3}, {1, 0, 0, 2},
63  {1, 0, 1, 2}, {1, 1, 2, 3},
64  {0, 1, 2, 3}, {0, 1, 0, 2},
65  {1, 1, 0, 2}, {0, 1, 1, 2},
66  {0, 0, 1, 2}, {0, 0, 0, 1},
67  {1, 1, 1, 2},
68  };
69 
70 /**
71  * Lookup table for copying macroblocks. Each entry contains the respective
72  * x and y pixel offset for the copy source.
73  */
74 static const int8_t motion_vector[16][2] =
75  {{-4, -4}, {-2, -4},
76  { 0, -4}, { 2, -4},
77  {-4, -2}, {-4, 0},
78  {-3, -3}, {-1, -3},
79  { 1, -3}, { 3, -3},
80  {-3, -1}, {-2, -2},
81  { 0, -2}, { 2, -2},
82  { 4, -2}, {-2, 0},
83  };
84 
86 {
87  YopDecContext *s = avctx->priv_data;
88 
89  av_frame_free(&s->frame);
90 
91  return 0;
92 }
93 
95 {
96  YopDecContext *s = avctx->priv_data;
97  s->avctx = avctx;
98 
99  if (avctx->width & 1 || avctx->height & 1 ||
100  av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
101  av_log(avctx, AV_LOG_ERROR, "YOP has invalid dimensions\n");
102  return AVERROR_INVALIDDATA;
103  }
104 
105  if (avctx->extradata_size < 3) {
106  av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata.\n");
107  return AVERROR_INVALIDDATA;
108  }
109 
110  avctx->pix_fmt = AV_PIX_FMT_PAL8;
111 
112  s->num_pal_colors = avctx->extradata[0];
113  s->first_color[0] = avctx->extradata[1];
114  s->first_color[1] = avctx->extradata[2];
115 
116  if (s->num_pal_colors + s->first_color[0] > 256 ||
117  s->num_pal_colors + s->first_color[1] > 256) {
118  av_log(avctx, AV_LOG_ERROR,
119  "Palette parameters invalid, header probably corrupt\n");
120  return AVERROR_INVALIDDATA;
121  }
122 
123  s->frame = av_frame_alloc();
124  if (!s->frame)
125  return AVERROR(ENOMEM);
126 
127  return 0;
128 }
129 
130 /**
131  * Paint a macroblock using the pattern in paint_lut.
132  * @param s codec context
133  * @param tag the tag that was in the nibble
134  */
135 static int yop_paint_block(YopDecContext *s, int linesize, int tag)
136 {
137  if (s->src_end - s->srcptr < paint_lut[tag][3]) {
138  av_log(s->avctx, AV_LOG_ERROR, "Packet too small.\n");
139  return AVERROR_INVALIDDATA;
140  }
141 
142  s->dstptr[0] = s->srcptr[0];
143  s->dstptr[1] = s->srcptr[paint_lut[tag][0]];
144  s->dstptr[linesize] = s->srcptr[paint_lut[tag][1]];
145  s->dstptr[linesize + 1] = s->srcptr[paint_lut[tag][2]];
146 
147  // The number of src bytes consumed is in the last part of the lut entry.
148  s->srcptr += paint_lut[tag][3];
149  return 0;
150 }
151 
152 /**
153  * Copy a previously painted macroblock to the current_block.
154  * @param copy_tag the tag that was in the nibble
155  */
156 static int yop_copy_previous_block(YopDecContext *s, int linesize, int copy_tag)
157 {
158  uint8_t *bufptr;
159 
160  // Calculate position for the copy source
161  bufptr = s->dstptr + motion_vector[copy_tag][0] +
162  linesize * motion_vector[copy_tag][1];
163  if (bufptr < s->dstbuf) {
164  av_log(s->avctx, AV_LOG_ERROR, "File probably corrupt\n");
165  return AVERROR_INVALIDDATA;
166  }
167 
168  s->dstptr[0] = bufptr[0];
169  s->dstptr[1] = bufptr[1];
170  s->dstptr[linesize] = bufptr[linesize];
171  s->dstptr[linesize + 1] = bufptr[linesize + 1];
172 
173  return 0;
174 }
175 
176 /**
177  * Return the next nibble in sequence, consuming a new byte on the input
178  * only if necessary.
179  */
181 {
182  int ret;
183 
184  if (s->low_nibble) {
185  ret = *s->low_nibble & 0xf;
186  s->low_nibble = NULL;
187  }else {
188  s->low_nibble = s->srcptr++;
189  ret = *s->low_nibble >> 4;
190  }
191  return ret;
192 }
193 
194 static int yop_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
195  int *got_frame, AVPacket *avpkt)
196 {
197  YopDecContext *s = avctx->priv_data;
198  AVFrame *frame = s->frame;
199  int tag, firstcolor, is_odd_frame;
200  int ret, i, x, y;
201  uint32_t *palette;
202 
203  if (avpkt->size < 4 + 3 * s->num_pal_colors) {
204  av_log(avctx, AV_LOG_ERROR, "Packet too small.\n");
205  return AVERROR_INVALIDDATA;
206  }
207 
208  if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
209  return ret;
210 
211  if (!avctx->frame_number)
212  memset(frame->data[1], 0, AVPALETTE_SIZE);
213 
214  s->dstbuf = frame->data[0];
215  s->dstptr = frame->data[0];
216  s->srcptr = avpkt->data + 4;
217  s->src_end = avpkt->data + avpkt->size;
218  s->low_nibble = NULL;
219 
220  is_odd_frame = avpkt->data[0];
221  if(is_odd_frame>1){
222  av_log(avctx, AV_LOG_ERROR, "frame is too odd %d\n", is_odd_frame);
223  return AVERROR_INVALIDDATA;
224  }
225  firstcolor = s->first_color[is_odd_frame];
226  palette = (uint32_t *)frame->data[1];
227 
228  for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3) {
229  palette[i + firstcolor] = (s->srcptr[0] << 18) |
230  (s->srcptr[1] << 10) |
231  (s->srcptr[2] << 2);
232  palette[i + firstcolor] |= 0xFFU << 24 |
233  (palette[i + firstcolor] >> 6) & 0x30303;
234  }
235 
236  frame->palette_has_changed = 1;
237 
238  for (y = 0; y < avctx->height; y += 2) {
239  for (x = 0; x < avctx->width; x += 2) {
240  if (s->srcptr - avpkt->data >= avpkt->size) {
241  av_log(avctx, AV_LOG_ERROR, "Packet too small.\n");
242  return AVERROR_INVALIDDATA;
243  }
244 
246 
247  if (tag != 0xf) {
248  ret = yop_paint_block(s, frame->linesize[0], tag);
249  if (ret < 0)
250  return ret;
251  } else {
253  ret = yop_copy_previous_block(s, frame->linesize[0], tag);
254  if (ret < 0)
255  return ret;
256  }
257  s->dstptr += 2;
258  }
259  s->dstptr += 2*frame->linesize[0] - x;
260  }
261 
262  if ((ret = av_frame_ref(rframe, s->frame)) < 0)
263  return ret;
264 
265  *got_frame = 1;
266  return avpkt->size;
267 }
268 
270  .p.name = "yop",
271  .p.long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
272  .p.type = AVMEDIA_TYPE_VIDEO,
273  .p.id = AV_CODEC_ID_YOP,
274  .priv_data_size = sizeof(YopDecContext),
276  .close = yop_decode_close,
278  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
279 };
YopDecContext::num_pal_colors
int num_pal_colors
Definition: yop.c:39
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
ff_yop_decoder
const FFCodec ff_yop_decoder
Definition: yop.c:269
YopDecContext::src_end
const uint8_t * src_end
Definition: yop.c:45
yop_get_next_nibble
static uint8_t yop_get_next_nibble(YopDecContext *s)
Return the next nibble in sequence, consuming a new byte on the input only if necessary.
Definition: yop.c:180
YopDecContext::dstbuf
uint8_t * dstbuf
Definition: yop.c:47
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
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
YopDecContext::first_color
int first_color[2]
Definition: yop.c:40
FFCodec
Definition: codec_internal.h:112
AV_CODEC_ID_YOP
@ AV_CODEC_ID_YOP
Definition: codec_id.h:189
paint_lut
static const uint8_t paint_lut[15][4]
Lookup table for painting macroblocks.
Definition: yop.c:59
YopDecContext::avctx
AVCodecContext * avctx
Definition: yop.c:36
init
static int init
Definition: av_tx.c:47
YopDecContext::low_nibble
const uint8_t * low_nibble
Definition: yop.c:43
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
U
#define U(x)
Definition: vp56_arith.h:37
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
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:491
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
YopDecContext::dstptr
uint8_t * dstptr
Definition: yop.c:46
motion_vector
static const int8_t motion_vector[16][2]
Lookup table for copying macroblocks.
Definition: yop.c:74
NULL
#define NULL
Definition: coverity.c:32
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
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:117
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
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
YopDecContext::srcptr
const uint8_t * srcptr
Definition: yop.c:44
yop_decode_close
static av_cold int yop_decode_close(AVCodecContext *avctx)
Definition: yop.c:85
yop_decode_init
static av_cold int yop_decode_init(AVCodecContext *avctx)
Definition: yop.c:94
YopDecContext::frame
AVFrame * frame
Definition: yop.c:37
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:490
internal.h
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:664
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
YopDecContext
Definition: yop.c:35
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
YopDecContext::frame_data_length
int frame_data_length
Definition: yop.c:41
tag
uint32_t tag
Definition: movenc.c:1646
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
yop_copy_previous_block
static int yop_copy_previous_block(YopDecContext *s, int linesize, int copy_tag)
Copy a previously painted macroblock to the current_block.
Definition: yop.c:156
AVCodecContext
main external API structure.
Definition: avcodec.h:389
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecContext::frame_number
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1037
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
yop_paint_block
static int yop_paint_block(YopDecContext *s, int linesize, int tag)
Paint a macroblock using the pattern in paint_lut.
Definition: yop.c:135
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
imgutils.h
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
av_image_check_size
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:318
yop_decode_frame
static int yop_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: yop.c:194
copy_tag
static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
Definition: ismindex.c:107