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.

260 lines
8.1KB

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