You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.0KB

  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. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/imgutils.h"
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "internal.h"
  29. typedef struct YopDecContext {
  30. AVCodecContext *avctx;
  31. AVFrame *frame;
  32. int num_pal_colors;
  33. int first_color[2];
  34. int frame_data_length;
  35. uint8_t *low_nibble;
  36. uint8_t *srcptr;
  37. uint8_t *src_end;
  38. uint8_t *dstptr;
  39. uint8_t *dstbuf;
  40. } YopDecContext;
  41. // These tables are taken directly from:
  42. // http://wiki.multimedia.cx/index.php?title=Psygnosis_YOP
  43. /**
  44. * Lookup table for painting macroblocks. Bytes 0-2 of each entry contain
  45. * the macroblock positions to be painted (taken as (0, B0, B1, B2)).
  46. * Byte 3 contains the number of bytes consumed on the input,
  47. * equal to max(bytes 0-2) + 1.
  48. */
  49. static const uint8_t paint_lut[15][4] =
  50. {{1, 2, 3, 4}, {1, 2, 0, 3},
  51. {1, 2, 1, 3}, {1, 2, 2, 3},
  52. {1, 0, 2, 3}, {1, 0, 0, 2},
  53. {1, 0, 1, 2}, {1, 1, 2, 3},
  54. {0, 1, 2, 3}, {0, 1, 0, 2},
  55. {1, 1, 0, 2}, {0, 1, 1, 2},
  56. {0, 0, 1, 2}, {0, 0, 0, 1},
  57. {1, 1, 1, 2},
  58. };
  59. /**
  60. * Lookup table for copying macroblocks. Each entry contains the respective
  61. * x and y pixel offset for the copy source.
  62. */
  63. static const int8_t motion_vector[16][2] =
  64. {{-4, -4}, {-2, -4},
  65. { 0, -4}, { 2, -4},
  66. {-4, -2}, {-4, 0},
  67. {-3, -3}, {-1, -3},
  68. { 1, -3}, { 3, -3},
  69. {-3, -1}, {-2, -2},
  70. { 0, -2}, { 2, -2},
  71. { 4, -2}, {-2, 0},
  72. };
  73. static av_cold int yop_decode_close(AVCodecContext *avctx)
  74. {
  75. YopDecContext *s = avctx->priv_data;
  76. av_frame_free(&s->frame);
  77. return 0;
  78. }
  79. static av_cold int yop_decode_init(AVCodecContext *avctx)
  80. {
  81. YopDecContext *s = avctx->priv_data;
  82. s->avctx = avctx;
  83. if (avctx->width & 1 || avctx->height & 1 ||
  84. av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
  85. av_log(avctx, AV_LOG_ERROR, "YOP has invalid dimensions\n");
  86. return AVERROR_INVALIDDATA;
  87. }
  88. if (avctx->extradata_size < 3) {
  89. av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata.\n");
  90. return AVERROR_INVALIDDATA;
  91. }
  92. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  93. s->num_pal_colors = avctx->extradata[0];
  94. s->first_color[0] = avctx->extradata[1];
  95. s->first_color[1] = avctx->extradata[2];
  96. if (s->num_pal_colors + s->first_color[0] > 256 ||
  97. s->num_pal_colors + s->first_color[1] > 256) {
  98. av_log(avctx, AV_LOG_ERROR,
  99. "Palette parameters invalid, header probably corrupt\n");
  100. return AVERROR_INVALIDDATA;
  101. }
  102. s->frame = av_frame_alloc();
  103. if (!s->frame)
  104. return AVERROR(ENOMEM);
  105. return 0;
  106. }
  107. /**
  108. * Paint a macroblock using the pattern in paint_lut.
  109. * @param s codec context
  110. * @param tag the tag that was in the nibble
  111. */
  112. static int yop_paint_block(YopDecContext *s, int linesize, int tag)
  113. {
  114. if (s->src_end - s->srcptr < paint_lut[tag][3]) {
  115. av_log(s->avctx, AV_LOG_ERROR, "Packet too small.\n");
  116. return AVERROR_INVALIDDATA;
  117. }
  118. s->dstptr[0] = s->srcptr[0];
  119. s->dstptr[1] = s->srcptr[paint_lut[tag][0]];
  120. s->dstptr[linesize] = s->srcptr[paint_lut[tag][1]];
  121. s->dstptr[linesize + 1] = s->srcptr[paint_lut[tag][2]];
  122. // The number of src bytes consumed is in the last part of the lut entry.
  123. s->srcptr += paint_lut[tag][3];
  124. return 0;
  125. }
  126. /**
  127. * Copy a previously painted macroblock to the current_block.
  128. * @param copy_tag the tag that was in the nibble
  129. */
  130. static int yop_copy_previous_block(YopDecContext *s, int linesize, int copy_tag)
  131. {
  132. uint8_t *bufptr;
  133. // Calculate position for the copy source
  134. bufptr = s->dstptr + motion_vector[copy_tag][0] +
  135. linesize * motion_vector[copy_tag][1];
  136. if (bufptr < s->dstbuf) {
  137. av_log(s->avctx, AV_LOG_ERROR, "File probably corrupt\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. s->dstptr[0] = bufptr[0];
  141. s->dstptr[1] = bufptr[1];
  142. s->dstptr[linesize] = bufptr[linesize];
  143. s->dstptr[linesize + 1] = bufptr[linesize + 1];
  144. return 0;
  145. }
  146. /**
  147. * Return the next nibble in sequence, consuming a new byte on the input
  148. * only if necessary.
  149. */
  150. static uint8_t yop_get_next_nibble(YopDecContext *s)
  151. {
  152. int ret;
  153. if (s->low_nibble) {
  154. ret = *s->low_nibble & 0xf;
  155. s->low_nibble = NULL;
  156. }else {
  157. s->low_nibble = s->srcptr++;
  158. ret = *s->low_nibble >> 4;
  159. }
  160. return ret;
  161. }
  162. static int yop_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  163. AVPacket *avpkt)
  164. {
  165. YopDecContext *s = avctx->priv_data;
  166. AVFrame *frame = s->frame;
  167. int tag, firstcolor, is_odd_frame;
  168. int ret, i, x, y;
  169. uint32_t *palette;
  170. if (avpkt->size < 4 + 3 * s->num_pal_colors) {
  171. av_log(avctx, AV_LOG_ERROR, "Packet too small.\n");
  172. return AVERROR_INVALIDDATA;
  173. }
  174. if ((ret = ff_reget_buffer(avctx, frame)) < 0)
  175. return ret;
  176. if (!avctx->frame_number)
  177. memset(frame->data[1], 0, AVPALETTE_SIZE);
  178. s->dstbuf = frame->data[0];
  179. s->dstptr = frame->data[0];
  180. s->srcptr = avpkt->data + 4;
  181. s->src_end = avpkt->data + avpkt->size;
  182. s->low_nibble = NULL;
  183. is_odd_frame = avpkt->data[0];
  184. if(is_odd_frame>1){
  185. av_log(avctx, AV_LOG_ERROR, "frame is too odd %d\n", is_odd_frame);
  186. return AVERROR_INVALIDDATA;
  187. }
  188. firstcolor = s->first_color[is_odd_frame];
  189. palette = (uint32_t *)frame->data[1];
  190. for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3) {
  191. palette[i + firstcolor] = (s->srcptr[0] << 18) |
  192. (s->srcptr[1] << 10) |
  193. (s->srcptr[2] << 2);
  194. palette[i + firstcolor] |= 0xFFU << 24 |
  195. (palette[i + firstcolor] >> 6) & 0x30303;
  196. }
  197. frame->palette_has_changed = 1;
  198. for (y = 0; y < avctx->height; y += 2) {
  199. for (x = 0; x < avctx->width; x += 2) {
  200. if (s->srcptr - avpkt->data >= avpkt->size) {
  201. av_log(avctx, AV_LOG_ERROR, "Packet too small.\n");
  202. return AVERROR_INVALIDDATA;
  203. }
  204. tag = yop_get_next_nibble(s);
  205. if (tag != 0xf) {
  206. ret = yop_paint_block(s, frame->linesize[0], tag);
  207. if (ret < 0)
  208. return ret;
  209. } else {
  210. tag = yop_get_next_nibble(s);
  211. ret = yop_copy_previous_block(s, frame->linesize[0], tag);
  212. if (ret < 0)
  213. return ret;
  214. }
  215. s->dstptr += 2;
  216. }
  217. s->dstptr += 2*frame->linesize[0] - x;
  218. }
  219. if ((ret = av_frame_ref(data, s->frame)) < 0)
  220. return ret;
  221. *got_frame = 1;
  222. return avpkt->size;
  223. }
  224. AVCodec ff_yop_decoder = {
  225. .name = "yop",
  226. .type = AVMEDIA_TYPE_VIDEO,
  227. .id = AV_CODEC_ID_YOP,
  228. .priv_data_size = sizeof(YopDecContext),
  229. .init = yop_decode_init,
  230. .close = yop_decode_close,
  231. .decode = yop_decode_frame,
  232. .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
  233. };