Merge commit '5e9c6ef8f3beb9ed7b271654a82349ac90fe43f2'
authorMichael Niedermayer <michaelni@gmx.at>
Wed, 14 Nov 2012 10:22:40 +0000 (11:22 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 14 Nov 2012 10:59:20 +0000 (11:59 +0100)
* commit '5e9c6ef8f3beb9ed7b271654a82349ac90fe43f2':
  x86: h264_weight_10bit: port to cpuflags
  libtheoraenc: add missing pixdesc.h header
  avcodec: remove ff_is_hwaccel_pix_fmt
  pixdesc: add av_pix_fmt_get_chroma_sub_sample
  hlsenc: stand alone hls segmenter

Conflicts:
doc/muxers.texi
libavcodec/ffv1enc.c
libavcodec/imgconvert.c
libavcodec/mpegvideo_enc.c
libavcodec/tiffenc.c
libavformat/Makefile
libavformat/allformats.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
14 files changed:
1  2 
doc/muxers.texi
libavcodec/avcodec.h
libavcodec/imgconvert.c
libavcodec/internal.h
libavcodec/libtheoraenc.c
libavcodec/mpegvideo.c
libavcodec/mpegvideo_enc.c
libavcodec/tiffenc.c
libavcodec/utils.c
libavformat/Makefile
libavformat/allformats.c
libavformat/hlsenc.c
libavutil/pixdesc.c
libavutil/pixdesc.h

diff --cc doc/muxers.texi
@@@ -96,72 -90,27 +96,93 @@@ ffmpeg -i INPUT -c:a pcm_u8 -c:v mpeg2v
  
  See also the @ref{crc} muxer.
  
 -avconv -i in.nut out.m3u8
 +@anchor{framemd5}
 +@section framemd5
 +
 +Per-packet MD5 testing format.
 +
 +This muxer computes and prints the MD5 hash for each audio
 +and video packet. By default audio frames are converted to signed
 +16-bit raw audio and video frames to raw video before computing the
 +hash.
 +
 +The output of the muxer consists of a line for each audio and video
 +packet of the form:
 +@example
 +@var{stream_index}, @var{packet_dts}, @var{packet_pts}, @var{packet_duration}, @var{packet_size}, @var{MD5}
 +@end example
 +
 +@var{MD5} is a hexadecimal number representing the computed MD5 hash
 +for the packet.
 +
 +For example to compute the MD5 of the audio and video frames in
 +@file{INPUT}, converted to raw audio and video packets, and store it
 +in the file @file{out.md5}:
 +@example
 +ffmpeg -i INPUT -f framemd5 out.md5
 +@end example
 +
 +To print the information to stdout, use the command:
 +@example
 +ffmpeg -i INPUT -f framemd5 -
 +@end example
 +
 +See also the @ref{md5} muxer.
 +
+ @anchor{hls}
+ @section hls
+ Apple HTTP Live Streaming muxer that segments MPEG-TS according to
+ the HTTP Live Streaming specification.
+ It creates a playlist file and numbered segment files. The output
+ filename specifies the playlist filename; the segment filenames
+ receive the same basename as the playlist, a sequential number and
+ a .ts extension.
+ @example
++ffmpeg -i in.nut out.m3u8
+ @end example
+ @table @option
+ @item -hls_time segment length in seconds
+ @item -hls_list_size maximum number of playlist entries
+ @item -hls_wrap number after which index wraps
+ @end table
 +@anchor{ico}
 +@section ico
 +
 +ICO file muxer.
 +
 +Microsoft's icon file format (ICO) has some strict limitations that should be noted:
 +
 +@itemize
 +@item
 +Size cannot exceed 256 pixels in any dimension
 +
 +@item
 +Only BMP and PNG images can be stored
 +
 +@item
 +If a BMP image is used, it must be one of the following pixel formats:
 +@example
 +BMP Bit Depth      FFmpeg Pixel Format
 +1bit               pal8
 +4bit               pal8
 +8bit               pal8
 +16bit              rgb555le
 +24bit              bgr24
 +32bit              bgra
 +@end example
 +
 +@item
 +If a BMP image is used, it must use the BITMAPINFOHEADER DIB header
 +
 +@item
 +If a PNG image is used, it must use the rgba pixel format
 +@end itemize
 +
  @anchor{image2}
  @section image2
  
Simple merge
@@@ -431,17 -415,18 +431,11 @@@ void avcodec_get_chroma_sub_sample(enu
      *v_shift = desc->log2_chroma_h;
  }
  
- int ff_is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
- {
-     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
-     return desc->flags & PIX_FMT_HWACCEL;
- }
 -int avpicture_fill(AVPicture *picture, uint8_t *ptr,
 +int avpicture_fill(AVPicture *picture, const uint8_t *ptr,
                     enum AVPixelFormat pix_fmt, int width, int height)
  {
 -    int ret;
 -
 -    if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
 -        return ret;
 -
 -    if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
 -        return ret;
 -
 -    return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
 +    return av_image_fill_arrays(picture->data, picture->linesize,
 +                                ptr, pix_fmt, width, height, 1);
  }
  
  int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, int width, int height,
Simple merge
   * and o_ prefixes on variables which are libogg types.
   */
  
 -/* Libav includes */
 +/* FFmpeg includes */
  #include "libavutil/common.h"
  #include "libavutil/intreadwrite.h"
+ #include "libavutil/pixdesc.h"
  #include "libavutil/log.h"
  #include "libavutil/base64.h"
  #include "avcodec.h"
@@@ -839,35 -865,44 +839,34 @@@ av_cold int ff_MPV_common_init(MpegEncC
      s->flags  = s->avctx->flags;
      s->flags2 = s->avctx->flags2;
  
 -    if (s->width && s->height) {
--        /* set chroma shifts */
-         avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
-                                       &s->chroma_y_shift);
 -        av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
 -                                         &s->chroma_x_shift,
 -                                         &s->chroma_y_shift);
++    /* set chroma shifts */
++    avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
  
 -        /* convert fourcc to upper case */
 -        s->codec_tag          = avpriv_toupper4(s->avctx->codec_tag);
 +    /* convert fourcc to upper case */
 +    s->codec_tag        = avpriv_toupper4(s->avctx->codec_tag);
 +    s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  
 -        s->stream_codec_tag   = avpriv_toupper4(s->avctx->stream_codec_tag);
 +    s->avctx->coded_frame = &s->current_picture.f;
  
 -        s->avctx->coded_frame = &s->current_picture.f;
 +    if (s->encoding) {
 +        if (s->msmpeg4_version) {
 +            FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
 +                                2 * 2 * (MAX_LEVEL + 1) *
 +                                (MAX_RUN + 1) * 2 * sizeof(int), fail);
 +        }
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  
 -        if (s->encoding) {
 -            if (s->msmpeg4_version) {
 -                FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
 -                                  2 * 2 * (MAX_LEVEL + 1) *
 -                                  (MAX_RUN + 1) * 2 * sizeof(int), fail);
 -            }
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
 -
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
 -                              64 * 32   * sizeof(int), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
 -                              64 * 32   * sizeof(int), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,
 -                              64 * 32 * 2 * sizeof(uint16_t), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,
 -                              64 * 32 * 2 * sizeof(uint16_t), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
 -                              MAX_PICTURE_COUNT * sizeof(Picture *), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
 -                              MAX_PICTURE_COUNT * sizeof(Picture *), fail);
 -
 -            if (s->avctx->noise_reduction) {
 -                FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
 -                                  2 * 64 * sizeof(uint16_t), fail);
 -            }
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,          64 * 32   * sizeof(int), fail)
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix,   64 * 32   * sizeof(int), fail)
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,          64 * 32   * sizeof(int), fail)
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,        64 * 32 * 2 * sizeof(uint16_t), fail)
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,        64 * 32 * 2 * sizeof(uint16_t), fail)
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,           MAX_PICTURE_COUNT * sizeof(Picture *), fail)
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
 +
 +        if (s->avctx->noise_reduction) {
 +            FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail);
          }
      }
  
@@@ -1762,13 -1782,12 +1761,13 @@@ void ff_print_debug_info(MpegEncContex
                                     (s->codec_id == AV_CODEC_ID_H264 ? 0 : 1);
          s->low_delay = 0; // needed to see the vectors without trashing the buffers
  
-         avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
-                                       &h_chroma_shift, &v_chroma_shift);
 -        av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
 -                                         &h_chroma_shift, &v_chroma_shift);
++        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
++
          for (i = 0; i < 3; i++) {
 -            memcpy(s->visualization_buffer[i], pict->data[i],
 -                   (i == 0) ? pict->linesize[i] * height:
 -                              pict->linesize[i] * height >> v_chroma_shift);
 +            size_t size= (i == 0) ? pict->linesize[i] * FFALIGN(height, 16):
 +                         pict->linesize[i] * FFALIGN(height, 16) >> v_chroma_shift;
 +            s->visualization_buffer[i]= av_realloc(s->visualization_buffer[i], size);
 +            memcpy(s->visualization_buffer[i], pict->data[i], size);
              pict->data[i] = s->visualization_buffer[i];
          }
          pict->type   = FF_BUFFER_TYPE_COPY;
@@@ -685,10 -602,8 +686,9 @@@ av_cold int ff_MPV_encode_init(AVCodecC
      if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
          s->inter_quant_bias = avctx->inter_quant_bias;
  
 -    av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
 -                                     &chroma_v_shift);
 +    av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
 +
-     avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
-                                   &chroma_v_shift);
++    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
  
      if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
          s->avctx->time_base.den > (1 << 16) - 1) {
@@@ -1074,8 -995,9 +1074,7 @@@ static int load_input_picture(MpegEncCo
              // empty
          } else {
              int h_chroma_shift, v_chroma_shift;
-             avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift,
-                                           &v_chroma_shift);
 -            av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
 -                                             &h_chroma_shift,
 -                                             &v_chroma_shift);
++            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  
              for (i = 0; i < 3; i++) {
                  int src_stride = pic_arg->linesize[i];
@@@ -293,10 -270,12 +293,9 @@@ static int encode_frame(AVCodecContext 
      case AV_PIX_FMT_YUV410P:
      case AV_PIX_FMT_YUV411P:
          s->photometric_interpretation = 6;
-         avcodec_get_chroma_sub_sample(avctx->pix_fmt,
-                 &shift_h, &shift_v);
 -        av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
 -                                         &shift_h, &shift_v);
 -        s->bpp = 8 + (16 >> (shift_h + shift_v));
++        avcodec_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v);
          s->subsampling[0] = 1 << shift_h;
          s->subsampling[1] = 1 << shift_v;
 -        s->bpp_tab_size = 3;
          is_yuv = 1;
          break;
      default:
Simple merge
@@@ -125,10 -111,8 +125,11 @@@ OBJS-$(CONFIG_H263_MUXER
  OBJS-$(CONFIG_H264_DEMUXER)              += h264dec.o rawdec.o
  OBJS-$(CONFIG_H264_MUXER)                += rawenc.o
  OBJS-$(CONFIG_HLS_DEMUXER)               += hls.o
+ OBJS-$(CONFIG_HLS_MUXER)                 += hlsenc.o mpegtsenc.o
 +OBJS-$(CONFIG_ICO_DEMUXER)               += icodec.o
 +OBJS-$(CONFIG_ICO_MUXER)                 += icoenc.o
  OBJS-$(CONFIG_IDCIN_DEMUXER)             += idcin.o
 +OBJS-$(CONFIG_IDF_DEMUXER)               += bintext.o
  OBJS-$(CONFIG_IFF_DEMUXER)               += iff.o
  OBJS-$(CONFIG_ILBC_DEMUXER)              += ilbc.o
  OBJS-$(CONFIG_ILBC_MUXER)                += ilbc.o
@@@ -114,10 -106,8 +114,10 @@@ void av_register_all(void
      REGISTER_MUXDEMUX (H261, h261);
      REGISTER_MUXDEMUX (H263, h263);
      REGISTER_MUXDEMUX (H264, h264);
-     REGISTER_DEMUXER  (HLS, hls);
+     REGISTER_MUXDEMUX (HLS, hls);
 +    REGISTER_MUXDEMUX (ICO, ico);
      REGISTER_DEMUXER  (IDCIN, idcin);
 +    REGISTER_DEMUXER  (IDF, idf);
      REGISTER_DEMUXER  (IFF, iff);
      REGISTER_MUXDEMUX (ILBC, ilbc);
      REGISTER_MUXDEMUX (IMAGE2, image2);
index 0000000,88f47e0..ecf07ef
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,324 +1,324 @@@
 - * This file is part of Libav.
+ /*
+  * Apple HTTP Live Streaming segmenter
+  * Copyright (c) 2012, Luca Barbato
+  *
 - * Libav is free software; you can redistribute it and/or
++ * This file is part of FFmpeg.
+  *
 - * Libav is distributed in the hope that it will be useful,
++ * FFmpeg is free software; you can redistribute it and/or
+  * modify it under the terms of the GNU Lesser General Public
+  * License as published by the Free Software Foundation; either
+  * version 2.1 of the License, or (at your option) any later version.
+  *
 - * License along with Libav; if not, write to the Free Software
++ * FFmpeg is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
++ * License along with FFmpeg; if not, write to the Free Software
+  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+  */
+ #include <float.h>
+ #include "libavutil/mathematics.h"
+ #include "libavutil/parseutils.h"
+ #include "libavutil/avstring.h"
+ #include "libavutil/opt.h"
+ #include "libavutil/log.h"
+ #include "avformat.h"
+ #include "internal.h"
+ typedef struct ListEntry {
+     char  name[1024];
+     int   duration;
+     struct ListEntry *next;
+ } ListEntry;
+ typedef struct HLSContext {
+     const AVClass *class;  // Class for private options.
+     int number;
+     AVOutputFormat *oformat;
+     AVFormatContext *avf;
+     float time;            // Set by a private option.
+     int  size;             // Set by a private option.
+     int  wrap;             // Set by a private option.
+     int64_t recording_time;
+     int has_video;
+     int64_t start_pts;
+     int64_t end_pts;
+     ListEntry *list;
+     ListEntry *end_list;
+     char *basename;
+     AVIOContext *pb;
+ } HLSContext;
+ static int hls_mux_init(AVFormatContext *s)
+ {
+     HLSContext *hls = s->priv_data;
+     AVFormatContext *oc;
+     int i;
+     hls->avf = oc = avformat_alloc_context();
+     if (!oc)
+         return AVERROR(ENOMEM);
+     oc->oformat            = hls->oformat;
+     oc->interrupt_callback = s->interrupt_callback;
+     for (i = 0; i < s->nb_streams; i++) {
+         AVStream *st;
+         if (!(st = avformat_new_stream(oc, NULL)))
+             return AVERROR(ENOMEM);
+         avcodec_copy_context(st->codec, s->streams[i]->codec);
+         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
+     }
+     return 0;
+ }
+ static int append_entry(HLSContext *hls, uint64_t duration)
+ {
+     ListEntry *en = av_malloc(sizeof(*en));
+     if (!en)
+         return AVERROR(ENOMEM);
+     av_get_frame_filename(en->name, sizeof(en->name), hls->basename,
+                           hls->number -1);
+     en->duration = duration;
+     en->next     = NULL;
+     if (!hls->list)
+         hls->list = en;
+     else
+         hls->end_list->next = en;
+     hls->end_list = en;
+     if (hls->number >= hls->size) {
+         en = hls->list;
+         hls->list = en->next;
+         av_free(en);
+     }
+     return 0;
+ }
+ static void free_entries(HLSContext *hls)
+ {
+     ListEntry *p = hls->list, *en;
+     while(p) {
+         en = p;
+         p = p->next;
+         av_free(en);
+     }
+ }
+ static int hls_window(AVFormatContext *s, int last)
+ {
+     HLSContext *hls = s->priv_data;
+     ListEntry *en;
+     int ret = 0;
+     if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
+                           &s->interrupt_callback, NULL)) < 0)
+         goto fail;
+     avio_printf(hls->pb, "#EXTM3U\n");
+     avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
+     avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", (int)hls->time);
+     avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
+                 FFMAX(0, hls->number - hls->size));
+     for (en = hls->list; en; en = en->next) {
+         avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
+         avio_printf(hls->pb, "%s\n", en->name);
+     }
+     if (last)
+         avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
+ fail:
+     avio_closep(&hls->pb);
+     return ret;
+ }
+ static int hls_start(AVFormatContext *s)
+ {
+     HLSContext *c = s->priv_data;
+     AVFormatContext *oc = c->avf;
+     int err = 0;
+     if (c->wrap)
+         c->number %= c->wrap;
+     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
+                               c->basename, c->number++) < 0)
+         return AVERROR(EINVAL);
+     if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+                           &s->interrupt_callback, NULL)) < 0)
+         return err;
+     if (oc->oformat->priv_class && oc->priv_data)
+         av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
+     return 0;
+ }
+ static int hls_write_header(AVFormatContext *s)
+ {
+     HLSContext *hls = s->priv_data;
+     int ret, i;
+     char *p;
+     const char *pattern = "%d.ts";
+     int basename_size = strlen(s->filename) + strlen(pattern);
+     hls->number      = 0;
+     hls->recording_time = hls->time * 1000000;
+     hls->start_pts      = AV_NOPTS_VALUE;
+     for (i = 0; i < s->nb_streams; i++)
+         hls->has_video +=
+             s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
+     if (hls->has_video > 1)
+         av_log(s, AV_LOG_WARNING,
+                "More than a single video stream present, "
+                "expect issues decoding it.\n");
+     hls->oformat = av_guess_format("mpegts", NULL, NULL);
+     if (!hls->oformat) {
+         ret = AVERROR_MUXER_NOT_FOUND;
+         goto fail;
+     }
+     hls->basename = av_malloc(basename_size);
+     if (!hls->basename) {
+         ret = AVERROR(ENOMEM);
+         goto fail;
+     }
+     strcpy(hls->basename, s->filename);
+     p = strrchr(hls->basename, '.');
+     if (p)
+         *p = '\0';
+     av_strlcat(hls->basename, "%d.ts", basename_size);
+     if ((ret = hls_mux_init(s)) < 0)
+         goto fail;
+     if ((ret = hls_start(s)) < 0)
+         goto fail;
+     if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
+         return ret;
+ fail:
+     if (ret) {
+         av_free(hls->basename);
+         if (hls->avf)
+             avformat_free_context(hls->avf);
+     }
+     return ret;
+ }
+ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
+ {
+     HLSContext *hls = s->priv_data;
+     AVFormatContext *oc = hls->avf;
+     AVStream *st = s->streams[pkt->stream_index];
+     int64_t end_pts = hls->recording_time * hls->number;
+     int ret;
+     if (hls->start_pts == AV_NOPTS_VALUE) {
+         hls->start_pts = pkt->pts;
+         hls->end_pts   = pkt->pts;
+     }
+     end_pts += hls->start_pts;
+     if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)      &&
+         av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 &&
+         pkt->flags & AV_PKT_FLAG_KEY) {
+         append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
+                                      st->time_base.num,
+                                      st->time_base.den));
+         hls->end_pts = pkt->pts;
+         av_write_frame(oc, NULL); /* Flush any buffered data */
+         avio_close(oc->pb);
+         ret = hls_start(s);
+         if (ret)
+             return ret;
+         oc = hls->avf;
+         if ((ret = hls_window(s, 0)) < 0)
+             return ret;
+     }
+     ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
+     return ret;
+ }
+ static int hls_write_trailer(struct AVFormatContext *s)
+ {
+     HLSContext *hls = s->priv_data;
+     AVFormatContext *oc = hls->avf;
+     av_write_trailer(oc);
+     avio_closep(&oc->pb);
+     avformat_free_context(oc);
+     av_free(hls->basename);
+     hls_window(s, 1);
+     free_entries(hls);
+     avio_close(hls->pb);
+     return 0;
+ }
+ #define OFFSET(x) offsetof(HLSContext, x)
+ #define E AV_OPT_FLAG_ENCODING_PARAM
+ static const AVOption options[] = {
+     {"hls_time",      "segment length in seconds",               OFFSET(time),    AV_OPT_TYPE_FLOAT,  {.dbl = 2},     0, FLT_MAX, E},
+     {"hls_list_size", "maximum number of playlist entries",      OFFSET(size),    AV_OPT_TYPE_INT,    {.i64 = 5},     0, INT_MAX, E},
+     {"hls_wrap",      "number after which the index wraps",      OFFSET(wrap),    AV_OPT_TYPE_INT,    {.i64 = 0},     0, INT_MAX, E},
+     { NULL },
+ };
+ static const AVClass hls_class = {
+     .class_name = "hls muxer",
+     .item_name  = av_default_item_name,
+     .option     = options,
+     .version    = LIBAVUTIL_VERSION_INT,
+ };
+ AVOutputFormat ff_hls_muxer = {
+     .name           = "hls",
+     .long_name      = NULL_IF_CONFIG_SMALL("hls"),
+     .extensions     = "m3u8",
+     .priv_data_size = sizeof(HLSContext),
+     .audio_codec    = AV_CODEC_ID_MP2,
+     .video_codec    = AV_CODEC_ID_MPEG2VIDEO,
+     .flags          = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
+     .write_header   = hls_write_header,
+     .write_packet   = hls_write_packet,
+     .write_trailer  = hls_write_trailer,
+     .priv_class     = &hls_class,
+ };
Simple merge
Simple merge