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.

296 lines
9.2KB

  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. * A description of the bitstream format for Flash Screen Video version 1/2
  30. * is part of the SWF File Format Specification (version 10), which can be
  31. * downloaded from http://www.adobe.com/devnet/swf.html.
  32. */
  33. /*
  34. * Encoding ideas: A basic encoder would just use a fixed block size.
  35. * Block sizes can be multiples of 16, from 16 to 256. The blocks don't
  36. * have to be quadratic. A brute force search with a set of different
  37. * block sizes should give a better result than to just use a fixed size.
  38. *
  39. * TODO:
  40. * Don't reencode the frame in brute force mode if the frame is a dupe.
  41. * Speed up. Make the difference check faster.
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <zlib.h>
  46. #include "avcodec.h"
  47. #include "internal.h"
  48. #include "put_bits.h"
  49. #include "bytestream.h"
  50. typedef struct FlashSVContext {
  51. AVCodecContext *avctx;
  52. uint8_t *previous_frame;
  53. int image_width, image_height;
  54. int block_width, block_height;
  55. uint8_t *tmpblock;
  56. uint8_t *encbuffer;
  57. int block_size;
  58. z_stream zstream;
  59. int last_key_frame;
  60. } FlashSVContext;
  61. static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
  62. int h, int w, int stride, uint8_t *pfptr)
  63. {
  64. int i, j;
  65. uint8_t *nsptr;
  66. uint8_t *npfptr;
  67. int diff = 0;
  68. for (i = dx + h; i > dx; i--) {
  69. nsptr = sptr + i * stride + dy * 3;
  70. npfptr = pfptr + i * stride + dy * 3;
  71. for (j = 0; j < w * 3; j++) {
  72. diff |= npfptr[j] ^ nsptr[j];
  73. dptr[j] = nsptr[j];
  74. }
  75. dptr += w * 3;
  76. }
  77. if (diff)
  78. return 1;
  79. return 0;
  80. }
  81. static av_cold int flashsv_encode_end(AVCodecContext *avctx)
  82. {
  83. FlashSVContext *s = avctx->priv_data;
  84. deflateEnd(&s->zstream);
  85. av_free(s->encbuffer);
  86. av_free(s->previous_frame);
  87. av_free(s->tmpblock);
  88. av_frame_free(&avctx->coded_frame);
  89. return 0;
  90. }
  91. static av_cold int flashsv_encode_init(AVCodecContext *avctx)
  92. {
  93. FlashSVContext *s = avctx->priv_data;
  94. s->avctx = avctx;
  95. if (avctx->width > 4095 || avctx->height > 4095) {
  96. av_log(avctx, AV_LOG_ERROR,
  97. "Input dimensions too large, input must be max 4096x4096 !\n");
  98. return AVERROR_INVALIDDATA;
  99. }
  100. // Needed if zlib unused or init aborted before deflateInit
  101. memset(&s->zstream, 0, sizeof(z_stream));
  102. s->last_key_frame = 0;
  103. s->image_width = avctx->width;
  104. s->image_height = avctx->height;
  105. s->tmpblock = av_mallocz(3 * 256 * 256);
  106. s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
  107. if (!s->tmpblock || !s->encbuffer) {
  108. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  109. return AVERROR(ENOMEM);
  110. }
  111. avctx->coded_frame = av_frame_alloc();
  112. if (!avctx->coded_frame) {
  113. flashsv_encode_end(avctx);
  114. return AVERROR(ENOMEM);
  115. }
  116. return 0;
  117. }
  118. static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
  119. int buf_size, int block_width, int block_height,
  120. uint8_t *previous_frame, int *I_frame)
  121. {
  122. PutBitContext pb;
  123. int h_blocks, v_blocks, h_part, v_part, i, j;
  124. int buf_pos, res;
  125. int pred_blocks = 0;
  126. init_put_bits(&pb, buf, buf_size * 8);
  127. put_bits(&pb, 4, block_width / 16 - 1);
  128. put_bits(&pb, 12, s->image_width);
  129. put_bits(&pb, 4, block_height / 16 - 1);
  130. put_bits(&pb, 12, s->image_height);
  131. flush_put_bits(&pb);
  132. buf_pos = 4;
  133. h_blocks = s->image_width / block_width;
  134. h_part = s->image_width % block_width;
  135. v_blocks = s->image_height / block_height;
  136. v_part = s->image_height % block_height;
  137. /* loop over all block columns */
  138. for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
  139. int y_pos = j * block_height; // vertical position in frame
  140. int cur_blk_height = (j < v_blocks) ? block_height : v_part;
  141. /* loop over all block rows */
  142. for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
  143. int x_pos = i * block_width; // horizontal position in frame
  144. int cur_blk_width = (i < h_blocks) ? block_width : h_part;
  145. int ret = Z_OK;
  146. uint8_t *ptr = buf + buf_pos;
  147. /* copy the block to the temp buffer before compression
  148. * (if it differs from the previous frame's block) */
  149. res = copy_region_enc(p->data[0], s->tmpblock,
  150. s->image_height - (y_pos + cur_blk_height + 1),
  151. x_pos, cur_blk_height, cur_blk_width,
  152. p->linesize[0], previous_frame);
  153. if (res || *I_frame) {
  154. unsigned long zsize = 3 * block_width * block_height;
  155. ret = compress2(ptr + 2, &zsize, s->tmpblock,
  156. 3 * cur_blk_width * cur_blk_height, 9);
  157. //ret = deflateReset(&s->zstream);
  158. if (ret != Z_OK)
  159. av_log(s->avctx, AV_LOG_ERROR,
  160. "error while compressing block %dx%d\n", i, j);
  161. bytestream_put_be16(&ptr, zsize);
  162. buf_pos += zsize + 2;
  163. av_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
  164. } else {
  165. pred_blocks++;
  166. bytestream_put_be16(&ptr, 0);
  167. buf_pos += 2;
  168. }
  169. }
  170. }
  171. if (pred_blocks)
  172. *I_frame = 0;
  173. else
  174. *I_frame = 1;
  175. return buf_pos;
  176. }
  177. static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  178. const AVFrame *pict, int *got_packet)
  179. {
  180. FlashSVContext * const s = avctx->priv_data;
  181. const AVFrame * const p = pict;
  182. uint8_t *pfptr;
  183. int res;
  184. int I_frame = 0;
  185. int opt_w = 4, opt_h = 4;
  186. /* First frame needs to be a keyframe */
  187. if (avctx->frame_number == 0) {
  188. s->previous_frame = av_mallocz(FFABS(p->linesize[0]) * s->image_height);
  189. if (!s->previous_frame) {
  190. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  191. return AVERROR(ENOMEM);
  192. }
  193. I_frame = 1;
  194. }
  195. if (p->linesize[0] < 0)
  196. pfptr = s->previous_frame - (s->image_height - 1) * p->linesize[0];
  197. else
  198. pfptr = s->previous_frame;
  199. /* Check the placement of keyframes */
  200. if (avctx->gop_size > 0 &&
  201. avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
  202. I_frame = 1;
  203. }
  204. if ((res = ff_alloc_packet(pkt, s->image_width * s->image_height * 3)) < 0) {
  205. //Conservative upper bound check for compressed data
  206. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n",
  207. s->image_width * s->image_height * 3);
  208. return res;
  209. }
  210. pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
  211. pfptr, &I_frame);
  212. //save the current frame
  213. if (p->linesize[0] > 0)
  214. memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
  215. else
  216. memcpy(s->previous_frame,
  217. p->data[0] + p->linesize[0] * (s->image_height - 1),
  218. s->image_height * FFABS(p->linesize[0]));
  219. //mark the frame type so the muxer can mux it correctly
  220. if (I_frame) {
  221. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  222. avctx->coded_frame->key_frame = 1;
  223. s->last_key_frame = avctx->frame_number;
  224. av_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
  225. } else {
  226. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
  227. avctx->coded_frame->key_frame = 0;
  228. }
  229. if (avctx->coded_frame->key_frame)
  230. pkt->flags |= AV_PKT_FLAG_KEY;
  231. *got_packet = 1;
  232. return 0;
  233. }
  234. AVCodec ff_flashsv_encoder = {
  235. .name = "flashsv",
  236. .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
  237. .type = AVMEDIA_TYPE_VIDEO,
  238. .id = AV_CODEC_ID_FLASHSV,
  239. .priv_data_size = sizeof(FlashSVContext),
  240. .init = flashsv_encode_init,
  241. .encode2 = flashsv_encode_frame,
  242. .close = flashsv_encode_end,
  243. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
  244. };