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.

277 lines
8.2KB

  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. AVFrame frame;
  31. AVCodecContext *avctx;
  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_init(AVCodecContext *avctx)
  74. {
  75. YopDecContext *s = avctx->priv_data;
  76. s->avctx = avctx;
  77. if (avctx->width & 1 || avctx->height & 1 ||
  78. av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
  79. av_log(avctx, AV_LOG_ERROR, "YOP has invalid dimensions\n");
  80. return AVERROR_INVALIDDATA;
  81. }
  82. if (avctx->extradata_size < 3) {
  83. av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata.\n");
  84. return AVERROR_INVALIDDATA;
  85. }
  86. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  87. avcodec_get_frame_defaults(&s->frame);
  88. s->num_pal_colors = avctx->extradata[0];
  89. s->first_color[0] = avctx->extradata[1];
  90. s->first_color[1] = avctx->extradata[2];
  91. if (s->num_pal_colors + s->first_color[0] > 256 ||
  92. s->num_pal_colors + s->first_color[1] > 256) {
  93. av_log(avctx, AV_LOG_ERROR,
  94. "Palette parameters invalid, header probably corrupt\n");
  95. return AVERROR_INVALIDDATA;
  96. }
  97. return 0;
  98. }
  99. static av_cold int yop_decode_close(AVCodecContext *avctx)
  100. {
  101. YopDecContext *s = avctx->priv_data;
  102. if (s->frame.data[0])
  103. avctx->release_buffer(avctx, &s->frame);
  104. return 0;
  105. }
  106. /**
  107. * Paint a macroblock using the pattern in paint_lut.
  108. * @param s codec context
  109. * @param tag the tag that was in the nibble
  110. */
  111. static int yop_paint_block(YopDecContext *s, int tag)
  112. {
  113. if (s->src_end - s->srcptr < paint_lut[tag][3]) {
  114. av_log(s->avctx, AV_LOG_ERROR, "Packet too small.\n");
  115. return AVERROR_INVALIDDATA;
  116. }
  117. s->dstptr[0] = s->srcptr[0];
  118. s->dstptr[1] = s->srcptr[paint_lut[tag][0]];
  119. s->dstptr[s->frame.linesize[0]] = s->srcptr[paint_lut[tag][1]];
  120. s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]];
  121. // The number of src bytes consumed is in the last part of the lut entry.
  122. s->srcptr += paint_lut[tag][3];
  123. return 0;
  124. }
  125. /**
  126. * Copy a previously painted macroblock to the current_block.
  127. * @param copy_tag the tag that was in the nibble
  128. */
  129. static int yop_copy_previous_block(YopDecContext *s, int copy_tag)
  130. {
  131. uint8_t *bufptr;
  132. // Calculate position for the copy source
  133. bufptr = s->dstptr + motion_vector[copy_tag][0] +
  134. s->frame.linesize[0] * motion_vector[copy_tag][1];
  135. if (bufptr < s->dstbuf) {
  136. av_log(s->avctx, AV_LOG_ERROR, "File probably corrupt\n");
  137. return AVERROR_INVALIDDATA;
  138. }
  139. s->dstptr[0] = bufptr[0];
  140. s->dstptr[1] = bufptr[1];
  141. s->dstptr[s->frame.linesize[0]] = bufptr[s->frame.linesize[0]];
  142. s->dstptr[s->frame.linesize[0] + 1] = bufptr[s->frame.linesize[0] + 1];
  143. return 0;
  144. }
  145. /**
  146. * Return the next nibble in sequence, consuming a new byte on the input
  147. * only if necessary.
  148. */
  149. static uint8_t yop_get_next_nibble(YopDecContext *s)
  150. {
  151. int ret;
  152. if (s->low_nibble) {
  153. ret = *s->low_nibble & 0xf;
  154. s->low_nibble = NULL;
  155. }else {
  156. s->low_nibble = s->srcptr++;
  157. ret = *s->low_nibble >> 4;
  158. }
  159. return ret;
  160. }
  161. static int yop_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  162. AVPacket *avpkt)
  163. {
  164. YopDecContext *s = avctx->priv_data;
  165. int tag, firstcolor, is_odd_frame;
  166. int ret, i, x, y;
  167. uint32_t *palette;
  168. if (avpkt->size < 4 + 3 * s->num_pal_colors) {
  169. av_log(avctx, AV_LOG_ERROR, "Packet too small.\n");
  170. return AVERROR_INVALIDDATA;
  171. }
  172. if (s->frame.data[0])
  173. avctx->release_buffer(avctx, &s->frame);
  174. ret = ff_get_buffer(avctx, &s->frame);
  175. if (ret < 0) {
  176. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  177. return ret;
  178. }
  179. if (!avctx->frame_number)
  180. memset(s->frame.data[1], 0, AVPALETTE_SIZE);
  181. s->dstbuf = s->frame.data[0];
  182. s->dstptr = s->frame.data[0];
  183. s->srcptr = avpkt->data + 4;
  184. s->src_end = avpkt->data + avpkt->size;
  185. s->low_nibble = NULL;
  186. is_odd_frame = avpkt->data[0];
  187. if(is_odd_frame>1){
  188. av_log(avctx, AV_LOG_ERROR, "frame is too odd %d\n", is_odd_frame);
  189. return AVERROR_INVALIDDATA;
  190. }
  191. firstcolor = s->first_color[is_odd_frame];
  192. palette = (uint32_t *)s->frame.data[1];
  193. for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3) {
  194. palette[i + firstcolor] = (s->srcptr[0] << 18) |
  195. (s->srcptr[1] << 10) |
  196. (s->srcptr[2] << 2);
  197. palette[i + firstcolor] |= 0xFFU << 24 |
  198. (palette[i + firstcolor] >> 6) & 0x30303;
  199. }
  200. s->frame.palette_has_changed = 1;
  201. for (y = 0; y < avctx->height; y += 2) {
  202. for (x = 0; x < avctx->width; x += 2) {
  203. if (s->srcptr - avpkt->data >= avpkt->size) {
  204. av_log(avctx, AV_LOG_ERROR, "Packet too small.\n");
  205. return AVERROR_INVALIDDATA;
  206. }
  207. tag = yop_get_next_nibble(s);
  208. if (tag != 0xf) {
  209. ret = yop_paint_block(s, tag);
  210. if (ret < 0)
  211. return ret;
  212. } else {
  213. tag = yop_get_next_nibble(s);
  214. ret = yop_copy_previous_block(s, tag);
  215. if (ret < 0) {
  216. avctx->release_buffer(avctx, &s->frame);
  217. return ret;
  218. }
  219. }
  220. s->dstptr += 2;
  221. }
  222. s->dstptr += 2*s->frame.linesize[0] - x;
  223. }
  224. *got_frame = 1;
  225. *(AVFrame *) data = s->frame;
  226. return avpkt->size;
  227. }
  228. AVCodec ff_yop_decoder = {
  229. .name = "yop",
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .id = AV_CODEC_ID_YOP,
  232. .priv_data_size = sizeof(YopDecContext),
  233. .init = yop_decode_init,
  234. .close = yop_decode_close,
  235. .decode = yop_decode_frame,
  236. .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
  237. };