]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/segment.c
avrndec: add DR1 capability flag
[ffmpeg] / libavformat / segment.c
index 744c14799044312a192579f94861c40658fde169..e148dc020b9d83fa9df870796c87ecd350e99d24 100644 (file)
@@ -1,5 +1,4 @@
 /*
- * Generic segmenter
  * Copyright (c) 2011, Luca Barbato
  *
  * This file is part of Libav.
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+/**
+ * @file generic segmenter
+ * M3U8 specification can be find here:
+ * @url{http://tools.ietf.org/id/draft-pantos-http-live-streaming-08.txt}
+ */
+
 #include <float.h>
 
 #include "avformat.h"
 #include "libavutil/mathematics.h"
 
 typedef enum {
+    LIST_TYPE_UNDEFINED = -1,
     LIST_TYPE_FLAT = 0,
     LIST_TYPE_EXT,
+    LIST_TYPE_M3U8,
     LIST_TYPE_NB,
 } ListType;
 
 typedef struct {
     const AVClass *class;  /**< Class for private options. */
-    int number;
+    int segment_idx;       ///< index of the segment file to write, starting from 0
+    int segment_idx_wrap;  ///< number after which the index wraps
+    int segment_count;     ///< number of segment files already written
     AVFormatContext *avf;
     char *format;          ///< format to use for output segment files
     char *list;            ///< filename for the segment list file
     int   list_size;       ///< number of entries for the segment list file
+    double list_max_segment_time; ///< max segment time in the current list
     ListType list_type;    ///< set the list type
     AVIOContext *list_pb;  ///< list file put-byte context
-    int  wrap;             ///< number after which the index wraps
     char *time_str;        ///< segment duration specification string
     int64_t time;          ///< segment duration
     char *times_str;       ///< segment times specification string
@@ -64,14 +73,15 @@ static int segment_start(AVFormatContext *s)
     AVFormatContext *oc = seg->avf;
     int err = 0;
 
-    if (seg->wrap)
-        seg->number %= seg->wrap;
+    if (seg->segment_idx_wrap)
+        seg->segment_idx %= seg->segment_idx_wrap;
 
     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
-                              s->filename, seg->number++) < 0) {
+                              s->filename, seg->segment_idx++) < 0) {
         av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
         return AVERROR(EINVAL);
     }
+    seg->segment_count++;
 
     if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
                           &s->interrupt_callback, NULL)) < 0)
@@ -104,6 +114,38 @@ fail:
     return err;
 }
 
+static int segment_list_open(AVFormatContext *s)
+{
+    SegmentContext *seg = s->priv_data;
+    int ret;
+
+    ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
+                     &s->interrupt_callback, NULL);
+    if (ret < 0)
+        return ret;
+    seg->list_max_segment_time = 0;
+
+    if (seg->list_type == LIST_TYPE_M3U8) {
+        avio_printf(seg->list_pb, "#EXTM3U\n");
+        avio_printf(seg->list_pb, "#EXT-X-VERSION:4\n");
+    }
+
+    return ret;
+}
+
+static void segment_list_close(AVFormatContext *s)
+{
+    SegmentContext *seg = s->priv_data;
+
+    if (seg->list_type == LIST_TYPE_M3U8) {
+        avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%d\n",
+                    (int)ceil(seg->list_max_segment_time));
+        avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
+    }
+
+    avio_close(seg->list_pb);
+}
+
 static int segment_end(AVFormatContext *s)
 {
     SegmentContext *seg = s->priv_data;
@@ -118,10 +160,9 @@ static int segment_end(AVFormatContext *s)
                oc->filename);
 
     if (seg->list) {
-        if (seg->list_size && !(seg->number % seg->list_size)) {
-            avio_close(seg->list_pb);
-            if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
-                                  &s->interrupt_callback, NULL)) < 0)
+        if (seg->list_size && !(seg->segment_count % seg->list_size)) {
+            segment_list_close(s);
+            if ((ret = segment_list_open(s)) < 0)
                 goto end;
         }
 
@@ -129,7 +170,11 @@ static int segment_end(AVFormatContext *s)
             avio_printf(seg->list_pb, "%s\n", oc->filename);
         } else if (seg->list_type == LIST_TYPE_EXT) {
             avio_printf(seg->list_pb, "%s,%f,%f\n", oc->filename, seg->start_time, seg->end_time);
+        } else if (seg->list_type == LIST_TYPE_M3U8) {
+            avio_printf(seg->list_pb, "#EXTINF:%f,\n%s\n",
+                        seg->end_time - seg->start_time, oc->filename);
         }
+        seg->list_max_segment_time = FFMAX(seg->end_time - seg->start_time, seg->list_max_segment_time);
         avio_flush(seg->list_pb);
     }
 
@@ -201,7 +246,7 @@ static int seg_write_header(AVFormatContext *s)
     AVFormatContext *oc;
     int ret, i;
 
-    seg->number = 0;
+    seg->segment_count = 0;
 
     if (seg->time_str && seg->times_str) {
         av_log(s, AV_LOG_ERROR,
@@ -238,10 +283,15 @@ static int seg_write_header(AVFormatContext *s)
     if (!oc)
         return AVERROR(ENOMEM);
 
-    if (seg->list)
-        if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
-                              &s->interrupt_callback, NULL)) < 0)
+    if (seg->list) {
+        if (seg->list_type == LIST_TYPE_UNDEFINED) {
+            if      (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
+            else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
+            else                                      seg->list_type = LIST_TYPE_FLAT;
+        }
+        if ((ret = segment_list_open(s)) < 0)
             goto fail;
+    }
 
     for (i = 0; i< s->nb_streams; i++)
         seg->has_video +=
@@ -271,10 +321,11 @@ static int seg_write_header(AVFormatContext *s)
     oc->nb_streams = s->nb_streams;
 
     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
-                              s->filename, seg->number++) < 0) {
+                              s->filename, seg->segment_idx++) < 0) {
         ret = AVERROR(EINVAL);
         goto fail;
     }
+    seg->segment_count++;
 
     if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
                           &s->interrupt_callback, NULL)) < 0)
@@ -293,7 +344,7 @@ fail:
             avformat_free_context(oc);
         }
         if (seg->list)
-            avio_close(seg->list_pb);
+            segment_list_close(s);
     }
     return ret;
 }
@@ -307,9 +358,10 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
     int ret;
 
     if (seg->times) {
-        end_pts = seg->number <= seg->nb_times ? seg->times[seg->number-1] : INT64_MAX;
+        end_pts = seg->segment_count <= seg->nb_times ?
+            seg->times[seg->segment_count-1] : INT64_MAX;
     } else {
-        end_pts = seg->time * seg->number;
+        end_pts = seg->time * seg->segment_count;
     }
 
     /* if the segment has video, start a new segment *only* with a key video frame */
@@ -349,7 +401,7 @@ static int seg_write_trailer(struct AVFormatContext *s)
     AVFormatContext *oc = seg->avf;
     int ret = segment_end(s);
     if (seg->list)
-        avio_close(seg->list_pb);
+        segment_list_close(s);
 
     av_opt_free(seg);
     av_freep(&seg->times);
@@ -365,14 +417,15 @@ static int seg_write_trailer(struct AVFormatContext *s)
 static const AVOption options[] = {
     { "segment_format",    "set container format used for the segments", OFFSET(format),  AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
     { "segment_list",      "set the segment list filename",              OFFSET(list),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
-    { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT,  {.dbl = 5},     0, INT_MAX, E },
-    { "segment_list_type", "set the segment list type",                  OFFSET(list_type), AV_OPT_TYPE_INT,  {.dbl = LIST_TYPE_FLAT}, 0, LIST_TYPE_NB-1, E, "list_type" },
+    { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT,  {.dbl = 0},     0, INT_MAX, E },
+    { "segment_list_type", "set the segment list type",                  OFFSET(list_type), AV_OPT_TYPE_INT,  {.dbl = LIST_TYPE_UNDEFINED}, -1, LIST_TYPE_NB-1, E, "list_type" },
     { "flat", "flat format",     0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
     { "ext",  "extended format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_EXT  }, INT_MIN, INT_MAX, 0, "list_type" },
+    { "m3u8", "M3U8 format",     0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, 0, "list_type" },
     { "segment_time",      "set segment duration",                       OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
     { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta_str), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, E },
     { "segment_times",     "set segment split time points",              OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL},  0, 0,       E },
-    { "segment_wrap",      "set number after which the index wraps",     OFFSET(wrap),    AV_OPT_TYPE_INT,    {.dbl = 0},     0, INT_MAX, E },
+    { "segment_wrap",      "set number after which the index wraps",     OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
     { NULL },
 };