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.

360 lines
12KB

  1. /*
  2. * Copyright (c) 2000 Fabrice Bellard
  3. * Copyright (c) 2002 Francois Revol
  4. * Copyright (c) 2006 Baptiste Coudurier
  5. *
  6. * first version by Francois Revol <revol@free.fr>
  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. /**
  25. * @file
  26. * GIF encoder
  27. * @see http://www.w3.org/Graphics/GIF/spec-gif89a.txt
  28. */
  29. #define BITSTREAM_WRITER_LE
  30. #include "libavutil/opt.h"
  31. #include "libavutil/imgutils.h"
  32. #include "avcodec.h"
  33. #include "bytestream.h"
  34. #include "internal.h"
  35. #include "lzw.h"
  36. #include "gif.h"
  37. #include "put_bits.h"
  38. typedef struct GIFContext {
  39. const AVClass *class;
  40. LZWState *lzw;
  41. uint8_t *buf;
  42. AVFrame *last_frame;
  43. int flags;
  44. uint32_t palette[AVPALETTE_COUNT]; ///< local reference palette for !pal8
  45. int palette_loaded;
  46. int transparent_index;
  47. uint8_t *pal_exdata;
  48. uint8_t *tmpl; ///< temporary line buffer
  49. } GIFContext;
  50. enum {
  51. GF_OFFSETTING = 1<<0,
  52. GF_TRANSDIFF = 1<<1,
  53. };
  54. static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
  55. {
  56. int histogram[AVPALETTE_COUNT] = {0};
  57. int x, y, i;
  58. for (y = 0; y < h; y++) {
  59. for (x = 0; x < w; x++)
  60. histogram[buf[x]]++;
  61. buf += linesize;
  62. }
  63. for (i = 0; i < FF_ARRAY_ELEMS(histogram); i++)
  64. if (!histogram[i])
  65. return i;
  66. return -1;
  67. }
  68. static int gif_image_write_image(AVCodecContext *avctx,
  69. uint8_t **bytestream, uint8_t *end,
  70. const uint32_t *palette,
  71. const uint8_t *buf, const int linesize,
  72. AVPacket *pkt)
  73. {
  74. GIFContext *s = avctx->priv_data;
  75. int len = 0, height = avctx->height, width = avctx->width, x, y;
  76. int x_start = 0, y_start = 0, trans = s->transparent_index;
  77. int honor_transparency = (s->flags & GF_TRANSDIFF) && s->last_frame;
  78. const uint8_t *ptr;
  79. /* Crop image */
  80. if ((s->flags & GF_OFFSETTING) && s->last_frame && !palette) {
  81. const uint8_t *ref = s->last_frame->data[0];
  82. const int ref_linesize = s->last_frame->linesize[0];
  83. int x_end = avctx->width - 1,
  84. y_end = avctx->height - 1;
  85. /* skip common lines */
  86. while (y_start < y_end) {
  87. if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
  88. break;
  89. y_start++;
  90. }
  91. while (y_end > y_start) {
  92. if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
  93. break;
  94. y_end--;
  95. }
  96. height = y_end + 1 - y_start;
  97. /* skip common columns */
  98. while (x_start < x_end) {
  99. int same_column = 1;
  100. for (y = y_start; y <= y_end; y++) {
  101. if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
  102. same_column = 0;
  103. break;
  104. }
  105. }
  106. if (!same_column)
  107. break;
  108. x_start++;
  109. }
  110. while (x_end > x_start) {
  111. int same_column = 1;
  112. for (y = y_start; y <= y_end; y++) {
  113. if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
  114. same_column = 0;
  115. break;
  116. }
  117. }
  118. if (!same_column)
  119. break;
  120. x_end--;
  121. }
  122. width = x_end + 1 - x_start;
  123. av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
  124. width, height, x_start, y_start, avctx->width, avctx->height);
  125. }
  126. /* image block */
  127. bytestream_put_byte(bytestream, GIF_IMAGE_SEPARATOR);
  128. bytestream_put_le16(bytestream, x_start);
  129. bytestream_put_le16(bytestream, y_start);
  130. bytestream_put_le16(bytestream, width);
  131. bytestream_put_le16(bytestream, height);
  132. if (!palette) {
  133. bytestream_put_byte(bytestream, 0x00); /* flags */
  134. } else {
  135. unsigned i;
  136. bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */
  137. for (i = 0; i < AVPALETTE_COUNT; i++) {
  138. const uint32_t v = palette[i];
  139. bytestream_put_be24(bytestream, v);
  140. }
  141. }
  142. if (honor_transparency && trans < 0) {
  143. trans = pick_palette_entry(buf + y_start*linesize + x_start,
  144. linesize, width, height);
  145. if (trans < 0) { // TODO, patch welcome
  146. av_log(avctx, AV_LOG_DEBUG, "No available color, can not use transparency\n");
  147. } else {
  148. uint8_t *pal_exdata = s->pal_exdata;
  149. if (!pal_exdata)
  150. pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  151. if (!pal_exdata)
  152. return AVERROR(ENOMEM);
  153. memcpy(pal_exdata, s->palette, AVPALETTE_SIZE);
  154. pal_exdata[trans*4 + 3*!HAVE_BIGENDIAN] = 0x00;
  155. }
  156. }
  157. if (trans < 0)
  158. honor_transparency = 0;
  159. bytestream_put_byte(bytestream, 0x08);
  160. ff_lzw_encode_init(s->lzw, s->buf, 2 * width * height,
  161. 12, FF_LZW_GIF, put_bits);
  162. ptr = buf + y_start*linesize + x_start;
  163. if (honor_transparency) {
  164. const int ref_linesize = s->last_frame->linesize[0];
  165. const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start;
  166. for (y = 0; y < height; y++) {
  167. memcpy(s->tmpl, ptr, width);
  168. for (x = 0; x < width; x++)
  169. if (ref[x] == ptr[x])
  170. s->tmpl[x] = trans;
  171. len += ff_lzw_encode(s->lzw, s->tmpl, width);
  172. ptr += linesize;
  173. ref += ref_linesize;
  174. }
  175. } else {
  176. for (y = 0; y < height; y++) {
  177. len += ff_lzw_encode(s->lzw, ptr, width);
  178. ptr += linesize;
  179. }
  180. }
  181. len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
  182. ptr = s->buf;
  183. while (len > 0) {
  184. int size = FFMIN(255, len);
  185. bytestream_put_byte(bytestream, size);
  186. if (end - *bytestream < size)
  187. return -1;
  188. bytestream_put_buffer(bytestream, ptr, size);
  189. ptr += size;
  190. len -= size;
  191. }
  192. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  193. return 0;
  194. }
  195. static av_cold int gif_encode_init(AVCodecContext *avctx)
  196. {
  197. GIFContext *s = avctx->priv_data;
  198. if (avctx->width > 65535 || avctx->height > 65535) {
  199. av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
  200. return AVERROR(EINVAL);
  201. }
  202. #if FF_API_CODED_FRAME
  203. FF_DISABLE_DEPRECATION_WARNINGS
  204. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  205. avctx->coded_frame->key_frame = 1;
  206. FF_ENABLE_DEPRECATION_WARNINGS
  207. #endif
  208. s->transparent_index = -1;
  209. s->lzw = av_mallocz(ff_lzw_encode_state_size);
  210. s->buf = av_malloc(avctx->width*avctx->height*2);
  211. s->tmpl = av_malloc(avctx->width);
  212. if (!s->tmpl || !s->buf || !s->lzw)
  213. return AVERROR(ENOMEM);
  214. if (avpriv_set_systematic_pal2(s->palette, avctx->pix_fmt) < 0)
  215. av_assert0(avctx->pix_fmt == AV_PIX_FMT_PAL8);
  216. return 0;
  217. }
  218. /* FIXME: duplicated with lavc */
  219. static int get_palette_transparency_index(const uint32_t *palette)
  220. {
  221. int transparent_color_index = -1;
  222. unsigned i, smallest_alpha = 0xff;
  223. if (!palette)
  224. return -1;
  225. for (i = 0; i < AVPALETTE_COUNT; i++) {
  226. const uint32_t v = palette[i];
  227. if (v >> 24 < smallest_alpha) {
  228. smallest_alpha = v >> 24;
  229. transparent_color_index = i;
  230. }
  231. }
  232. return smallest_alpha < 128 ? transparent_color_index : -1;
  233. }
  234. static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  235. const AVFrame *pict, int *got_packet)
  236. {
  237. GIFContext *s = avctx->priv_data;
  238. uint8_t *outbuf_ptr, *end;
  239. const uint32_t *palette = NULL;
  240. int ret;
  241. if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
  242. return ret;
  243. outbuf_ptr = pkt->data;
  244. end = pkt->data + pkt->size;
  245. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  246. uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  247. if (!pal_exdata)
  248. return AVERROR(ENOMEM);
  249. memcpy(pal_exdata, pict->data[1], AVPALETTE_SIZE);
  250. palette = (uint32_t*)pict->data[1];
  251. s->pal_exdata = pal_exdata;
  252. /* The first palette with PAL8 will be used as generic palette by the
  253. * muxer so we don't need to write it locally in the packet. We store
  254. * it as a reference here in case it changes later. */
  255. if (!s->palette_loaded) {
  256. memcpy(s->palette, palette, AVPALETTE_SIZE);
  257. s->transparent_index = get_palette_transparency_index(palette);
  258. s->palette_loaded = 1;
  259. palette = NULL;
  260. } else if (!memcmp(s->palette, palette, AVPALETTE_SIZE)) {
  261. palette = NULL;
  262. }
  263. }
  264. gif_image_write_image(avctx, &outbuf_ptr, end, palette,
  265. pict->data[0], pict->linesize[0], pkt);
  266. if (!s->last_frame) {
  267. s->last_frame = av_frame_alloc();
  268. if (!s->last_frame)
  269. return AVERROR(ENOMEM);
  270. }
  271. av_frame_unref(s->last_frame);
  272. ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
  273. if (ret < 0)
  274. return ret;
  275. pkt->size = outbuf_ptr - pkt->data;
  276. pkt->flags |= AV_PKT_FLAG_KEY;
  277. *got_packet = 1;
  278. return 0;
  279. }
  280. static int gif_encode_close(AVCodecContext *avctx)
  281. {
  282. GIFContext *s = avctx->priv_data;
  283. av_freep(&s->lzw);
  284. av_freep(&s->buf);
  285. av_frame_free(&s->last_frame);
  286. av_freep(&s->tmpl);
  287. return 0;
  288. }
  289. #define OFFSET(x) offsetof(GIFContext, x)
  290. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  291. static const AVOption gif_options[] = {
  292. { "gifflags", "set GIF flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = GF_OFFSETTING|GF_TRANSDIFF}, 0, INT_MAX, FLAGS, "flags" },
  293. { "offsetting", "enable picture offsetting", 0, AV_OPT_TYPE_CONST, {.i64=GF_OFFSETTING}, INT_MIN, INT_MAX, FLAGS, "flags" },
  294. { "transdiff", "enable transparency detection between frames", 0, AV_OPT_TYPE_CONST, {.i64=GF_TRANSDIFF}, INT_MIN, INT_MAX, FLAGS, "flags" },
  295. { NULL }
  296. };
  297. static const AVClass gif_class = {
  298. .class_name = "GIF encoder",
  299. .item_name = av_default_item_name,
  300. .option = gif_options,
  301. .version = LIBAVUTIL_VERSION_INT,
  302. };
  303. AVCodec ff_gif_encoder = {
  304. .name = "gif",
  305. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  306. .type = AVMEDIA_TYPE_VIDEO,
  307. .id = AV_CODEC_ID_GIF,
  308. .priv_data_size = sizeof(GIFContext),
  309. .init = gif_encode_init,
  310. .encode2 = gif_encode_frame,
  311. .close = gif_encode_close,
  312. .pix_fmts = (const enum AVPixelFormat[]){
  313. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  314. AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
  315. },
  316. .priv_class = &gif_class,
  317. };