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.

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