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.

262 lines
8.2KB

  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. #ifdef CONFIG_ZLIB
  50. #include <zlib.h>
  51. #include "common.h"
  52. #include "avcodec.h"
  53. #include "bitstream.h"
  54. typedef struct FlashSVContext {
  55. AVCodecContext *avctx;
  56. AVFrame frame;
  57. int image_width, image_height;
  58. int block_width, block_height;
  59. uint8_t* tmpblock;
  60. int block_size;
  61. z_stream zstream;
  62. } FlashSVContext;
  63. static void copy_region(uint8_t *sptr, uint8_t *dptr,
  64. int dx, int dy, int h, int w, int stride)
  65. {
  66. int i;
  67. for (i = dx+h; i > dx; i--)
  68. {
  69. memcpy(dptr+(i*stride)+dy*3, sptr, w*3);
  70. sptr += w*3;
  71. }
  72. }
  73. static int flashsv_decode_init(AVCodecContext *avctx)
  74. {
  75. FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
  76. int zret; // Zlib return code
  77. s->avctx = avctx;
  78. s->zstream.zalloc = Z_NULL;
  79. s->zstream.zfree = Z_NULL;
  80. s->zstream.opaque = Z_NULL;
  81. zret = inflateInit(&(s->zstream));
  82. if (zret != Z_OK) {
  83. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  84. return 1;
  85. }
  86. avctx->pix_fmt = PIX_FMT_BGR24;
  87. avctx->has_b_frames = 0;
  88. s->frame.data[0] = NULL;
  89. return 0;
  90. }
  91. static int flashsv_decode_frame(AVCodecContext *avctx,
  92. void *data, int *data_size,
  93. uint8_t *buf, int buf_size)
  94. {
  95. FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
  96. int h_blocks, v_blocks, h_part, v_part, i, j;
  97. GetBitContext gb;
  98. /* no supplementary picture */
  99. if (buf_size == 0)
  100. return 0;
  101. if(s->frame.data[0])
  102. avctx->release_buffer(avctx, &s->frame);
  103. init_get_bits(&gb, buf, buf_size * 8);
  104. /* start to parse the bitstream */
  105. s->block_width = 16* (get_bits(&gb, 4)+1);
  106. s->image_width = get_bits(&gb,12);
  107. s->block_height= 16* (get_bits(&gb, 4)+1);
  108. s->image_height= get_bits(&gb,12);
  109. /* calculate amount of blocks and the size of the border blocks */
  110. h_blocks = s->image_width / s->block_width;
  111. h_part = s->image_width % s->block_width;
  112. v_blocks = s->image_height / s->block_height;
  113. v_part = s->image_height % s->block_height;
  114. /* the block size could change between frames, make sure the buffer
  115. * is large enough, if not, get a larger one */
  116. if(s->block_size < s->block_width*s->block_height) {
  117. if (s->tmpblock != NULL)
  118. av_free(s->tmpblock);
  119. if ((s->tmpblock = av_malloc(3*s->block_width*s->block_height)) == NULL) {
  120. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  121. return -1;
  122. }
  123. }
  124. s->block_size = s->block_width*s->block_height;
  125. /* init the image size once */
  126. if((avctx->width==0) && (avctx->height==0)){
  127. avctx->width = s->image_width;
  128. avctx->height = s->image_height;
  129. }
  130. /* check for changes of image width and image height */
  131. if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) {
  132. av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n");
  133. av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",avctx->height,
  134. avctx->width,s->image_height,s->image_width);
  135. return -1;
  136. }
  137. av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
  138. s->image_width, s->image_height, s->block_width, s->block_height,
  139. h_blocks, v_blocks, h_part, v_part);
  140. s->frame.reference = 1;
  141. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  142. if (avctx->get_buffer(avctx, &s->frame) < 0) {
  143. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  144. return -1;
  145. }
  146. /* loop over all block columns */
  147. for (j = 0; j < v_blocks + (v_part?1:0); j++)
  148. {
  149. int hp = j*s->block_height; // horiz position in frame
  150. int hs = (j<v_blocks)?s->block_height:v_part; // size of block
  151. /* loop over all block rows */
  152. for (i = 0; i < h_blocks + (h_part?1:0); i++)
  153. {
  154. int wp = i*s->block_width; // vert position in frame
  155. int ws = (i<h_blocks)?s->block_width:h_part; // size of block
  156. /* get the size of the compressed zlib chunk */
  157. int size = get_bits(&gb, 16);
  158. if (size == 0) {
  159. /* no change, don't do anything */
  160. } else {
  161. /* decompress block */
  162. int ret = inflateReset(&(s->zstream));
  163. if (ret != Z_OK)
  164. {
  165. av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
  166. /* return -1; */
  167. }
  168. s->zstream.next_in = buf+(get_bits_count(&gb)/8);
  169. s->zstream.avail_in = size;
  170. s->zstream.next_out = s->tmpblock;
  171. s->zstream.avail_out = s->block_size*3;
  172. ret = inflate(&(s->zstream), Z_FINISH);
  173. if (ret == Z_DATA_ERROR)
  174. {
  175. av_log(avctx, AV_LOG_ERROR, "Zlib resync occured\n");
  176. inflateSync(&(s->zstream));
  177. ret = inflate(&(s->zstream), Z_FINISH);
  178. }
  179. if ((ret != Z_OK) && (ret != Z_STREAM_END))
  180. {
  181. av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret);
  182. /* return -1; */
  183. }
  184. copy_region(s->tmpblock, s->frame.data[0], s->image_height-(hp+hs+1), wp, hs, ws, s->frame.linesize[0]);
  185. skip_bits(&gb, 8*size); /* skip the consumed bits */
  186. }
  187. }
  188. }
  189. *data_size = sizeof(AVFrame);
  190. *(AVFrame*)data = s->frame;
  191. if ((get_bits_count(&gb)/8) != buf_size)
  192. av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
  193. buf_size, (get_bits_count(&gb)/8));
  194. /* report that the buffer was completely consumed */
  195. return buf_size;
  196. }
  197. static int flashsv_decode_end(AVCodecContext *avctx)
  198. {
  199. FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
  200. inflateEnd(&(s->zstream));
  201. /* release the frame if needed */
  202. if (s->frame.data[0])
  203. avctx->release_buffer(avctx, &s->frame);
  204. /* free the tmpblock */
  205. if (s->tmpblock != NULL)
  206. av_free(s->tmpblock);
  207. return 0;
  208. }
  209. AVCodec flashsv_decoder = {
  210. "flashsv",
  211. CODEC_TYPE_VIDEO,
  212. CODEC_ID_FLASHSV,
  213. sizeof(FlashSVContext),
  214. flashsv_decode_init,
  215. NULL,
  216. flashsv_decode_end,
  217. flashsv_decode_frame,
  218. CODEC_CAP_DR1,
  219. .pix_fmts = (enum PixelFormat[]){PIX_FMT_BGR24, -1},
  220. };
  221. #endif //CONFIG_ZLIB