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.

236 lines
6.6KB

  1. /*
  2. * Bitmap Brothers JV video decoder
  3. * Copyright (c) 2011 Peter Ross <pross@xvid.org>
  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. * @file
  23. * Bitmap Brothers JV video decoder
  24. * @author Peter Ross <pross@xvid.org>
  25. */
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "blockdsp.h"
  29. #include "get_bits.h"
  30. #include "internal.h"
  31. typedef struct JvContext {
  32. BlockDSPContext bdsp;
  33. AVFrame *frame;
  34. uint32_t palette[AVPALETTE_COUNT];
  35. int palette_has_changed;
  36. } JvContext;
  37. static av_cold int decode_init(AVCodecContext *avctx)
  38. {
  39. JvContext *s = avctx->priv_data;
  40. if (!avctx->width || !avctx->height ||
  41. (avctx->width & 7) || (avctx->height & 7)) {
  42. av_log(avctx, AV_LOG_ERROR, "Invalid video dimensions: %dx%d\n",
  43. avctx->width, avctx->height);
  44. return AVERROR(EINVAL);
  45. }
  46. s->frame = av_frame_alloc();
  47. if (!s->frame)
  48. return AVERROR(ENOMEM);
  49. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  50. ff_blockdsp_init(&s->bdsp, avctx);
  51. return 0;
  52. }
  53. /**
  54. * Decode 2x2 block
  55. */
  56. static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
  57. {
  58. int i, j, v[2];
  59. switch (get_bits(gb, 2)) {
  60. case 1:
  61. v[0] = get_bits(gb, 8);
  62. for (j = 0; j < 2; j++)
  63. memset(dst + j * linesize, v[0], 2);
  64. break;
  65. case 2:
  66. v[0] = get_bits(gb, 8);
  67. v[1] = get_bits(gb, 8);
  68. for (j = 0; j < 2; j++)
  69. for (i = 0; i < 2; i++)
  70. dst[j * linesize + i] = v[get_bits1(gb)];
  71. break;
  72. case 3:
  73. for (j = 0; j < 2; j++)
  74. for (i = 0; i < 2; i++)
  75. dst[j * linesize + i] = get_bits(gb, 8);
  76. }
  77. }
  78. /**
  79. * Decode 4x4 block
  80. */
  81. static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
  82. {
  83. int i, j, v[2];
  84. switch (get_bits(gb, 2)) {
  85. case 1:
  86. v[0] = get_bits(gb, 8);
  87. for (j = 0; j < 4; j++)
  88. memset(dst + j * linesize, v[0], 4);
  89. break;
  90. case 2:
  91. v[0] = get_bits(gb, 8);
  92. v[1] = get_bits(gb, 8);
  93. for (j = 2; j >= 0; j -= 2) {
  94. for (i = 0; i < 4; i++)
  95. dst[j * linesize + i] = v[get_bits1(gb)];
  96. for (i = 0; i < 4; i++)
  97. dst[(j + 1) * linesize + i] = v[get_bits1(gb)];
  98. }
  99. break;
  100. case 3:
  101. for (j = 0; j < 4; j += 2)
  102. for (i = 0; i < 4; i += 2)
  103. decode2x2(gb, dst + j * linesize + i, linesize);
  104. }
  105. }
  106. /**
  107. * Decode 8x8 block
  108. */
  109. static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize,
  110. BlockDSPContext *bdsp)
  111. {
  112. int i, j, v[2];
  113. switch (get_bits(gb, 2)) {
  114. case 1:
  115. v[0] = get_bits(gb, 8);
  116. bdsp->fill_block_tab[1](dst, v[0], linesize, 8);
  117. break;
  118. case 2:
  119. v[0] = get_bits(gb, 8);
  120. v[1] = get_bits(gb, 8);
  121. for (j = 7; j >= 0; j--)
  122. for (i = 0; i < 8; i++)
  123. dst[j * linesize + i] = v[get_bits1(gb)];
  124. break;
  125. case 3:
  126. for (j = 0; j < 8; j += 4)
  127. for (i = 0; i < 8; i += 4)
  128. decode4x4(gb, dst + j * linesize + i, linesize);
  129. }
  130. }
  131. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  132. AVPacket *avpkt)
  133. {
  134. JvContext *s = avctx->priv_data;
  135. const uint8_t *buf = avpkt->data;
  136. const uint8_t *buf_end = buf + avpkt->size;
  137. int video_size, video_type, i, j, ret;
  138. if (avpkt->size < 6)
  139. return AVERROR_INVALIDDATA;
  140. video_size = AV_RL32(buf);
  141. video_type = buf[4];
  142. buf += 5;
  143. if (video_size) {
  144. if (video_size < 0 || video_size > avpkt->size - 5) {
  145. av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
  146. return AVERROR_INVALIDDATA;
  147. }
  148. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  149. return ret;
  150. if (video_type == 0 || video_type == 1) {
  151. GetBitContext gb;
  152. init_get_bits(&gb, buf, 8 * video_size);
  153. for (j = 0; j < avctx->height; j += 8)
  154. for (i = 0; i < avctx->width; i += 8)
  155. decode8x8(&gb,
  156. s->frame->data[0] + j * s->frame->linesize[0] + i,
  157. s->frame->linesize[0], &s->bdsp);
  158. buf += video_size;
  159. } else if (video_type == 2) {
  160. int v = *buf++;
  161. for (j = 0; j < avctx->height; j++)
  162. memset(s->frame->data[0] + j * s->frame->linesize[0],
  163. v, avctx->width);
  164. } else {
  165. av_log(avctx, AV_LOG_WARNING,
  166. "unsupported frame type %i\n", video_type);
  167. return AVERROR_INVALIDDATA;
  168. }
  169. }
  170. if (buf_end - buf >= AVPALETTE_COUNT * 3) {
  171. for (i = 0; i < AVPALETTE_COUNT; i++) {
  172. uint32_t pal = AV_RB24(buf);
  173. s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
  174. buf += 3;
  175. }
  176. s->palette_has_changed = 1;
  177. }
  178. if (video_size) {
  179. s->frame->key_frame = 1;
  180. s->frame->pict_type = AV_PICTURE_TYPE_I;
  181. s->frame->palette_has_changed = s->palette_has_changed;
  182. s->palette_has_changed = 0;
  183. memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
  184. if ((ret = av_frame_ref(data, s->frame)) < 0)
  185. return ret;
  186. *got_frame = 1;
  187. }
  188. return avpkt->size;
  189. }
  190. static av_cold int decode_close(AVCodecContext *avctx)
  191. {
  192. JvContext *s = avctx->priv_data;
  193. av_frame_free(&s->frame);
  194. return 0;
  195. }
  196. AVCodec ff_jv_decoder = {
  197. .name = "jv",
  198. .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
  199. .type = AVMEDIA_TYPE_VIDEO,
  200. .id = AV_CODEC_ID_JV,
  201. .priv_data_size = sizeof(JvContext),
  202. .init = decode_init,
  203. .close = decode_close,
  204. .decode = decode_frame,
  205. .capabilities = AV_CODEC_CAP_DR1,
  206. };