qtrleenc.c 11 KB

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