flashsv.c 8.2 KB

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