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.

233 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 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 "bitstream.h"
  29. #include "blockdsp.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);
  51. return 0;
  52. }
  53. /**
  54. * Decode 2x2 block
  55. */
  56. static inline void decode2x2(BitstreamContext *bc, uint8_t *dst, int linesize)
  57. {
  58. int i, j, v[2];
  59. switch (bitstream_read(bc, 2)) {
  60. case 1:
  61. v[0] = bitstream_read(bc, 8);
  62. for (j = 0; j < 2; j++)
  63. memset(dst + j * linesize, v[0], 2);
  64. break;
  65. case 2:
  66. v[0] = bitstream_read(bc, 8);
  67. v[1] = bitstream_read(bc, 8);
  68. for (j = 0; j < 2; j++)
  69. for (i = 0; i < 2; i++)
  70. dst[j * linesize + i] = v[bitstream_read_bit(bc)];
  71. break;
  72. case 3:
  73. for (j = 0; j < 2; j++)
  74. for (i = 0; i < 2; i++)
  75. dst[j * linesize + i] = bitstream_read(bc, 8);
  76. }
  77. }
  78. /**
  79. * Decode 4x4 block
  80. */
  81. static inline void decode4x4(BitstreamContext *bc, uint8_t *dst, int linesize)
  82. {
  83. int i, j, v[2];
  84. switch (bitstream_read(bc, 2)) {
  85. case 1:
  86. v[0] = bitstream_read(bc, 8);
  87. for (j = 0; j < 4; j++)
  88. memset(dst + j * linesize, v[0], 4);
  89. break;
  90. case 2:
  91. v[0] = bitstream_read(bc, 8);
  92. v[1] = bitstream_read(bc, 8);
  93. for (j = 2; j >= 0; j -= 2) {
  94. for (i = 0; i < 4; i++)
  95. dst[j * linesize + i] = v[bitstream_read_bit(bc)];
  96. for (i = 0; i < 4; i++)
  97. dst[(j + 1) * linesize + i] = v[bitstream_read_bit(bc)];
  98. }
  99. break;
  100. case 3:
  101. for (j = 0; j < 4; j += 2)
  102. for (i = 0; i < 4; i += 2)
  103. decode2x2(bc, dst + j * linesize + i, linesize);
  104. }
  105. }
  106. /**
  107. * Decode 8x8 block
  108. */
  109. static inline void decode8x8(BitstreamContext *bc, uint8_t *dst, int linesize,
  110. BlockDSPContext *bdsp)
  111. {
  112. int i, j, v[2];
  113. switch (bitstream_read(bc, 2)) {
  114. case 1:
  115. v[0] = bitstream_read(bc, 8);
  116. bdsp->fill_block_tab[1](dst, v[0], linesize, 8);
  117. break;
  118. case 2:
  119. v[0] = bitstream_read(bc, 8);
  120. v[1] = bitstream_read(bc, 8);
  121. for (j = 7; j >= 0; j--)
  122. for (i = 0; i < 8; i++)
  123. dst[j * linesize + i] = v[bitstream_read_bit(bc)];
  124. break;
  125. case 3:
  126. for (j = 0; j < 8; j += 4)
  127. for (i = 0; i < 8; i += 4)
  128. decode4x4(bc, 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. int buf_size = avpkt->size;
  136. const uint8_t *buf = avpkt->data;
  137. const uint8_t *buf_end = buf + buf_size;
  138. int video_size, video_type, i, j, ret;
  139. video_size = AV_RL32(buf);
  140. video_type = buf[4];
  141. buf += 5;
  142. if (video_size) {
  143. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
  144. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  145. return ret;
  146. }
  147. if (video_type == 0 || video_type == 1) {
  148. BitstreamContext bc;
  149. bitstream_init8(&bc, buf, FFMIN(video_size, buf_end - buf));
  150. for (j = 0; j < avctx->height; j += 8)
  151. for (i = 0; i < avctx->width; i += 8)
  152. decode8x8(&bc,
  153. s->frame->data[0] + j * s->frame->linesize[0] + i,
  154. s->frame->linesize[0], &s->bdsp);
  155. buf += video_size;
  156. } else if (video_type == 2) {
  157. if (buf + 1 <= buf_end) {
  158. int v = *buf++;
  159. for (j = 0; j < avctx->height; j++)
  160. memset(s->frame->data[0] + j * s->frame->linesize[0],
  161. v, avctx->width);
  162. }
  163. } else {
  164. av_log(avctx, AV_LOG_WARNING,
  165. "unsupported frame type %i\n", video_type);
  166. return AVERROR_INVALIDDATA;
  167. }
  168. }
  169. if (buf < buf_end) {
  170. for (i = 0; i < AVPALETTE_COUNT && buf + 3 <= buf_end; i++) {
  171. s->palette[i] = AV_RB24(buf) << 2;
  172. buf += 3;
  173. }
  174. s->palette_has_changed = 1;
  175. }
  176. if (video_size) {
  177. s->frame->key_frame = 1;
  178. s->frame->pict_type = AV_PICTURE_TYPE_I;
  179. s->frame->palette_has_changed = s->palette_has_changed;
  180. s->palette_has_changed = 0;
  181. memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
  182. if ((ret = av_frame_ref(data, s->frame)) < 0)
  183. return ret;
  184. *got_frame = 1;
  185. }
  186. return buf_size;
  187. }
  188. static av_cold int decode_close(AVCodecContext *avctx)
  189. {
  190. JvContext *s = avctx->priv_data;
  191. av_frame_free(&s->frame);
  192. return 0;
  193. }
  194. AVCodec ff_jv_decoder = {
  195. .name = "jv",
  196. .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
  197. .type = AVMEDIA_TYPE_VIDEO,
  198. .id = AV_CODEC_ID_JV,
  199. .priv_data_size = sizeof(JvContext),
  200. .init = decode_init,
  201. .close = decode_close,
  202. .decode = decode_frame,
  203. .capabilities = AV_CODEC_CAP_DR1,
  204. };