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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/imgutils.h"
27 
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "internal.h"
31 
32 typedef struct YopDecContext {
35 
37  int first_color[2];
39 
46 
47 // These tables are taken directly from:
48 // http://wiki.multimedia.cx/index.php?title=Psygnosis_YOP
49 
56 static const uint8_t paint_lut[15][4] =
57  {{1, 2, 3, 4}, {1, 2, 0, 3},
58  {1, 2, 1, 3}, {1, 2, 2, 3},
59  {1, 0, 2, 3}, {1, 0, 0, 2},
60  {1, 0, 1, 2}, {1, 1, 2, 3},
61  {0, 1, 2, 3}, {0, 1, 0, 2},
62  {1, 1, 0, 2}, {0, 1, 1, 2},
63  {0, 0, 1, 2}, {0, 0, 0, 1},
64  {1, 1, 1, 2},
65  };
66 
71 static const int8_t motion_vector[16][2] =
72  {{-4, -4}, {-2, -4},
73  { 0, -4}, { 2, -4},
74  {-4, -2}, {-4, 0},
75  {-3, -3}, {-1, -3},
76  { 1, -3}, { 3, -3},
77  {-3, -1}, {-2, -2},
78  { 0, -2}, { 2, -2},
79  { 4, -2}, {-2, 0},
80  };
81 
83 {
84  YopDecContext *s = avctx->priv_data;
85  s->avctx = avctx;
86 
87  if (avctx->width & 1 || avctx->height & 1 ||
88  av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
89  av_log(avctx, AV_LOG_ERROR, "YOP has invalid dimensions\n");
90  return -1;
91  }
92 
93  if (avctx->extradata_size < 3) {
94  av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata.\n");
95  return AVERROR_INVALIDDATA;
96  }
97 
98  avctx->pix_fmt = AV_PIX_FMT_PAL8;
99 
100  s->num_pal_colors = avctx->extradata[0];
101  s->first_color[0] = avctx->extradata[1];
102  s->first_color[1] = avctx->extradata[2];
103 
104  if (s->num_pal_colors + s->first_color[0] > 256 ||
105  s->num_pal_colors + s->first_color[1] > 256) {
106  av_log(avctx, AV_LOG_ERROR,
107  "YOP: palette parameters invalid, header probably corrupt\n");
108  return AVERROR_INVALIDDATA;
109  }
110 
111  return 0;
112 }
113 
115 {
116  YopDecContext *s = avctx->priv_data;
117  if (s->frame.data[0])
118  avctx->release_buffer(avctx, &s->frame);
119  return 0;
120 }
121 
127 static int yop_paint_block(YopDecContext *s, int tag)
128 {
129  if (s->src_end - s->srcptr < paint_lut[tag][3]) {
130  av_log(s->avctx, AV_LOG_ERROR, "Packet too small.\n");
131  return AVERROR_INVALIDDATA;
132  }
133 
134  s->dstptr[0] = s->srcptr[0];
135  s->dstptr[1] = s->srcptr[paint_lut[tag][0]];
136  s->dstptr[s->frame.linesize[0]] = s->srcptr[paint_lut[tag][1]];
137  s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]];
138 
139  // The number of src bytes consumed is in the last part of the lut entry.
140  s->srcptr += paint_lut[tag][3];
141  return 0;
142 }
143 
149 {
150  uint8_t *bufptr;
151 
152  // Calculate position for the copy source
153  bufptr = s->dstptr + motion_vector[copy_tag][0] +
154  s->frame.linesize[0] * motion_vector[copy_tag][1];
155  if (bufptr < s->dstbuf) {
157  "YOP: cannot decode, file probably corrupt\n");
158  return AVERROR_INVALIDDATA;
159  }
160 
161  s->dstptr[0] = bufptr[0];
162  s->dstptr[1] = bufptr[1];
163  s->dstptr[s->frame.linesize[0]] = bufptr[s->frame.linesize[0]];
164  s->dstptr[s->frame.linesize[0] + 1] = bufptr[s->frame.linesize[0] + 1];
165 
166  return 0;
167 }
168 
174 {
175  int ret;
176 
177  if (s->low_nibble) {
178  ret = *s->low_nibble & 0xf;
179  s->low_nibble = NULL;
180  }else {
181  s->low_nibble = s->srcptr++;
182  ret = *s->low_nibble >> 4;
183  }
184  return ret;
185 }
186 
187 static int yop_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
188  AVPacket *avpkt)
189 {
190  YopDecContext *s = avctx->priv_data;
191  int tag, firstcolor, is_odd_frame;
192  int ret, i, x, y;
193  uint32_t *palette;
194 
195  if (avpkt->size < 4 + 3 * s->num_pal_colors) {
196  av_log(avctx, AV_LOG_ERROR, "Packet too small.\n");
197  return AVERROR_INVALIDDATA;
198  }
199 
200  if (s->frame.data[0])
201  avctx->release_buffer(avctx, &s->frame);
202 
203  ret = ff_get_buffer(avctx, &s->frame);
204  if (ret < 0) {
205  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
206  return ret;
207  }
208 
209  s->dstbuf = s->frame.data[0];
210  s->dstptr = s->frame.data[0];
211  s->srcptr = avpkt->data + 4;
212  s->src_end = avpkt->data + avpkt->size;
213  s->low_nibble = NULL;
214 
215  is_odd_frame = avpkt->data[0];
216  firstcolor = s->first_color[is_odd_frame];
217  palette = (uint32_t *)s->frame.data[1];
218 
219  for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3)
220  palette[i + firstcolor] = (s->srcptr[0] << 18) |
221  (s->srcptr[1] << 10) |
222  (s->srcptr[2] << 2);
223 
224  s->frame.palette_has_changed = 1;
225 
226  for (y = 0; y < avctx->height; y += 2) {
227  for (x = 0; x < avctx->width; x += 2) {
228  if (s->srcptr - avpkt->data >= avpkt->size) {
229  av_log(avctx, AV_LOG_ERROR, "Packet too small.\n");
230  return AVERROR_INVALIDDATA;
231  }
232 
233  tag = yop_get_next_nibble(s);
234 
235  if (tag != 0xf) {
236  ret = yop_paint_block(s, tag);
237  if (ret < 0)
238  return ret;
239  } else {
240  tag = yop_get_next_nibble(s);
241  ret = yop_copy_previous_block(s, tag);
242  if (ret < 0) {
243  avctx->release_buffer(avctx, &s->frame);
244  return ret;
245  }
246  }
247  s->dstptr += 2;
248  }
249  s->dstptr += 2*s->frame.linesize[0] - x;
250  }
251 
252  *got_frame = 1;
253  *(AVFrame *) data = s->frame;
254  return avpkt->size;
255 }
256 
258  .name = "yop",
259  .type = AVMEDIA_TYPE_VIDEO,
260  .id = AV_CODEC_ID_YOP,
261  .priv_data_size = sizeof(YopDecContext),
265  .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
266  .capabilities = CODEC_CAP_DR1,
267 };
uint8_t * srcptr
Definition: yop.c:41
static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
Definition: ismindex.c:85
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
misc image utilities
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:173
int size
Definition: avcodec.h:916
uint8_t * src_end
Definition: yop.c:42
int frame_data_length
Definition: yop.c:38
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static int yop_copy_previous_block(YopDecContext *s, int copy_tag)
Copy a previously painted macroblock to the current_block.
Definition: yop.c:148
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t * dstptr
Definition: yop.c:43
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
static int yop_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: yop.c:187
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
uint32_t tag
Definition: movenc.c:802
bitstream reader API header.
AVFrame frame
Definition: yop.c:33
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static int yop_paint_block(YopDecContext *s, int tag)
Paint a macroblock using the pattern in paint_lut.
Definition: yop.c:127
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
uint8_t * low_nibble
Definition: yop.c:40
static const int8_t motion_vector[16][2]
Lookup table for copying macroblocks.
Definition: yop.c:71
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
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:220
int width
picture width / height.
Definition: avcodec.h:1508
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static const uint8_t paint_lut[15][4]
Lookup table for painting macroblocks.
Definition: yop.c:56
AVCodec ff_yop_decoder
Definition: yop.c:257
NULL
Definition: eval.c:52
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static av_cold int yop_decode_close(AVCodecContext *avctx)
Definition: yop.c:114
int extradata_size
Definition: avcodec.h:1455
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
int num_pal_colors
Definition: yop.c:36
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int first_color[2]
Definition: yop.c:37
common internal api header.
void * priv_data
Definition: avcodec.h:1382
static av_cold int yop_decode_init(AVCodecContext *avctx)
Definition: yop.c:82
AVCodecContext * avctx
Definition: yop.c:34
uint8_t * dstbuf
Definition: yop.c:44
This structure stores compressed data.
Definition: avcodec.h:898
for(j=16;j >0;--j)
struct YopDecContext YopDecContext