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.

226 lines
6.2KB

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