FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
paf.c
Go to the documentation of this file.
1 /*
2  * Packed Animation File video and audio decoder
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/paf.h"
24 #include "bytestream.h"
25 #include "avcodec.h"
26 #include "copy_block.h"
27 #include "internal.h"
28 
29 
30 static const uint8_t block_sequences[16][8] =
31 {
32  { 0, 0, 0, 0, 0, 0, 0, 0 },
33  { 2, 0, 0, 0, 0, 0, 0, 0 },
34  { 5, 7, 0, 0, 0, 0, 0, 0 },
35  { 5, 0, 0, 0, 0, 0, 0, 0 },
36  { 6, 0, 0, 0, 0, 0, 0, 0 },
37  { 5, 7, 5, 7, 0, 0, 0, 0 },
38  { 5, 7, 5, 0, 0, 0, 0, 0 },
39  { 5, 7, 6, 0, 0, 0, 0, 0 },
40  { 5, 5, 0, 0, 0, 0, 0, 0 },
41  { 3, 0, 0, 0, 0, 0, 0, 0 },
42  { 6, 6, 0, 0, 0, 0, 0, 0 },
43  { 2, 4, 0, 0, 0, 0, 0, 0 },
44  { 2, 4, 5, 7, 0, 0, 0, 0 },
45  { 2, 4, 5, 0, 0, 0, 0, 0 },
46  { 2, 4, 6, 0, 0, 0, 0, 0 },
47  { 2, 4, 5, 7, 5, 7, 0, 0 }
48 };
49 
50 typedef struct PAFVideoDecContext {
53 
58 
61 
63 {
64  PAFVideoDecContext *c = avctx->priv_data;
65  int i;
66 
67  if (avctx->height & 3 || avctx->width & 3) {
68  av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
69  return AVERROR_INVALIDDATA;
70  }
71 
72  avctx->pix_fmt = AV_PIX_FMT_PAL8;
73 
75  c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
76  c->video_size = avctx->height * avctx->width;
77  for (i = 0; i < 4; i++) {
78  c->frame[i] = av_mallocz(c->frame_size);
79  if (!c->frame[i])
80  return AVERROR(ENOMEM);
81  }
82 
83  return 0;
84 }
85 
87 {
88  int x, y;
89 
90  x = b & 0x7F;
91  y = ((a & 0x3F) << 1) | (b >> 7 & 1);
92 
93  return y * 2 * avctx->width + x * 2;
94 }
95 
96 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
97 {
98  PAFVideoDecContext *c = avctx->priv_data;
99  int i;
100 
101  for (i = 0; i < 4; i++) {
102  bytestream2_get_buffer(&c->gb, dst, 4);
103  dst += avctx->width;
104  }
105 }
106 
108 {
109  int i;
110 
111  for (i = 0; i < 4; i++) {
112  if ((mask >> 4) & (1 << (3 - i)))
113  dst[i] = color;
114  if ((mask & 15) & (1 << (3 - i)))
115  dst[avctx->width + i] = color;
116  }
117 }
118 
119 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
120 {
121  int i;
122 
123  for (i = 0; i < 4; i++) {
124  if ((mask >> 4) & (1 << (3 - i)))
125  dst[i] = src[i];
126  if ((mask & 15) & (1 << (3 - i)))
127  dst[avctx->width + i] = src[avctx->width + i];
128  }
129 }
130 
131 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
132 {
133  PAFVideoDecContext *c = avctx->priv_data;
134  uint32_t opcode_size, offset;
135  uint8_t *dst, *dend, mask = 0, color = 0, a, b, p;
136  const uint8_t *src, *send, *opcodes;
137  int i, j, x = 0;
138 
139  i = bytestream2_get_byte(&c->gb);
140  if (i) {
141  if (code & 0x10) {
142  int align;
143 
144  align = bytestream2_tell(&c->gb) & 3;
145  if (align)
146  bytestream2_skip(&c->gb, 4 - align);
147  }
148  do {
149  a = bytestream2_get_byte(&c->gb);
150  b = bytestream2_get_byte(&c->gb);
151  p = (a & 0xC0) >> 6;
152  dst = c->frame[p] + get_video_page_offset(avctx, a, b);
153  dend = c->frame[p] + c->frame_size;
154  offset = (b & 0x7F) * 2;
155  j = bytestream2_get_le16(&c->gb) + offset;
156 
157  do {
158  offset++;
159  if (dst + 3 * avctx->width + 4 > dend)
160  return AVERROR_INVALIDDATA;
161  copy4h(avctx, dst);
162  if ((offset & 0x3F) == 0)
163  dst += avctx->width * 3;
164  dst += 4;
165  } while (offset < j);
166  } while (--i);
167  }
168 
169  dst = c->frame[c->current_frame];
170  dend = c->frame[c->current_frame] + c->frame_size;
171  do {
172  a = bytestream2_get_byte(&c->gb);
173  b = bytestream2_get_byte(&c->gb);
174  p = (a & 0xC0) >> 6;
175  src = c->frame[p] + get_video_page_offset(avctx, a, b);
176  send = c->frame[p] + c->frame_size;
177  if ((src + 3 * avctx->width + 4 > send) ||
178  (dst + 3 * avctx->width + 4 > dend))
179  return AVERROR_INVALIDDATA;
180  copy_block4(dst, src, avctx->width, avctx->width, 4);
181  i++;
182  if ((i & 0x3F) == 0)
183  dst += avctx->width * 3;
184  dst += 4;
185  } while (i < c->video_size / 16);
186 
187  opcode_size = bytestream2_get_le16(&c->gb);
188  bytestream2_skip(&c->gb, 2);
189 
190  if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
191  return AVERROR_INVALIDDATA;
192 
193  opcodes = pkt + bytestream2_tell(&c->gb);
194  bytestream2_skipu(&c->gb, opcode_size);
195 
196  dst = c->frame[c->current_frame];
197 
198  for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
199  for (j = 0; j < avctx->width; j += 4, dst += 4) {
200  int opcode, k = 0;
201 
202  if (x > opcode_size)
203  return AVERROR_INVALIDDATA;
204  if (j & 4) {
205  opcode = opcodes[x] & 15;
206  x++;
207  } else {
208  opcode = opcodes[x] >> 4;
209  }
210 
211  while (block_sequences[opcode][k]) {
212 
213  offset = avctx->width * 2;
214  code = block_sequences[opcode][k++];
215 
216  switch (code) {
217  case 2:
218  offset = 0;
219  case 3:
220  color = bytestream2_get_byte(&c->gb);
221  case 4:
222  mask = bytestream2_get_byte(&c->gb);
223  copy_color_mask(avctx, mask, dst + offset, color);
224  break;
225  case 5:
226  offset = 0;
227  case 6:
228  a = bytestream2_get_byte(&c->gb);
229  b = bytestream2_get_byte(&c->gb);
230  p = (a & 0xC0) >> 6;
231  src = c->frame[p] + get_video_page_offset(avctx, a, b);
232  send = c->frame[p] + c->frame_size;
233  case 7:
234  if (src + offset + avctx->width + 4 > send)
235  return AVERROR_INVALIDDATA;
236  mask = bytestream2_get_byte(&c->gb);
237  copy_src_mask(avctx, mask, dst + offset, src + offset);
238  break;
239  }
240  }
241  }
242  }
243 
244  return 0;
245 }
246 
247 static int paf_vid_decode(AVCodecContext *avctx, void *data,
248  int *got_frame, AVPacket *pkt)
249 {
250  PAFVideoDecContext *c = avctx->priv_data;
251  uint8_t code, *dst, *src, *end;
252  int i, frame, ret;
253 
254  c->pic.reference = 3;
255  if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
256  return ret;
257 
258  bytestream2_init(&c->gb, pkt->data, pkt->size);
259 
260  code = bytestream2_get_byte(&c->gb);
261  if (code & 0x20) {
262  for (i = 0; i < 4; i++)
263  memset(c->frame[i], 0, c->frame_size);
264 
265  memset(c->pic.data[1], 0, AVPALETTE_SIZE);
266  c->current_frame = 0;
267  c->pic.key_frame = 1;
269  } else {
270  c->pic.key_frame = 0;
272  }
273 
274  if (code & 0x40) {
275  uint32_t *out = (uint32_t *)c->pic.data[1];
276  int index, count;
277 
278  index = bytestream2_get_byte(&c->gb);
279  count = bytestream2_get_byte(&c->gb) + 1;
280 
281  if (index + count > 256)
282  return AVERROR_INVALIDDATA;
283  if (bytestream2_get_bytes_left(&c->gb) < 3*count)
284  return AVERROR_INVALIDDATA;
285 
286  out += index;
287  for (i = 0; i < count; i++) {
288  unsigned r, g, b;
289 
290  r = bytestream2_get_byteu(&c->gb);
291  r = r << 2 | r >> 4;
292  g = bytestream2_get_byteu(&c->gb);
293  g = g << 2 | g >> 4;
294  b = bytestream2_get_byteu(&c->gb);
295  b = b << 2 | b >> 4;
296  *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
297  }
298  c->pic.palette_has_changed = 1;
299  }
300 
301  switch (code & 0x0F) {
302  case 0:
303  if ((ret = decode_0(avctx, code, pkt->data)) < 0)
304  return ret;
305  break;
306  case 1:
307  dst = c->frame[c->current_frame];
308  bytestream2_skip(&c->gb, 2);
310  return AVERROR_INVALIDDATA;
311  bytestream2_get_bufferu(&c->gb, dst, c->video_size);
312  break;
313  case 2:
314  frame = bytestream2_get_byte(&c->gb);
315  if (frame > 3)
316  return AVERROR_INVALIDDATA;
317  if (frame != c->current_frame)
318  memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
319  break;
320  case 4:
321  dst = c->frame[c->current_frame];
322  end = dst + c->video_size;
323 
324  bytestream2_skip(&c->gb, 2);
325 
326  while (dst < end) {
327  int8_t code;
328  int count;
329 
330  if (bytestream2_get_bytes_left(&c->gb) < 2)
331  return AVERROR_INVALIDDATA;
332 
333  code = bytestream2_get_byteu(&c->gb);
334  count = FFABS(code) + 1;
335 
336  if (dst + count > end)
337  return AVERROR_INVALIDDATA;
338  if (code < 0)
339  memset(dst, bytestream2_get_byteu(&c->gb), count);
340  else
341  bytestream2_get_buffer(&c->gb, dst, count);
342  dst += count;
343  }
344  break;
345  default:
346  av_log_ask_for_sample(avctx, "unknown/invalid code\n");
347  return AVERROR_INVALIDDATA;
348  }
349 
350  dst = c->pic.data[0];
351  src = c->frame[c->current_frame];
352  for (i = 0; i < avctx->height; i++) {
353  memcpy(dst, src, avctx->width);
354  dst += c->pic.linesize[0];
355  src += avctx->width;
356  }
357 
358  c->current_frame = (c->current_frame + 1) & 3;
359 
360  *got_frame = 1;
361  *(AVFrame *)data = c->pic;
362 
363  return pkt->size;
364 }
365 
367 {
368  PAFVideoDecContext *c = avctx->priv_data;
369  int i;
370 
371  if (c->pic.data[0])
372  avctx->release_buffer(avctx, &c->pic);
373 
374  for (i = 0; i < 4; i++)
375  av_freep(&c->frame[i]);
376 
377  return 0;
378 }
379 
381 {
382  if (avctx->channels != 2) {
383  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
384  return AVERROR_INVALIDDATA;
385  }
386 
388  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
389 
390  return 0;
391 }
392 
393 static int paf_aud_decode(AVCodecContext *avctx, void *data,
394  int *got_frame_ptr, AVPacket *pkt)
395 {
396  AVFrame *frame = data;
397  uint8_t *buf = pkt->data;
398  int16_t *output_samples;
399  const uint8_t *t;
400  int frames, ret, i, j, k;
401 
402  frames = pkt->size / PAF_SOUND_FRAME_SIZE;
403  if (frames < 1)
404  return AVERROR_INVALIDDATA;
405 
406  frame->nb_samples = PAF_SOUND_SAMPLES * frames;
407  if ((ret = ff_get_buffer(avctx, frame)) < 0)
408  return ret;
409 
410  output_samples = (int16_t *)frame->data[0];
411  for (i = 0; i < frames; i++) {
412  t = buf + 256 * sizeof(uint16_t);
413  for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
414  for (k = 0; k < 2; k++) {
415  *output_samples++ = AV_RL16(buf + *t * 2);
416  t++;
417  }
418  }
419  buf += PAF_SOUND_FRAME_SIZE;
420  }
421 
422  *got_frame_ptr = 1;
423 
424  return pkt->size;
425 }
426 
428  .name = "paf_video",
429  .type = AVMEDIA_TYPE_VIDEO,
430  .id = AV_CODEC_ID_PAF_VIDEO,
431  .priv_data_size = sizeof(PAFVideoDecContext),
432  .init = paf_vid_init,
433  .close = paf_vid_close,
435  .capabilities = CODEC_CAP_DR1,
436  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
437 };
438 
440  .name = "paf_audio",
441  .type = AVMEDIA_TYPE_AUDIO,
442  .id = AV_CODEC_ID_PAF_AUDIO,
443  .init = paf_aud_init,
444  .decode = paf_aud_decode,
445  .capabilities = CODEC_CAP_DR1,
446  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
447 };