LCOV - code coverage report
Current view: top level - libavformat - webpenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 101 0.0 %
Date: 2017-12-18 06:23:41 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * webp muxer
       3             :  * Copyright (c) 2014 Michael Niedermayer
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/intreadwrite.h"
      23             : #include "libavutil/opt.h"
      24             : #include "avformat.h"
      25             : #include "internal.h"
      26             : 
      27             : typedef struct WebpContext{
      28             :     AVClass *class;
      29             :     int frame_count;
      30             :     AVPacket last_pkt;
      31             :     int loop;
      32             :     int wrote_webp_header;
      33             :     int using_webp_anim_encoder;
      34             : } WebpContext;
      35             : 
      36           0 : static int webp_write_header(AVFormatContext *s)
      37             : {
      38             :     AVStream *st;
      39             : 
      40           0 :     if (s->nb_streams != 1) {
      41           0 :         av_log(s, AV_LOG_ERROR, "Only exactly 1 stream is supported\n");
      42           0 :         return AVERROR(EINVAL);
      43             :     }
      44           0 :     st = s->streams[0];
      45           0 :     if (st->codecpar->codec_id != AV_CODEC_ID_WEBP) {
      46           0 :         av_log(s, AV_LOG_ERROR, "Only WebP is supported\n");
      47           0 :         return AVERROR(EINVAL);
      48             :     }
      49           0 :     avpriv_set_pts_info(st, 24, 1, 1000);
      50             : 
      51           0 :     return 0;
      52             : }
      53             : 
      54           0 : static int is_animated_webp_packet(AVPacket *pkt)
      55             : {
      56           0 :     if (pkt->size) {
      57           0 :         int skip = 0;
      58           0 :         unsigned flags = 0;
      59             : 
      60           0 :         if (pkt->size < 4)
      61           0 :             return 0;
      62           0 :         if (AV_RL32(pkt->data) == AV_RL32("RIFF"))
      63           0 :             skip = 12;
      64             : 
      65           0 :         if (pkt->size < skip + 4)
      66           0 :             return 0;
      67           0 :         if (AV_RL32(pkt->data + skip) == AV_RL32("VP8X")) {
      68           0 :             flags |= pkt->data[skip + 4 + 4];
      69             :         }
      70             : 
      71           0 :         if (flags & 2)  // ANIMATION_FLAG is on
      72           0 :             return 1;
      73             :     }
      74           0 :     return 0;
      75             : }
      76             : 
      77           0 : static int flush(AVFormatContext *s, int trailer, int64_t pts)
      78             : {
      79           0 :     WebpContext *w = s->priv_data;
      80           0 :     AVStream *st = s->streams[0];
      81             : 
      82           0 :     if (w->last_pkt.size) {
      83           0 :         int skip = 0;
      84           0 :         unsigned flags = 0;
      85           0 :         int vp8x = 0;
      86             : 
      87           0 :         if (w->last_pkt.size < 4)
      88           0 :             return 0;
      89           0 :         if (AV_RL32(w->last_pkt.data) == AV_RL32("RIFF"))
      90           0 :             skip = 12;
      91             : 
      92           0 :         if (w->last_pkt.size < skip + 4)
      93           0 :             return 0;  // Safe to do this as a valid WebP bitstream is >=30 bytes.
      94           0 :         if (AV_RL32(w->last_pkt.data + skip) == AV_RL32("VP8X")) {
      95           0 :             flags |= w->last_pkt.data[skip + 4 + 4];
      96           0 :             vp8x = 1;
      97           0 :             skip += AV_RL32(w->last_pkt.data + skip + 4) + 8;
      98             :         }
      99             : 
     100           0 :         if (!w->wrote_webp_header) {
     101           0 :             avio_write(s->pb, "RIFF\0\0\0\0WEBP", 12);
     102           0 :             w->wrote_webp_header = 1;
     103           0 :             if (w->frame_count > 1)  // first non-empty packet
     104           0 :                 w->frame_count = 1;  // so we don't count previous empty packets.
     105             :         }
     106             : 
     107           0 :         if (w->frame_count == 1) {
     108           0 :             if (!trailer) {
     109           0 :                 vp8x = 1;
     110           0 :                 flags |= 2 + 16;
     111             :             }
     112             : 
     113           0 :             if (vp8x) {
     114           0 :                 avio_write(s->pb, "VP8X", 4);
     115           0 :                 avio_wl32(s->pb, 10);
     116           0 :                 avio_w8(s->pb, flags);
     117           0 :                 avio_wl24(s->pb, 0);
     118           0 :                 avio_wl24(s->pb, st->codecpar->width - 1);
     119           0 :                 avio_wl24(s->pb, st->codecpar->height - 1);
     120             :             }
     121           0 :             if (!trailer) {
     122           0 :                 avio_write(s->pb, "ANIM", 4);
     123           0 :                 avio_wl32(s->pb, 6);
     124           0 :                 avio_wl32(s->pb, 0xFFFFFFFF);
     125           0 :                 avio_wl16(s->pb, w->loop);
     126             :             }
     127             :         }
     128             : 
     129           0 :         if (w->frame_count > trailer) {
     130           0 :             avio_write(s->pb, "ANMF", 4);
     131           0 :             avio_wl32(s->pb, 16 + w->last_pkt.size - skip);
     132           0 :             avio_wl24(s->pb, 0);
     133           0 :             avio_wl24(s->pb, 0);
     134           0 :             avio_wl24(s->pb, st->codecpar->width - 1);
     135           0 :             avio_wl24(s->pb, st->codecpar->height - 1);
     136           0 :             if (w->last_pkt.pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
     137           0 :                 avio_wl24(s->pb, pts - w->last_pkt.pts);
     138             :             } else
     139           0 :                 avio_wl24(s->pb, w->last_pkt.duration);
     140           0 :             avio_w8(s->pb, 0);
     141             :         }
     142           0 :         avio_write(s->pb, w->last_pkt.data + skip, w->last_pkt.size - skip);
     143           0 :         av_packet_unref(&w->last_pkt);
     144             :     }
     145             : 
     146           0 :     return 0;
     147             : }
     148             : 
     149           0 : static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
     150             : {
     151           0 :     WebpContext *w = s->priv_data;
     152           0 :     w->using_webp_anim_encoder |= is_animated_webp_packet(pkt);
     153             : 
     154           0 :     if (w->using_webp_anim_encoder) {
     155           0 :         avio_write(s->pb, pkt->data, pkt->size);
     156           0 :         w->wrote_webp_header = 1;  // for good measure
     157             :     } else {
     158             :         int ret;
     159           0 :         if ((ret = flush(s, 0, pkt->pts)) < 0)
     160           0 :             return ret;
     161           0 :         av_packet_ref(&w->last_pkt, pkt);
     162             :     }
     163           0 :     ++w->frame_count;
     164             : 
     165           0 :     return 0;
     166             : }
     167             : 
     168           0 : static int webp_write_trailer(AVFormatContext *s)
     169             : {
     170             :     unsigned filesize;
     171           0 :     WebpContext *w = s->priv_data;
     172             : 
     173           0 :     if (w->using_webp_anim_encoder) {
     174           0 :         if ((w->frame_count > 1) && w->loop) {  // Write loop count.
     175           0 :             avio_seek(s->pb, 42, SEEK_SET);
     176           0 :             avio_wl16(s->pb, w->loop);
     177             :         }
     178             :     } else {
     179             :         int ret;
     180           0 :         if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0)
     181           0 :             return ret;
     182             : 
     183           0 :         filesize = avio_tell(s->pb);
     184           0 :         avio_seek(s->pb, 4, SEEK_SET);
     185           0 :         avio_wl32(s->pb, filesize - 8);
     186             :         // Note: without the following, avio only writes 8 bytes to the file.
     187           0 :         avio_seek(s->pb, filesize, SEEK_SET);
     188             :     }
     189             : 
     190           0 :     return 0;
     191             : }
     192             : 
     193             : #define OFFSET(x) offsetof(WebpContext, x)
     194             : #define ENC AV_OPT_FLAG_ENCODING_PARAM
     195             : static const AVOption options[] = {
     196             :     { "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop),
     197             :       AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC },
     198             :     { NULL },
     199             : };
     200             : 
     201             : static const AVClass webp_muxer_class = {
     202             :     .class_name = "WebP muxer",
     203             :     .item_name  = av_default_item_name,
     204             :     .version    = LIBAVUTIL_VERSION_INT,
     205             :     .option     = options,
     206             : };
     207             : AVOutputFormat ff_webp_muxer = {
     208             :     .name           = "webp",
     209             :     .long_name      = NULL_IF_CONFIG_SMALL("WebP"),
     210             :     .extensions     = "webp",
     211             :     .priv_data_size = sizeof(WebpContext),
     212             :     .video_codec    = AV_CODEC_ID_WEBP,
     213             :     .write_header   = webp_write_header,
     214             :     .write_packet   = webp_write_packet,
     215             :     .write_trailer  = webp_write_trailer,
     216             :     .priv_class     = &webp_muxer_class,
     217             :     .flags          = AVFMT_VARIABLE_FPS,
     218             : };

Generated by: LCOV version 1.13