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.

302 lines
9.0KB

  1. /*
  2. * Flash Screen Video encoder
  3. * Copyright (C) 2004 Alex Beregszaszi
  4. * Copyright (C) 2006 Benjamin Larsson
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /* Encoding development sponsored by http://fh-campuswien.ac.at */
  23. /**
  24. * @file
  25. * Flash Screen Video encoder
  26. * @author Alex Beregszaszi
  27. * @author Benjamin Larsson
  28. */
  29. /* Bitstream description
  30. * The picture is divided into blocks that are zlib-compressed.
  31. *
  32. * The decoder is fed complete frames, the frameheader contains:
  33. * 4bits of block width
  34. * 12bits of frame width
  35. * 4bits of block height
  36. * 12bits of frame height
  37. *
  38. * Directly after the header are the compressed blocks. The blocks
  39. * have their compressed size represented with 16bits in the beginig.
  40. * If the size = 0 then the block is unchanged from the previous frame.
  41. * All blocks are decompressed until the buffer is consumed.
  42. *
  43. * Encoding ideas, a basic encoder would just use a fixed block size.
  44. * Block sizes can be multipels of 16, from 16 to 256. The blocks don't
  45. * have to be quadratic. A brute force search with a set of different
  46. * block sizes should give a better result than to just use a fixed size.
  47. */
  48. /* TODO:
  49. * Don't reencode the frame in brute force mode if the frame is a dupe. Speed up.
  50. * Make the difference check faster.
  51. */
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <zlib.h>
  55. #include "avcodec.h"
  56. #include "put_bits.h"
  57. #include "bytestream.h"
  58. typedef struct FlashSVContext {
  59. AVCodecContext *avctx;
  60. uint8_t *previous_frame;
  61. AVFrame frame;
  62. int image_width, image_height;
  63. int block_width, block_height;
  64. uint8_t *tmpblock;
  65. uint8_t *encbuffer;
  66. int block_size;
  67. z_stream zstream;
  68. int last_key_frame;
  69. } FlashSVContext;
  70. static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
  71. int h, int w, int stride, uint8_t *pfptr)
  72. {
  73. int i, j;
  74. uint8_t *nsptr;
  75. uint8_t *npfptr;
  76. int diff = 0;
  77. for (i = dx + h; i > dx; i--) {
  78. nsptr = sptr + (i * stride) + dy * 3;
  79. npfptr = pfptr + (i * stride) + dy * 3;
  80. for (j = 0; j < w * 3; j++) {
  81. diff |= npfptr[j] ^ nsptr[j];
  82. dptr[j] = nsptr[j];
  83. }
  84. dptr += w * 3;
  85. }
  86. if (diff)
  87. return 1;
  88. return 0;
  89. }
  90. static av_cold int flashsv_encode_init(AVCodecContext *avctx)
  91. {
  92. FlashSVContext *s = avctx->priv_data;
  93. s->avctx = avctx;
  94. if ((avctx->width > 4095) || (avctx->height > 4095)) {
  95. av_log(avctx, AV_LOG_ERROR, "Input dimensions too large, input must be max 4096x4096 !\n");
  96. return AVERROR_INVALIDDATA;
  97. }
  98. // Needed if zlib unused or init aborted before deflateInit
  99. memset(&(s->zstream), 0, sizeof(z_stream));
  100. s->last_key_frame = 0;
  101. s->image_width = avctx->width;
  102. s->image_height = avctx->height;
  103. s->tmpblock = av_mallocz(3 * 256 * 256);
  104. s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
  105. if (!s->tmpblock || !s->encbuffer) {
  106. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  107. return AVERROR(ENOMEM);
  108. }
  109. return 0;
  110. }
  111. static int encode_bitstream(FlashSVContext *s, AVFrame *p, uint8_t *buf,
  112. int buf_size, int block_width, int block_height,
  113. uint8_t *previous_frame, int *I_frame)
  114. {
  115. PutBitContext pb;
  116. int h_blocks, v_blocks, h_part, v_part, i, j;
  117. int buf_pos, res;
  118. int pred_blocks = 0;
  119. init_put_bits(&pb, buf, buf_size * 8);
  120. put_bits(&pb, 4, (block_width / 16) - 1);
  121. put_bits(&pb, 12, s->image_width);
  122. put_bits(&pb, 4, (block_height / 16) - 1);
  123. put_bits(&pb, 12, s->image_height);
  124. flush_put_bits(&pb);
  125. buf_pos = 4;
  126. h_blocks = s->image_width / block_width;
  127. h_part = s->image_width % block_width;
  128. v_blocks = s->image_height / block_height;
  129. v_part = s->image_height % block_height;
  130. /* loop over all block columns */
  131. for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
  132. int hp = j * block_height; // horiz position in frame
  133. int hs = (j < v_blocks) ? block_height : v_part; // size of block
  134. /* loop over all block rows */
  135. for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
  136. int wp = i * block_width; // vert position in frame
  137. int ws = (i < h_blocks) ? block_width : h_part; // size of block
  138. int ret = Z_OK;
  139. uint8_t *ptr;
  140. ptr = buf + buf_pos;
  141. /* copy the block to the temp buffer before compression
  142. * (if it differs from the previous frame's block) */
  143. res = copy_region_enc(p->data[0], s->tmpblock,
  144. s->image_height - (hp + hs + 1),
  145. wp, hs, ws, p->linesize[0], previous_frame);
  146. if (res || *I_frame) {
  147. unsigned long zsize;
  148. zsize = 3 * block_width * block_height;
  149. ret = compress2(ptr + 2, &zsize, s->tmpblock, 3 * ws * hs, 9);
  150. //ret = deflateReset(&(s->zstream));
  151. if (ret != Z_OK)
  152. av_log(s->avctx, AV_LOG_ERROR, "error while compressing block %dx%d\n", i, j);
  153. bytestream_put_be16(&ptr, (unsigned int) zsize);
  154. buf_pos += zsize + 2;
  155. //av_log(avctx, AV_LOG_ERROR, "buf_pos = %d\n", buf_pos);
  156. } else {
  157. pred_blocks++;
  158. bytestream_put_be16(&ptr, 0);
  159. buf_pos += 2;
  160. }
  161. }
  162. }
  163. if (pred_blocks)
  164. *I_frame = 0;
  165. else
  166. *I_frame = 1;
  167. return buf_pos;
  168. }
  169. static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf,
  170. int buf_size, void *data)
  171. {
  172. FlashSVContext * const s = avctx->priv_data;
  173. AVFrame *pict = data;
  174. AVFrame * const p = &s->frame;
  175. uint8_t *pfptr;
  176. int res;
  177. int I_frame = 0;
  178. int opt_w, opt_h;
  179. *p = *pict;
  180. /* First frame needs to be a keyframe */
  181. if (avctx->frame_number == 0) {
  182. s->previous_frame = av_mallocz(FFABS(p->linesize[0]) * s->image_height);
  183. if (!s->previous_frame) {
  184. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  185. return AVERROR(ENOMEM);
  186. }
  187. I_frame = 1;
  188. }
  189. if (p->linesize[0] < 0)
  190. pfptr = s->previous_frame - ((s->image_height - 1) * p->linesize[0]);
  191. else
  192. pfptr = s->previous_frame;
  193. /* Check the placement of keyframes */
  194. if (avctx->gop_size > 0) {
  195. if (avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
  196. I_frame = 1;
  197. }
  198. }
  199. opt_w = 4;
  200. opt_h = 4;
  201. if (buf_size < s->image_width*s->image_height*3) {
  202. //Conservative upper bound check for compressed data
  203. av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n",
  204. buf_size, s->image_width * s->image_height * 3);
  205. return -1;
  206. }
  207. res = encode_bitstream(s, p, buf, buf_size, opt_w * 16, opt_h * 16, pfptr, &I_frame);
  208. //save the current frame
  209. if (p->linesize[0] > 0)
  210. memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
  211. else
  212. memcpy(s->previous_frame, p->data[0] + p->linesize[0] * (s->image_height - 1),
  213. s->image_height * FFABS(p->linesize[0]));
  214. //mark the frame type so the muxer can mux it correctly
  215. if (I_frame) {
  216. p->pict_type = FF_I_TYPE;
  217. p->key_frame = 1;
  218. s->last_key_frame = avctx->frame_number;
  219. av_log(avctx, AV_LOG_DEBUG, "Inserting key frame at frame %d\n", avctx->frame_number);
  220. } else {
  221. p->pict_type = FF_P_TYPE;
  222. p->key_frame = 0;
  223. }
  224. avctx->coded_frame = p;
  225. return res;
  226. }
  227. static av_cold int flashsv_encode_end(AVCodecContext *avctx)
  228. {
  229. FlashSVContext *s = avctx->priv_data;
  230. deflateEnd(&(s->zstream));
  231. av_free(s->encbuffer);
  232. av_free(s->previous_frame);
  233. av_free(s->tmpblock);
  234. return 0;
  235. }
  236. AVCodec ff_flashsv_encoder = {
  237. .name = "flashsv",
  238. .type = AVMEDIA_TYPE_VIDEO,
  239. .id = CODEC_ID_FLASHSV,
  240. .priv_data_size = sizeof(FlashSVContext),
  241. .init = flashsv_encode_init,
  242. .encode = flashsv_encode_frame,
  243. .close = flashsv_encode_end,
  244. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
  245. .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
  246. };