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.

335 lines
11KB

  1. /*
  2. * Quicktime Animation (RLE) Video Encoder
  3. * Copyright (C) 2007 Clemens Fruhwirth
  4. * Copyright (C) 2007 Alexis Ballier
  5. *
  6. * This file is based on flashsvenc.c.
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavcore/imgutils.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. /** Maximum RLE code for bulk copy */
  28. #define MAX_RLE_BULK 127
  29. /** Maximum RLE code for repeat */
  30. #define MAX_RLE_REPEAT 128
  31. /** Maximum RLE code for skip */
  32. #define MAX_RLE_SKIP 254
  33. typedef struct QtrleEncContext {
  34. AVCodecContext *avctx;
  35. AVFrame frame;
  36. int pixel_size;
  37. AVPicture previous_frame;
  38. unsigned int max_buf_size;
  39. /**
  40. * This array will contain at ith position the value of the best RLE code
  41. * if the line started at pixel i
  42. * There can be 3 values :
  43. * skip (0) : skip as much as possible pixels because they are equal to the
  44. * previous frame ones
  45. * repeat (<-1) : repeat that pixel -rle_code times, still as much as
  46. * possible
  47. * copy (>0) : copy the raw next rle_code pixels */
  48. signed char *rlecode_table;
  49. /**
  50. * This array will contain the length of the best rle encoding of the line
  51. * starting at ith pixel */
  52. int *length_table;
  53. /**
  54. * Will contain at ith position the number of consecutive pixels equal to the previous
  55. * frame starting from pixel i */
  56. uint8_t* skip_table;
  57. } QtrleEncContext;
  58. static av_cold int qtrle_encode_init(AVCodecContext *avctx)
  59. {
  60. QtrleEncContext *s = avctx->priv_data;
  61. if (av_check_image_size(avctx->width, avctx->height, 0, avctx) < 0) {
  62. return -1;
  63. }
  64. s->avctx=avctx;
  65. switch (avctx->pix_fmt) {
  66. case PIX_FMT_RGB555BE:
  67. s->pixel_size = 2;
  68. break;
  69. case PIX_FMT_RGB24:
  70. s->pixel_size = 3;
  71. break;
  72. case PIX_FMT_ARGB:
  73. s->pixel_size = 4;
  74. break;
  75. default:
  76. av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
  77. break;
  78. }
  79. avctx->bits_per_coded_sample = s->pixel_size*8;
  80. s->rlecode_table = av_mallocz(s->avctx->width);
  81. s->skip_table = av_mallocz(s->avctx->width);
  82. s->length_table = av_mallocz((s->avctx->width + 1)*sizeof(int));
  83. if (!s->skip_table || !s->length_table || !s->rlecode_table) {
  84. av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
  85. return -1;
  86. }
  87. if (avpicture_alloc(&s->previous_frame, avctx->pix_fmt, avctx->width, avctx->height) < 0) {
  88. av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
  89. return -1;
  90. }
  91. s->max_buf_size = s->avctx->width*s->avctx->height*s->pixel_size /* image base material */
  92. + 15 /* header + footer */
  93. + s->avctx->height*2 /* skip code+rle end */
  94. + s->avctx->width/MAX_RLE_BULK + 1 /* rle codes */;
  95. avctx->coded_frame = &s->frame;
  96. return 0;
  97. }
  98. /**
  99. * Compute the best RLE sequence for a line
  100. */
  101. static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t **buf)
  102. {
  103. int width=s->avctx->width;
  104. int i;
  105. signed char rlecode;
  106. /* We will use it to compute the best bulk copy sequence */
  107. unsigned int bulkcount;
  108. /* This will be the number of pixels equal to the preivous frame one's
  109. * starting from the ith pixel */
  110. unsigned int skipcount;
  111. /* This will be the number of consecutive equal pixels in the current
  112. * frame, starting from the ith one also */
  113. unsigned int av_uninit(repeatcount);
  114. /* The cost of the three different possibilities */
  115. int total_bulk_cost;
  116. int total_skip_cost;
  117. int total_repeat_cost;
  118. int temp_cost;
  119. int j;
  120. uint8_t *this_line = p-> data[0] + line*p-> linesize[0] +
  121. (width - 1)*s->pixel_size;
  122. uint8_t *prev_line = s->previous_frame.data[0] + line*s->previous_frame.linesize[0] +
  123. (width - 1)*s->pixel_size;
  124. s->length_table[width] = 0;
  125. skipcount = 0;
  126. for (i = width - 1; i >= 0; i--) {
  127. if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))
  128. skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
  129. else
  130. skipcount = 0;
  131. total_skip_cost = s->length_table[i + skipcount] + 2;
  132. s->skip_table[i] = skipcount;
  133. if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
  134. repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
  135. else
  136. repeatcount = 1;
  137. total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
  138. /* skip code is free for the first pixel, it costs one byte for repeat and bulk copy
  139. * so let's make it aware */
  140. if (i == 0) {
  141. total_skip_cost--;
  142. total_repeat_cost++;
  143. }
  144. if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
  145. /* repeat is the best */
  146. s->length_table[i] = total_repeat_cost;
  147. s->rlecode_table[i] = -repeatcount;
  148. }
  149. else if (skipcount > 0) {
  150. /* skip is the best choice here */
  151. s->length_table[i] = total_skip_cost;
  152. s->rlecode_table[i] = 0;
  153. }
  154. else {
  155. /* We cannot do neither skip nor repeat
  156. * thus we search for the best bulk copy to do */
  157. int limit = FFMIN(width - i, MAX_RLE_BULK);
  158. temp_cost = 1 + s->pixel_size + !i;
  159. total_bulk_cost = INT_MAX;
  160. for (j = 1; j <= limit; j++) {
  161. if (s->length_table[i + j] + temp_cost < total_bulk_cost) {
  162. /* We have found a better bulk copy ... */
  163. total_bulk_cost = s->length_table[i + j] + temp_cost;
  164. bulkcount = j;
  165. }
  166. temp_cost += s->pixel_size;
  167. }
  168. s->length_table[i] = total_bulk_cost;
  169. s->rlecode_table[i] = bulkcount;
  170. }
  171. this_line -= s->pixel_size;
  172. prev_line -= s->pixel_size;
  173. }
  174. /* Good ! Now we have the best sequence for this line, let's ouput it */
  175. /* We do a special case for the first pixel so that we avoid testing it in
  176. * the whole loop */
  177. i=0;
  178. this_line = p-> data[0] + line*p->linesize[0];
  179. if (s->rlecode_table[0] == 0) {
  180. bytestream_put_byte(buf, s->skip_table[0] + 1);
  181. i += s->skip_table[0];
  182. }
  183. else bytestream_put_byte(buf, 1);
  184. while (i < width) {
  185. rlecode = s->rlecode_table[i];
  186. bytestream_put_byte(buf, rlecode);
  187. if (rlecode == 0) {
  188. /* Write a skip sequence */
  189. bytestream_put_byte(buf, s->skip_table[i] + 1);
  190. i += s->skip_table[i];
  191. }
  192. else if (rlecode > 0) {
  193. /* bulk copy */
  194. bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
  195. i += rlecode;
  196. }
  197. else {
  198. /* repeat the bits */
  199. bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
  200. i -= rlecode;
  201. }
  202. }
  203. bytestream_put_byte(buf, -1); // end RLE line
  204. }
  205. /** Encode frame including header */
  206. static int encode_frame(QtrleEncContext *s, AVFrame *p, uint8_t *buf)
  207. {
  208. int i;
  209. int start_line = 0;
  210. int end_line = s->avctx->height;
  211. uint8_t *orig_buf = buf;
  212. if (!s->frame.key_frame) {
  213. unsigned line_size = s->avctx->width * s->pixel_size;
  214. for (start_line = 0; start_line < s->avctx->height; start_line++)
  215. if (memcmp(p->data[0] + start_line*p->linesize[0],
  216. s->previous_frame.data[0] + start_line*s->previous_frame.linesize[0],
  217. line_size))
  218. break;
  219. for (end_line=s->avctx->height; end_line > start_line; end_line--)
  220. if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
  221. s->previous_frame.data[0] + (end_line - 1)*s->previous_frame.linesize[0],
  222. line_size))
  223. break;
  224. }
  225. bytestream_put_be32(&buf, 0); // CHUNK SIZE, patched later
  226. if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
  227. bytestream_put_be16(&buf, 0); // header
  228. else {
  229. bytestream_put_be16(&buf, 8); // header
  230. bytestream_put_be16(&buf, start_line); // starting line
  231. bytestream_put_be16(&buf, 0); // unknown
  232. bytestream_put_be16(&buf, end_line - start_line); // lines to update
  233. bytestream_put_be16(&buf, 0); // unknown
  234. }
  235. for (i = start_line; i < end_line; i++)
  236. qtrle_encode_line(s, p, i, &buf);
  237. bytestream_put_byte(&buf, 0); // zero skip code = frame finished
  238. AV_WB32(orig_buf, buf - orig_buf); // patch the chunk size
  239. return buf - orig_buf;
  240. }
  241. static int qtrle_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  242. {
  243. QtrleEncContext * const s = avctx->priv_data;
  244. AVFrame *pict = data;
  245. AVFrame * const p = &s->frame;
  246. int chunksize;
  247. *p = *pict;
  248. if (buf_size < s->max_buf_size) {
  249. /* Upper bound check for compressed data */
  250. av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n", buf_size, s->max_buf_size);
  251. return -1;
  252. }
  253. if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
  254. /* I-Frame */
  255. p->pict_type = FF_I_TYPE;
  256. p->key_frame = 1;
  257. } else {
  258. /* P-Frame */
  259. p->pict_type = FF_P_TYPE;
  260. p->key_frame = 0;
  261. }
  262. chunksize = encode_frame(s, pict, buf);
  263. /* save the current frame */
  264. av_picture_copy(&s->previous_frame, (AVPicture *)p, avctx->pix_fmt, avctx->width, avctx->height);
  265. return chunksize;
  266. }
  267. static av_cold int qtrle_encode_end(AVCodecContext *avctx)
  268. {
  269. QtrleEncContext *s = avctx->priv_data;
  270. avpicture_free(&s->previous_frame);
  271. av_free(s->rlecode_table);
  272. av_free(s->length_table);
  273. av_free(s->skip_table);
  274. return 0;
  275. }
  276. AVCodec qtrle_encoder = {
  277. "qtrle",
  278. AVMEDIA_TYPE_VIDEO,
  279. CODEC_ID_QTRLE,
  280. sizeof(QtrleEncContext),
  281. qtrle_encode_init,
  282. qtrle_encode_frame,
  283. qtrle_encode_end,
  284. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB555BE, PIX_FMT_ARGB, PIX_FMT_NONE},
  285. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  286. };