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.

266 lines
8.4KB

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