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.

216 lines
6.0KB

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