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.

257 lines
8.7KB

  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 <zlib.h>
  50. #include "avcodec.h"
  51. #include "get_bits.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. memcpy(dptr + (i * stride) + dy * 3, sptr, w * 3);
  67. sptr += w * 3;
  68. }
  69. }
  70. static av_cold int flashsv_decode_init(AVCodecContext *avctx)
  71. {
  72. FlashSVContext *s = avctx->priv_data;
  73. int zret; // Zlib return code
  74. s->avctx = avctx;
  75. s->zstream.zalloc = Z_NULL;
  76. s->zstream.zfree = Z_NULL;
  77. s->zstream.opaque = Z_NULL;
  78. zret = inflateInit(&(s->zstream));
  79. if (zret != Z_OK) {
  80. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  81. return 1;
  82. }
  83. avctx->pix_fmt = PIX_FMT_BGR24;
  84. avcodec_get_frame_defaults(&s->frame);
  85. s->frame.data[0] = NULL;
  86. return 0;
  87. }
  88. static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
  89. int *data_size, AVPacket *avpkt)
  90. {
  91. const uint8_t *buf = avpkt->data;
  92. int buf_size = avpkt->size;
  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 (buf_size < 4)
  100. return -1;
  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. 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 AVERROR(ENOMEM);
  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 | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  139. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  140. av_log(avctx, AV_LOG_ERROR, "reget_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. int hp = j * s->block_height; // horiz position in frame
  146. int hs = (j < v_blocks) ? s->block_height : v_part; // size of block
  147. /* loop over all block rows */
  148. for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
  149. int wp = i * s->block_width; // vert position in frame
  150. int ws = (i < h_blocks) ? s->block_width : h_part; // size of block
  151. /* get the size of the compressed zlib chunk */
  152. int size = get_bits(&gb, 16);
  153. if (8 * size > get_bits_left(&gb)) {
  154. avctx->release_buffer(avctx, &s->frame);
  155. s->frame.data[0] = NULL;
  156. return -1;
  157. }
  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. av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
  165. /* return -1; */
  166. }
  167. s->zstream.next_in = buf + (get_bits_count(&gb) / 8);
  168. s->zstream.avail_in = size;
  169. s->zstream.next_out = s->tmpblock;
  170. s->zstream.avail_out = s->block_size * 3;
  171. ret = inflate(&(s->zstream), Z_FINISH);
  172. if (ret == Z_DATA_ERROR) {
  173. av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
  174. inflateSync(&(s->zstream));
  175. ret = inflate(&(s->zstream), Z_FINISH);
  176. }
  177. if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
  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),
  182. wp, hs, ws, s->frame.linesize[0]);
  183. skip_bits_long(&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 av_cold 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. av_free(s->tmpblock);
  204. return 0;
  205. }
  206. AVCodec ff_flashsv_decoder = {
  207. .name = "flashsv",
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. .id = CODEC_ID_FLASHSV,
  210. .priv_data_size = sizeof(FlashSVContext),
  211. .init = flashsv_decode_init,
  212. .close = flashsv_decode_end,
  213. .decode = flashsv_decode_frame,
  214. .capabilities = CODEC_CAP_DR1,
  215. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
  216. .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),
  217. };