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.

277 lines
8.5KB

  1. /*
  2. * Flash Screen Video decoder
  3. * Copyright (C) 2004 Alex Beregszaszi
  4. * Copyright (C) 2006 Benjamin Larsson
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file flashsv.c
  24. * Flash Screen Video decoder
  25. * @author Alex Beregszaszi
  26. * @author Benjamin Larsson
  27. */
  28. /* Bitstream description
  29. * The picture is divided into blocks that are zlib compressed.
  30. *
  31. * The decoder is fed complete frames, the frameheader contains:
  32. * 4bits of block width
  33. * 12bits of frame width
  34. * 4bits of block height
  35. * 12bits of frame height
  36. *
  37. * Directly after the header are the compressed blocks. The blocks
  38. * have their compressed size represented with 16bits in the beginnig.
  39. * If the size = 0 then the block is unchanged from the previous frame.
  40. * All blocks are decompressed until the buffer is consumed.
  41. *
  42. * Encoding ideas, a basic encoder would just use a fixed block size.
  43. * Block sizes can be multipels of 16, from 16 to 256. The blocks don't
  44. * have to be quadratic. A brute force search with a set of diffrent
  45. * block sizes should give a better result then to just use a fixed size.
  46. */
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include "common.h"
  50. #include "avcodec.h"
  51. #include "bitstream.h"
  52. #ifdef CONFIG_ZLIB
  53. #include <zlib.h>
  54. #endif
  55. typedef struct FlashSVContext {
  56. AVCodecContext *avctx;
  57. AVFrame frame;
  58. int image_width, image_height;
  59. int block_width, block_height;
  60. uint8_t* tmpblock;
  61. int block_size;
  62. #ifdef CONFIG_ZLIB
  63. z_stream zstream;
  64. #endif
  65. } FlashSVContext;
  66. static void copy_region(uint8_t *sptr, uint8_t *dptr,
  67. int dx, int dy, int h, int w, int stride)
  68. {
  69. int i;
  70. for (i = dx+h; i > dx; i--)
  71. {
  72. memcpy(dptr+(i*stride)+dy*3, sptr, w*3);
  73. sptr += w*3;
  74. }
  75. }
  76. static int flashsv_decode_init(AVCodecContext *avctx)
  77. {
  78. FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
  79. int zret; // Zlib return code
  80. s->avctx = avctx;
  81. #ifdef CONFIG_ZLIB
  82. s->zstream.zalloc = Z_NULL;
  83. s->zstream.zfree = Z_NULL;
  84. s->zstream.opaque = Z_NULL;
  85. zret = inflateInit(&(s->zstream));
  86. if (zret != Z_OK) {
  87. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  88. return 1;
  89. }
  90. #else
  91. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled. Needed for the decoder.\n");
  92. return 1;
  93. #endif
  94. avctx->pix_fmt = PIX_FMT_BGR24;
  95. avctx->has_b_frames = 0;
  96. s->frame.data[0] = NULL;
  97. return 0;
  98. }
  99. static int flashsv_decode_frame(AVCodecContext *avctx,
  100. void *data, int *data_size,
  101. uint8_t *buf, int buf_size)
  102. {
  103. FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
  104. int h_blocks, v_blocks, h_part, v_part, i, j;
  105. GetBitContext gb;
  106. /* no supplementary picture */
  107. if (buf_size == 0)
  108. return 0;
  109. if(s->frame.data[0])
  110. avctx->release_buffer(avctx, &s->frame);
  111. init_get_bits(&gb, buf, buf_size * 8);
  112. /* start to parse the bitstream */
  113. s->block_width = 16* (get_bits(&gb, 4)+1);
  114. s->image_width = get_bits(&gb,12);
  115. s->block_height= 16* (get_bits(&gb, 4)+1);
  116. s->image_height= get_bits(&gb,12);
  117. /* calculate amount of blocks and the size of the border blocks */
  118. h_blocks = s->image_width / s->block_width;
  119. h_part = s->image_width % s->block_width;
  120. v_blocks = s->image_height / s->block_height;
  121. v_part = s->image_height % s->block_height;
  122. /* the block size could change between frames, make sure the buffer
  123. * is large enough, if not, get a larger one */
  124. if(s->block_size < s->block_width*s->block_height) {
  125. if (s->tmpblock != NULL)
  126. av_free(s->tmpblock);
  127. s->block_size = s->block_width*s->block_height;
  128. if ((s->tmpblock = av_malloc(3*s->block_size)) == NULL) {
  129. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  130. return -1;
  131. }
  132. }
  133. /* init the image size once */
  134. if((avctx->width==0) && (avctx->height==0)){
  135. avctx->width = s->image_width;
  136. avctx->height = s->image_height;
  137. }
  138. /* check for changes of image width and image height */
  139. if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) {
  140. av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n");
  141. av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",avctx->height,
  142. avctx->width,s->image_height,s->image_width);
  143. return -1;
  144. }
  145. av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
  146. s->image_width, s->image_height, s->block_width, s->block_height,
  147. h_blocks, v_blocks, h_part, v_part);
  148. s->frame.reference = 1;
  149. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  150. if (avctx->get_buffer(avctx, &s->frame) < 0) {
  151. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  152. return -1;
  153. }
  154. /* loop over all block columns */
  155. for (j = 0; j < v_blocks + (v_part?1:0); j++)
  156. {
  157. int hp = j*s->block_height; // horiz position in frame
  158. int hs = (j<v_blocks)?s->block_height:v_part; // size of block
  159. /* loop over all block rows */
  160. for (i = 0; i < h_blocks + (h_part?1:0); i++)
  161. {
  162. int wp = i*s->block_width; // vert position in frame
  163. int ws = (i<h_blocks)?s->block_width:h_part; // size of block
  164. /* get the size of the compressed zlib chunk */
  165. int size = get_bits(&gb, 16);
  166. if (size == 0) {
  167. /* no change, don't do anything */
  168. } else {
  169. /* decompress block */
  170. #ifdef CONFIG_ZLIB
  171. int ret = inflateReset(&(s->zstream));
  172. if (ret != Z_OK)
  173. {
  174. av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
  175. /* return -1; */
  176. }
  177. s->zstream.next_in = buf+(get_bits_count(&gb)/8);
  178. s->zstream.avail_in = size;
  179. s->zstream.next_out = s->tmpblock;
  180. s->zstream.avail_out = s->block_size*3;
  181. ret = inflate(&(s->zstream), Z_FINISH);
  182. if (ret == Z_DATA_ERROR)
  183. {
  184. av_log(avctx, AV_LOG_ERROR, "Zlib resync occured\n");
  185. inflateSync(&(s->zstream));
  186. ret = inflate(&(s->zstream), Z_FINISH);
  187. }
  188. if ((ret != Z_OK) && (ret != Z_STREAM_END))
  189. {
  190. av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret);
  191. /* return -1; */
  192. }
  193. #else
  194. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
  195. return -1;
  196. #endif
  197. copy_region(s->tmpblock, s->frame.data[0], s->image_height-(hp+hs+1), wp, hs, ws, s->frame.linesize[0]);
  198. skip_bits(&gb, 8*size); /* skip the consumed bits */
  199. }
  200. }
  201. }
  202. *data_size = sizeof(AVFrame);
  203. *(AVFrame*)data = s->frame;
  204. if ((get_bits_count(&gb)/8) != buf_size)
  205. av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
  206. buf_size, (get_bits_count(&gb)/8));
  207. /* report that the buffer was completely consumed */
  208. return buf_size;
  209. }
  210. static int flashsv_decode_end(AVCodecContext *avctx)
  211. {
  212. FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
  213. #ifdef CONFIG_ZLIB
  214. inflateEnd(&(s->zstream));
  215. #endif
  216. /* release the frame if needed */
  217. if (s->frame.data[0])
  218. avctx->release_buffer(avctx, &s->frame);
  219. /* free the tmpblock */
  220. if (s->tmpblock != NULL)
  221. av_free(s->tmpblock);
  222. return 0;
  223. }
  224. AVCodec flashsv_decoder = {
  225. "flashsv",
  226. CODEC_TYPE_VIDEO,
  227. CODEC_ID_FLASHSV,
  228. sizeof(FlashSVContext),
  229. flashsv_decode_init,
  230. NULL,
  231. flashsv_decode_end,
  232. flashsv_decode_frame,
  233. CODEC_CAP_DR1,
  234. .pix_fmts = (enum PixelFormat[]){PIX_FMT_BGR24, -1},
  235. };