LCOV - code coverage report
Current view: top level - libavformat - gif.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 85 109 78.0 %
Date: 2017-12-11 04:34:20 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Animated GIF muxer
       3             :  * Copyright (c) 2000 Fabrice Bellard
       4             :  *
       5             :  * first version by Francois Revol <revol@free.fr>
       6             :  *
       7             :  * This file is part of FFmpeg.
       8             :  *
       9             :  * FFmpeg is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free Software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  *
      14             :  * FFmpeg is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  *
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with FFmpeg; if not, write to the Free Software
      21             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      22             :  */
      23             : 
      24             : #include "avformat.h"
      25             : #include "internal.h"
      26             : #include "libavutil/avassert.h"
      27             : #include "libavutil/imgutils.h"
      28             : #include "libavutil/log.h"
      29             : #include "libavutil/opt.h"
      30             : 
      31             : /* XXX: random value that shouldn't be taken into effect if there is no
      32             :  * transparent color in the palette (the transparency bit will be set to 0) */
      33             : #define DEFAULT_TRANSPARENCY_INDEX 0x1f
      34             : 
      35          26 : static int get_palette_transparency_index(const uint32_t *palette)
      36             : {
      37          26 :     int transparent_color_index = -1;
      38          26 :     unsigned i, smallest_alpha = 0xff;
      39             : 
      40          26 :     if (!palette)
      41          25 :         return -1;
      42             : 
      43         257 :     for (i = 0; i < AVPALETTE_COUNT; i++) {
      44         256 :         const uint32_t v = palette[i];
      45         256 :         if (v >> 24 < smallest_alpha) {
      46           0 :             smallest_alpha = v >> 24;
      47           0 :             transparent_color_index = i;
      48             :         }
      49             :     }
      50           1 :     return smallest_alpha < 128 ? transparent_color_index : -1;
      51             : }
      52             : 
      53           1 : static int gif_image_write_header(AVIOContext *pb, AVStream *st,
      54             :                                   int loop_count, uint32_t *palette)
      55             : {
      56             :     int i;
      57           1 :     int64_t aspect = 0;
      58           1 :     const AVRational sar = st->sample_aspect_ratio;
      59             : 
      60           1 :     if (sar.num > 0 && sar.den > 0) {
      61           0 :         aspect = sar.num * 64LL / sar.den - 15;
      62           0 :         if (aspect < 0 || aspect > 255)
      63           0 :             aspect = 0;
      64             :     }
      65             : 
      66           1 :     avio_write(pb, "GIF", 3);
      67           1 :     avio_write(pb, "89a", 3);
      68           1 :     avio_wl16(pb, st->codecpar->width);
      69           1 :     avio_wl16(pb, st->codecpar->height);
      70             : 
      71           1 :     if (palette) {
      72           1 :         const int bcid = get_palette_transparency_index(palette);
      73             : 
      74           1 :         avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
      75           1 :         avio_w8(pb, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid); /* background color index */
      76           1 :         avio_w8(pb, aspect);
      77         257 :         for (i = 0; i < 256; i++) {
      78         256 :             const uint32_t v = palette[i] & 0xffffff;
      79         256 :             avio_wb24(pb, v);
      80             :         }
      81             :     } else {
      82           0 :         avio_w8(pb, 0); /* flags */
      83           0 :         avio_w8(pb, 0); /* background color index */
      84           0 :         avio_w8(pb, aspect);
      85             :     }
      86             : 
      87             : 
      88           1 :     if (loop_count >= 0 ) {
      89             :         /* "NETSCAPE EXTENSION" for looped animation GIF */
      90           1 :         avio_w8(pb, 0x21); /* GIF Extension code */
      91           1 :         avio_w8(pb, 0xff); /* Application Extension Label */
      92           1 :         avio_w8(pb, 0x0b); /* Length of Application Block */
      93           1 :         avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
      94           1 :         avio_w8(pb, 0x03); /* Length of Data Sub-Block */
      95           1 :         avio_w8(pb, 0x01);
      96           1 :         avio_wl16(pb, (uint16_t)loop_count);
      97           1 :         avio_w8(pb, 0x00); /* Data Sub-block Terminator */
      98             :     }
      99             : 
     100           1 :     avio_flush(pb);
     101           1 :     return 0;
     102             : }
     103             : 
     104             : typedef struct GIFContext {
     105             :     AVClass *class;
     106             :     int loop;
     107             :     int last_delay;
     108             :     AVPacket *prev_pkt;
     109             :     int duration;
     110             : } GIFContext;
     111             : 
     112           1 : static int gif_write_header(AVFormatContext *s)
     113             : {
     114           1 :     GIFContext *gif = s->priv_data;
     115             :     AVCodecParameters *video_par;
     116             :     uint32_t palette[AVPALETTE_COUNT];
     117             : 
     118           2 :     if (s->nb_streams != 1 ||
     119           2 :         s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
     120           1 :         s->streams[0]->codecpar->codec_id   != AV_CODEC_ID_GIF) {
     121           0 :         av_log(s, AV_LOG_ERROR,
     122             :                "GIF muxer supports only a single video GIF stream.\n");
     123           0 :         return AVERROR(EINVAL);
     124             :     }
     125             : 
     126           1 :     video_par = s->streams[0]->codecpar;
     127             : 
     128           1 :     avpriv_set_pts_info(s->streams[0], 64, 1, 100);
     129           1 :     if (avpriv_set_systematic_pal2(palette, video_par->format) < 0) {
     130           0 :         av_assert0(video_par->format == AV_PIX_FMT_PAL8);
     131             :         /* delay header writing: we wait for the first palette to put it
     132             :          * globally */
     133             :     } else {
     134           1 :         gif_image_write_header(s->pb, s->streams[0], gif->loop, palette);
     135             :     }
     136             : 
     137           1 :     return 0;
     138             : }
     139             : 
     140          25 : static int flush_packet(AVFormatContext *s, AVPacket *new)
     141             : {
     142          25 :     GIFContext *gif = s->priv_data;
     143             :     int size, bcid;
     144          25 :     AVIOContext *pb = s->pb;
     145             :     const uint32_t *palette;
     146          25 :     AVPacket *pkt = gif->prev_pkt;
     147             : 
     148          25 :     if (!pkt)
     149           0 :         return 0;
     150             : 
     151             :     /* Mark one colour as transparent if the input palette contains at least
     152             :      * one colour that is more than 50% transparent. */
     153          25 :     palette = (uint32_t*)av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
     154          25 :     if (palette && size != AVPALETTE_SIZE) {
     155           0 :         av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
     156           0 :         return AVERROR_INVALIDDATA;
     157             :     }
     158          25 :     bcid = get_palette_transparency_index(palette);
     159             : 
     160          25 :     if (new && new->pts != AV_NOPTS_VALUE)
     161          24 :         gif->duration = av_clip_uint16(new->pts - gif->prev_pkt->pts);
     162           1 :     else if (!new && gif->last_delay >= 0)
     163           0 :         gif->duration = gif->last_delay;
     164             : 
     165             :     /* graphic control extension block */
     166          25 :     avio_w8(pb, 0x21);
     167          25 :     avio_w8(pb, 0xf9);
     168          25 :     avio_w8(pb, 0x04); /* block size */
     169          25 :     avio_w8(pb, 1<<2 | (bcid >= 0));
     170          25 :     avio_wl16(pb, gif->duration);
     171          25 :     avio_w8(pb, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid);
     172          25 :     avio_w8(pb, 0x00);
     173             : 
     174          25 :     avio_write(pb, pkt->data, pkt->size);
     175             : 
     176          25 :     av_packet_unref(gif->prev_pkt);
     177          25 :     if (new)
     178          24 :         av_packet_ref(gif->prev_pkt, new);
     179             : 
     180          25 :     return 0;
     181             : }
     182             : 
     183          25 : static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
     184             : {
     185          25 :     GIFContext *gif = s->priv_data;
     186          25 :     AVStream *video_st = s->streams[0];
     187             : 
     188          25 :     if (!gif->prev_pkt) {
     189           1 :         gif->prev_pkt = av_packet_alloc();
     190           1 :         if (!gif->prev_pkt)
     191           0 :             return AVERROR(ENOMEM);
     192             : 
     193             :         /* Write the first palette as global palette */
     194           1 :         if (video_st->codecpar->format == AV_PIX_FMT_PAL8) {
     195             :             int size;
     196           0 :             void *palette = av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
     197             : 
     198           0 :             if (!palette) {
     199           0 :                 av_log(s, AV_LOG_ERROR, "PAL8 packet is missing palette in extradata\n");
     200           0 :                 return AVERROR_INVALIDDATA;
     201             :             }
     202           0 :             if (size != AVPALETTE_SIZE) {
     203           0 :                 av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
     204           0 :                 return AVERROR_INVALIDDATA;
     205             :             }
     206           0 :             gif_image_write_header(s->pb, video_st, gif->loop, palette);
     207             :         }
     208             : 
     209           1 :         return av_packet_ref(gif->prev_pkt, pkt);
     210             :     }
     211          24 :     return flush_packet(s, pkt);
     212             : }
     213             : 
     214           1 : static int gif_write_trailer(AVFormatContext *s)
     215             : {
     216           1 :     GIFContext *gif = s->priv_data;
     217           1 :     AVIOContext *pb = s->pb;
     218             : 
     219           1 :     flush_packet(s, NULL);
     220           1 :     av_freep(&gif->prev_pkt);
     221           1 :     avio_w8(pb, 0x3b);
     222             : 
     223           1 :     return 0;
     224             : }
     225             : 
     226             : #define OFFSET(x) offsetof(GIFContext, x)
     227             : #define ENC AV_OPT_FLAG_ENCODING_PARAM
     228             : static const AVOption options[] = {
     229             :     { "loop", "Number of times to loop the output: -1 - no loop, 0 - infinite loop", OFFSET(loop),
     230             :       AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 65535, ENC },
     231             :     { "final_delay", "Force delay (in centiseconds) after the last frame", OFFSET(last_delay),
     232             :       AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC },
     233             :     { NULL },
     234             : };
     235             : 
     236             : static const AVClass gif_muxer_class = {
     237             :     .class_name = "GIF muxer",
     238             :     .item_name  = av_default_item_name,
     239             :     .version    = LIBAVUTIL_VERSION_INT,
     240             :     .option     = options,
     241             : };
     242             : 
     243             : AVOutputFormat ff_gif_muxer = {
     244             :     .name           = "gif",
     245             :     .long_name      = NULL_IF_CONFIG_SMALL("GIF Animation"),
     246             :     .mime_type      = "image/gif",
     247             :     .extensions     = "gif",
     248             :     .priv_data_size = sizeof(GIFContext),
     249             :     .audio_codec    = AV_CODEC_ID_NONE,
     250             :     .video_codec    = AV_CODEC_ID_GIF,
     251             :     .write_header   = gif_write_header,
     252             :     .write_packet   = gif_write_packet,
     253             :     .write_trailer  = gif_write_trailer,
     254             :     .priv_class     = &gif_muxer_class,
     255             :     .flags          = AVFMT_VARIABLE_FPS,
     256             : };

Generated by: LCOV version 1.13