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.

415 lines
14KB

  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 "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. /** Maximum RLE code for bulk copy */
  29. #define MAX_RLE_BULK 127
  30. /** Maximum RLE code for repeat */
  31. #define MAX_RLE_REPEAT 128
  32. /** Maximum RLE code for skip */
  33. #define MAX_RLE_SKIP 254
  34. typedef struct QtrleEncContext {
  35. AVCodecContext *avctx;
  36. int pixel_size;
  37. AVPicture previous_frame;
  38. unsigned int max_buf_size;
  39. int logical_width;
  40. /**
  41. * This array will contain at ith position the value of the best RLE code
  42. * if the line started at pixel i
  43. * There can be 3 values :
  44. * skip (0) : skip as much as possible pixels because they are equal to the
  45. * previous frame ones
  46. * repeat (<-1) : repeat that pixel -rle_code times, still as much as
  47. * possible
  48. * copy (>0) : copy the raw next rle_code pixels */
  49. signed char *rlecode_table;
  50. /**
  51. * This array will contain the length of the best rle encoding of the line
  52. * starting at ith pixel */
  53. int *length_table;
  54. /**
  55. * Will contain at ith position the number of consecutive pixels equal to the previous
  56. * frame starting from pixel i */
  57. uint8_t* skip_table;
  58. /** Encoded frame is a key frame */
  59. int key_frame;
  60. } QtrleEncContext;
  61. static av_cold int qtrle_encode_end(AVCodecContext *avctx)
  62. {
  63. QtrleEncContext *s = avctx->priv_data;
  64. avpicture_free(&s->previous_frame);
  65. av_free(s->rlecode_table);
  66. av_free(s->length_table);
  67. av_free(s->skip_table);
  68. return 0;
  69. }
  70. static av_cold int qtrle_encode_init(AVCodecContext *avctx)
  71. {
  72. QtrleEncContext *s = avctx->priv_data;
  73. int ret;
  74. if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
  75. return AVERROR(EINVAL);
  76. }
  77. s->avctx=avctx;
  78. s->logical_width=avctx->width;
  79. switch (avctx->pix_fmt) {
  80. case AV_PIX_FMT_GRAY8:
  81. if (avctx->width % 4) {
  82. av_log(avctx, AV_LOG_ERROR, "Width not being a multiple of 4 is not supported\n");
  83. return AVERROR(EINVAL);
  84. }
  85. s->logical_width = avctx->width / 4;
  86. s->pixel_size = 4;
  87. break;
  88. case AV_PIX_FMT_RGB555BE:
  89. s->pixel_size = 2;
  90. break;
  91. case AV_PIX_FMT_RGB24:
  92. s->pixel_size = 3;
  93. break;
  94. case AV_PIX_FMT_ARGB:
  95. s->pixel_size = 4;
  96. break;
  97. default:
  98. av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
  99. break;
  100. }
  101. avctx->bits_per_coded_sample = avctx->pix_fmt == AV_PIX_FMT_GRAY8 ? 40 : s->pixel_size*8;
  102. s->rlecode_table = av_mallocz(s->logical_width);
  103. s->skip_table = av_mallocz(s->logical_width);
  104. s->length_table = av_mallocz_array(s->logical_width + 1, sizeof(int));
  105. if (!s->skip_table || !s->length_table || !s->rlecode_table) {
  106. av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
  107. return AVERROR(ENOMEM);
  108. }
  109. if ((ret = avpicture_alloc(&s->previous_frame, avctx->pix_fmt, avctx->width, avctx->height)) < 0) {
  110. av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
  111. return ret;
  112. }
  113. s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size*2 /* image base material */
  114. + 15 /* header + footer */
  115. + s->avctx->height*2 /* skip code+rle end */
  116. + s->logical_width/MAX_RLE_BULK + 1 /* rle codes */;
  117. return 0;
  118. }
  119. /**
  120. * Compute the best RLE sequence for a line
  121. */
  122. static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
  123. {
  124. int width=s->logical_width;
  125. int i;
  126. signed char rlecode;
  127. /* This will be the number of pixels equal to the preivous frame one's
  128. * starting from the ith pixel */
  129. unsigned int skipcount;
  130. /* This will be the number of consecutive equal pixels in the current
  131. * frame, starting from the ith one also */
  132. unsigned int av_uninit(repeatcount);
  133. /* The cost of the three different possibilities */
  134. int total_skip_cost;
  135. int total_repeat_cost;
  136. int base_bulk_cost;
  137. int lowest_bulk_cost;
  138. int lowest_bulk_cost_index;
  139. int sec_lowest_bulk_cost;
  140. int sec_lowest_bulk_cost_index;
  141. uint8_t *this_line = p-> data[0] + line*p-> linesize[0] +
  142. (width - 1)*s->pixel_size;
  143. uint8_t *prev_line = s->previous_frame.data[0] + line*s->previous_frame.linesize[0] +
  144. (width - 1)*s->pixel_size;
  145. s->length_table[width] = 0;
  146. skipcount = 0;
  147. /* Initial values */
  148. lowest_bulk_cost = INT_MAX / 2;
  149. lowest_bulk_cost_index = width;
  150. sec_lowest_bulk_cost = INT_MAX / 2;
  151. sec_lowest_bulk_cost_index = width;
  152. base_bulk_cost = 1 + s->pixel_size;
  153. for (i = width - 1; i >= 0; i--) {
  154. int prev_bulk_cost;
  155. /* If our lowest bulk cost index is too far away, replace it
  156. * with the next lowest bulk cost */
  157. if (FFMIN(width, i + MAX_RLE_BULK) < lowest_bulk_cost_index) {
  158. lowest_bulk_cost = sec_lowest_bulk_cost;
  159. lowest_bulk_cost_index = sec_lowest_bulk_cost_index;
  160. sec_lowest_bulk_cost = INT_MAX / 2;
  161. sec_lowest_bulk_cost_index = width;
  162. }
  163. /* Deal with the first pixel's bulk cost */
  164. if (!i) {
  165. base_bulk_cost++;
  166. lowest_bulk_cost++;
  167. sec_lowest_bulk_cost++;
  168. }
  169. /* Look at the bulk cost of the previous loop and see if it is
  170. * a new lower bulk cost */
  171. prev_bulk_cost = s->length_table[i + 1] + base_bulk_cost;
  172. if (prev_bulk_cost <= sec_lowest_bulk_cost) {
  173. /* If it's lower than the 2nd lowest, then it may be lower
  174. * than the lowest */
  175. if (prev_bulk_cost <= lowest_bulk_cost) {
  176. /* If we have found a new lowest bulk cost,
  177. * then the 2nd lowest bulk cost is now farther than the
  178. * lowest bulk cost, and will never be used */
  179. sec_lowest_bulk_cost = INT_MAX / 2;
  180. lowest_bulk_cost = prev_bulk_cost;
  181. lowest_bulk_cost_index = i + 1;
  182. } else {
  183. /* Then it must be the 2nd lowest bulk cost */
  184. sec_lowest_bulk_cost = prev_bulk_cost;
  185. sec_lowest_bulk_cost_index = i + 1;
  186. }
  187. }
  188. if (!s->key_frame && !memcmp(this_line, prev_line, s->pixel_size))
  189. skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
  190. else
  191. skipcount = 0;
  192. total_skip_cost = s->length_table[i + skipcount] + 2;
  193. s->skip_table[i] = skipcount;
  194. if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
  195. repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
  196. else
  197. repeatcount = 1;
  198. total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
  199. /* skip code is free for the first pixel, it costs one byte for repeat and bulk copy
  200. * so let's make it aware */
  201. if (i == 0) {
  202. total_skip_cost--;
  203. total_repeat_cost++;
  204. }
  205. if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
  206. /* repeat is the best */
  207. s->length_table[i] = total_repeat_cost;
  208. s->rlecode_table[i] = -repeatcount;
  209. }
  210. else if (skipcount > 0) {
  211. /* skip is the best choice here */
  212. s->length_table[i] = total_skip_cost;
  213. s->rlecode_table[i] = 0;
  214. }
  215. else {
  216. /* We cannot do neither skip nor repeat
  217. * thus we use the best bulk copy */
  218. s->length_table[i] = lowest_bulk_cost;
  219. s->rlecode_table[i] = lowest_bulk_cost_index - i;
  220. }
  221. /* These bulk costs increase every iteration */
  222. lowest_bulk_cost += s->pixel_size;
  223. sec_lowest_bulk_cost += s->pixel_size;
  224. this_line -= s->pixel_size;
  225. prev_line -= s->pixel_size;
  226. }
  227. /* Good ! Now we have the best sequence for this line, let's output it */
  228. /* We do a special case for the first pixel so that we avoid testing it in
  229. * the whole loop */
  230. i=0;
  231. this_line = p-> data[0] + line*p->linesize[0];
  232. if (s->rlecode_table[0] == 0) {
  233. bytestream_put_byte(buf, s->skip_table[0] + 1);
  234. i += s->skip_table[0];
  235. }
  236. else bytestream_put_byte(buf, 1);
  237. while (i < width) {
  238. rlecode = s->rlecode_table[i];
  239. bytestream_put_byte(buf, rlecode);
  240. if (rlecode == 0) {
  241. /* Write a skip sequence */
  242. bytestream_put_byte(buf, s->skip_table[i] + 1);
  243. i += s->skip_table[i];
  244. }
  245. else if (rlecode > 0) {
  246. /* bulk copy */
  247. if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  248. int j;
  249. // QT grayscale colorspace has 0=white and 255=black, we will
  250. // ignore the palette that is included in the AVFrame because
  251. // AV_PIX_FMT_GRAY8 has defined color mapping
  252. for (j = 0; j < rlecode*s->pixel_size; ++j)
  253. bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
  254. } else {
  255. bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
  256. }
  257. i += rlecode;
  258. }
  259. else {
  260. /* repeat the bits */
  261. if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  262. int j;
  263. // QT grayscale colorspace has 0=white and 255=black, ...
  264. for (j = 0; j < s->pixel_size; ++j)
  265. bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
  266. } else {
  267. bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
  268. }
  269. i -= rlecode;
  270. }
  271. }
  272. bytestream_put_byte(buf, -1); // end RLE line
  273. }
  274. /** Encode frame including header */
  275. static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
  276. {
  277. int i;
  278. int start_line = 0;
  279. int end_line = s->avctx->height;
  280. uint8_t *orig_buf = buf;
  281. if (!s->key_frame) {
  282. unsigned line_size = s->logical_width * s->pixel_size;
  283. for (start_line = 0; start_line < s->avctx->height; start_line++)
  284. if (memcmp(p->data[0] + start_line*p->linesize[0],
  285. s->previous_frame.data[0] + start_line*s->previous_frame.linesize[0],
  286. line_size))
  287. break;
  288. for (end_line=s->avctx->height; end_line > start_line; end_line--)
  289. if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
  290. s->previous_frame.data[0] + (end_line - 1)*s->previous_frame.linesize[0],
  291. line_size))
  292. break;
  293. }
  294. bytestream_put_be32(&buf, 0); // CHUNK SIZE, patched later
  295. if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
  296. bytestream_put_be16(&buf, 0); // header
  297. else {
  298. bytestream_put_be16(&buf, 8); // header
  299. bytestream_put_be16(&buf, start_line); // starting line
  300. bytestream_put_be16(&buf, 0); // unknown
  301. bytestream_put_be16(&buf, end_line - start_line); // lines to update
  302. bytestream_put_be16(&buf, 0); // unknown
  303. }
  304. for (i = start_line; i < end_line; i++)
  305. qtrle_encode_line(s, p, i, &buf);
  306. bytestream_put_byte(&buf, 0); // zero skip code = frame finished
  307. AV_WB32(orig_buf, buf - orig_buf); // patch the chunk size
  308. return buf - orig_buf;
  309. }
  310. static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  311. const AVFrame *pict, int *got_packet)
  312. {
  313. QtrleEncContext * const s = avctx->priv_data;
  314. enum AVPictureType pict_type;
  315. int ret;
  316. if ((ret = ff_alloc_packet2(avctx, pkt, s->max_buf_size, 0)) < 0)
  317. return ret;
  318. if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
  319. /* I-Frame */
  320. pict_type = AV_PICTURE_TYPE_I;
  321. s->key_frame = 1;
  322. } else {
  323. /* P-Frame */
  324. pict_type = AV_PICTURE_TYPE_P;
  325. s->key_frame = 0;
  326. }
  327. pkt->size = encode_frame(s, pict, pkt->data);
  328. /* save the current frame */
  329. av_picture_copy(&s->previous_frame, (const AVPicture *)pict,
  330. avctx->pix_fmt, avctx->width, avctx->height);
  331. #if FF_API_CODED_FRAME
  332. FF_DISABLE_DEPRECATION_WARNINGS
  333. avctx->coded_frame->key_frame = s->key_frame;
  334. avctx->coded_frame->pict_type = pict_type;
  335. FF_ENABLE_DEPRECATION_WARNINGS
  336. #endif
  337. if (s->key_frame)
  338. pkt->flags |= AV_PKT_FLAG_KEY;
  339. *got_packet = 1;
  340. return 0;
  341. }
  342. AVCodec ff_qtrle_encoder = {
  343. .name = "qtrle",
  344. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  345. .type = AVMEDIA_TYPE_VIDEO,
  346. .id = AV_CODEC_ID_QTRLE,
  347. .priv_data_size = sizeof(QtrleEncContext),
  348. .init = qtrle_encode_init,
  349. .encode2 = qtrle_encode_frame,
  350. .close = qtrle_encode_end,
  351. .pix_fmts = (const enum AVPixelFormat[]){
  352. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB555BE, AV_PIX_FMT_ARGB, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  353. },
  354. };