]> git.sesse.net Git - ffmpeg/blob - libavformat/segment.c
Merge commit '9d4da474f5f40b019cb4cb931c8499deee586174'
[ffmpeg] / libavformat / segment.c
1 /*
2  * Copyright (c) 2011, Luca Barbato
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file generic segmenter
23  * M3U8 specification can be find here:
24  * @url{http://tools.ietf.org/id/draft-pantos-http-live-streaming}
25  */
26
27 /* #define DEBUG */
28
29 #include <float.h>
30
31 #include "avformat.h"
32 #include "internal.h"
33
34 #include "libavutil/avassert.h"
35 #include "libavutil/log.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/parseutils.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/timestamp.h"
41
42 typedef struct SegmentListEntry {
43     int index;
44     double start_time, end_time;
45     int64_t start_pts;
46     char filename[1024];
47     struct SegmentListEntry *next;
48 } SegmentListEntry;
49
50 typedef enum {
51     LIST_TYPE_UNDEFINED = -1,
52     LIST_TYPE_FLAT = 0,
53     LIST_TYPE_CSV,
54     LIST_TYPE_M3U8,
55     LIST_TYPE_EXT, ///< deprecated
56     LIST_TYPE_NB,
57 } ListType;
58
59 #define SEGMENT_LIST_FLAG_CACHE 1
60 #define SEGMENT_LIST_FLAG_LIVE  2
61
62 typedef struct {
63     const AVClass *class;  /**< Class for private options. */
64     int segment_idx;       ///< index of the segment file to write, starting from 0
65     int segment_idx_wrap;  ///< number after which the index wraps
66     int segment_count;     ///< number of segment files already written
67     AVOutputFormat *oformat;
68     AVFormatContext *avf;
69     char *format;          ///< format to use for output segment files
70     char *list;            ///< filename for the segment list file
71     int   list_flags;      ///< flags affecting list generation
72     int   list_size;       ///< number of entries for the segment list file
73     ListType list_type;    ///< set the list type
74     AVIOContext *list_pb;  ///< list file put-byte context
75     char *time_str;        ///< segment duration specification string
76     int64_t time;          ///< segment duration
77
78     char *times_str;       ///< segment times specification string
79     int64_t *times;        ///< list of segment interval specification
80     int nb_times;          ///< number of elments in the times array
81
82     char *frames_str;      ///< segment frame numbers specification string
83     int *frames;           ///< list of frame number specification
84     int nb_frames;         ///< number of elments in the frames array
85     int frame_count;
86
87     char *time_delta_str;  ///< approximation value duration used for the segment times
88     int64_t time_delta;
89     int  individual_header_trailer; /**< Set by a private option. */
90     int  write_header_trailer; /**< Set by a private option. */
91
92     int reset_timestamps;  ///< reset timestamps at the begin of each segment
93     char *reference_stream_specifier; ///< reference stream specifier
94     int   reference_stream_index;
95
96     SegmentListEntry cur_entry;
97     SegmentListEntry *segment_list_entries;
98     SegmentListEntry *segment_list_entries_end;
99
100     int is_first_pkt;      ///< tells if it is the first packet in the segment
101 } SegmentContext;
102
103 static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
104 {
105     int needs_quoting = !!str[strcspn(str, "\",\n\r")];
106
107     if (needs_quoting)
108         avio_w8(ctx, '"');
109
110     for (; *str; str++) {
111         if (*str == '"')
112             avio_w8(ctx, '"');
113         avio_w8(ctx, *str);
114     }
115     if (needs_quoting)
116         avio_w8(ctx, '"');
117 }
118
119 static int segment_mux_init(AVFormatContext *s)
120 {
121     SegmentContext *seg = s->priv_data;
122     AVFormatContext *oc;
123     int i;
124
125     seg->avf = oc = avformat_alloc_context();
126     if (!oc)
127         return AVERROR(ENOMEM);
128
129     oc->oformat            = seg->oformat;
130     oc->interrupt_callback = s->interrupt_callback;
131
132     for (i = 0; i < s->nb_streams; i++) {
133         AVStream *st;
134         AVCodecContext *icodec, *ocodec;
135
136         if (!(st = avformat_new_stream(oc, NULL)))
137             return AVERROR(ENOMEM);
138         icodec = s->streams[i]->codec;
139         ocodec = st->codec;
140         avcodec_copy_context(ocodec, icodec);
141         if (!oc->oformat->codec_tag ||
142             av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == ocodec->codec_id ||
143             av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0) {
144             ocodec->codec_tag = icodec->codec_tag;
145         } else {
146             ocodec->codec_tag = 0;
147         }
148         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
149     }
150
151     return 0;
152 }
153
154 static int set_segment_filename(AVFormatContext *s)
155 {
156     SegmentContext *seg = s->priv_data;
157     AVFormatContext *oc = seg->avf;
158
159     if (seg->segment_idx_wrap)
160         seg->segment_idx %= seg->segment_idx_wrap;
161     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
162                               s->filename, seg->segment_idx) < 0) {
163         av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
164         return AVERROR(EINVAL);
165     }
166     av_strlcpy(seg->cur_entry.filename, oc->filename, sizeof(seg->cur_entry.filename));
167     return 0;
168 }
169
170 static int segment_start(AVFormatContext *s, int write_header)
171 {
172     SegmentContext *seg = s->priv_data;
173     AVFormatContext *oc = seg->avf;
174     int err = 0;
175
176     if (write_header) {
177         avformat_free_context(oc);
178         seg->avf = NULL;
179         if ((err = segment_mux_init(s)) < 0)
180             return err;
181         oc = seg->avf;
182     }
183
184     seg->segment_idx++;
185     if ((err = set_segment_filename(s)) < 0)
186         return err;
187     seg->segment_count++;
188
189     if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
190                           &s->interrupt_callback, NULL)) < 0)
191         return err;
192
193     if (oc->oformat->priv_class && oc->priv_data)
194         av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
195
196     if (write_header) {
197         if ((err = avformat_write_header(oc, NULL)) < 0)
198             return err;
199     }
200
201     seg->is_first_pkt = 1;
202     return 0;
203 }
204
205 static int segment_list_open(AVFormatContext *s)
206 {
207     SegmentContext *seg = s->priv_data;
208     int ret;
209
210     ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
211                      &s->interrupt_callback, NULL);
212     if (ret < 0)
213         return ret;
214
215     if (seg->list_type == LIST_TYPE_M3U8 && seg->segment_list_entries) {
216         SegmentListEntry *entry;
217         double max_duration = 0;
218
219         avio_printf(seg->list_pb, "#EXTM3U\n");
220         avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
221         avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->segment_list_entries->index);
222         avio_printf(seg->list_pb, "#EXT-X-ALLOW-CACHE:%s\n",
223                     seg->list_flags & SEGMENT_LIST_FLAG_CACHE ? "YES" : "NO");
224
225         for (entry = seg->segment_list_entries; entry; entry = entry->next)
226             max_duration = FFMAX(max_duration, entry->end_time - entry->start_time);
227         avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%"PRId64"\n", (int64_t)ceil(max_duration));
228     }
229
230     return ret;
231 }
232
233 static void segment_list_print_entry(AVIOContext      *list_ioctx,
234                                      ListType          list_type,
235                                      const SegmentListEntry *list_entry)
236 {
237     switch (list_type) {
238     case LIST_TYPE_FLAT:
239         avio_printf(list_ioctx, "%s\n", list_entry->filename);
240         break;
241     case LIST_TYPE_CSV:
242     case LIST_TYPE_EXT:
243         print_csv_escaped_str(list_ioctx, list_entry->filename);
244         avio_printf(list_ioctx, ",%f,%f\n", list_entry->start_time, list_entry->end_time);
245         break;
246     case LIST_TYPE_M3U8:
247         avio_printf(list_ioctx, "#EXTINF:%f,\n%s\n",
248                     list_entry->end_time - list_entry->start_time, list_entry->filename);
249         break;
250     default:
251         av_assert0(!"Invalid list type");
252     }
253 }
254
255 static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
256 {
257     SegmentContext *seg = s->priv_data;
258     AVFormatContext *oc = seg->avf;
259     int ret = 0;
260
261     av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
262     if (write_trailer)
263         ret = av_write_trailer(oc);
264
265     if (ret < 0)
266         av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
267                oc->filename);
268
269     if (seg->list) {
270         if (seg->list_size || seg->list_type == LIST_TYPE_M3U8) {
271             SegmentListEntry *entry = av_mallocz(sizeof(*entry));
272             if (!entry) {
273                 ret = AVERROR(ENOMEM);
274                 goto end;
275             }
276
277             /* append new element */
278             memcpy(entry, &seg->cur_entry, sizeof(*entry));
279             if (!seg->segment_list_entries)
280                 seg->segment_list_entries = seg->segment_list_entries_end = entry;
281             else
282                 seg->segment_list_entries_end->next = entry;
283             seg->segment_list_entries_end = entry;
284
285             /* drop first item */
286             if (seg->list_size && seg->segment_count > seg->list_size) {
287                 entry = seg->segment_list_entries;
288                 seg->segment_list_entries = seg->segment_list_entries->next;
289                 av_freep(&entry);
290             }
291
292             avio_close(seg->list_pb);
293             if ((ret = segment_list_open(s)) < 0)
294                 goto end;
295             for (entry = seg->segment_list_entries; entry; entry = entry->next)
296                 segment_list_print_entry(seg->list_pb, seg->list_type, entry);
297             if (seg->list_type == LIST_TYPE_M3U8 && is_last)
298                 avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
299         } else {
300             segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry);
301         }
302         avio_flush(seg->list_pb);
303     }
304
305 end:
306     avio_close(oc->pb);
307
308     return ret;
309 }
310
311 static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
312                        const char *times_str)
313 {
314     char *p;
315     int i, ret = 0;
316     char *times_str1 = av_strdup(times_str);
317     char *saveptr = NULL;
318
319     if (!times_str1)
320         return AVERROR(ENOMEM);
321
322 #define FAIL(err) ret = err; goto end
323
324     *nb_times = 1;
325     for (p = times_str1; *p; p++)
326         if (*p == ',')
327             (*nb_times)++;
328
329     *times = av_malloc(sizeof(**times) * *nb_times);
330     if (!*times) {
331         av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
332         FAIL(AVERROR(ENOMEM));
333     }
334
335     p = times_str1;
336     for (i = 0; i < *nb_times; i++) {
337         int64_t t;
338         char *tstr = av_strtok(p, ",", &saveptr);
339         p = NULL;
340
341         if (!tstr || !tstr[0]) {
342             av_log(log_ctx, AV_LOG_ERROR, "Empty time specification in times list %s\n",
343                    times_str);
344             FAIL(AVERROR(EINVAL));
345         }
346
347         ret = av_parse_time(&t, tstr, 1);
348         if (ret < 0) {
349             av_log(log_ctx, AV_LOG_ERROR,
350                    "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
351             FAIL(AVERROR(EINVAL));
352         }
353         (*times)[i] = t;
354
355         /* check on monotonicity */
356         if (i && (*times)[i-1] > (*times)[i]) {
357             av_log(log_ctx, AV_LOG_ERROR,
358                    "Specified time %f is greater than the following time %f\n",
359                    (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
360             FAIL(AVERROR(EINVAL));
361         }
362     }
363
364 end:
365     av_free(times_str1);
366     return ret;
367 }
368
369 static int parse_frames(void *log_ctx, int **frames, int *nb_frames,
370                         const char *frames_str)
371 {
372     char *p;
373     int i, ret = 0;
374     char *frames_str1 = av_strdup(frames_str);
375     char *saveptr = NULL;
376
377     if (!frames_str1)
378         return AVERROR(ENOMEM);
379
380 #define FAIL(err) ret = err; goto end
381
382     *nb_frames = 1;
383     for (p = frames_str1; *p; p++)
384         if (*p == ',')
385             (*nb_frames)++;
386
387     *frames = av_malloc(sizeof(**frames) * *nb_frames);
388     if (!*frames) {
389         av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced frames array\n");
390         FAIL(AVERROR(ENOMEM));
391     }
392
393     p = frames_str1;
394     for (i = 0; i < *nb_frames; i++) {
395         long int f;
396         char *tailptr;
397         char *fstr = av_strtok(p, ",", &saveptr);
398
399         p = NULL;
400         if (!fstr) {
401             av_log(log_ctx, AV_LOG_ERROR, "Empty frame specification in frame list %s\n",
402                    frames_str);
403             FAIL(AVERROR(EINVAL));
404         }
405         f = strtol(fstr, &tailptr, 10);
406         if (*tailptr || f <= 0 || f >= INT_MAX) {
407             av_log(log_ctx, AV_LOG_ERROR,
408                    "Invalid argument '%s', must be a positive integer <= INT64_MAX\n",
409                    fstr);
410             FAIL(AVERROR(EINVAL));
411         }
412         (*frames)[i] = f;
413
414         /* check on monotonicity */
415         if (i && (*frames)[i-1] > (*frames)[i]) {
416             av_log(log_ctx, AV_LOG_ERROR,
417                    "Specified frame %d is greater than the following frame %d\n",
418                    (*frames)[i], (*frames)[i-1]);
419             FAIL(AVERROR(EINVAL));
420         }
421     }
422
423 end:
424     av_free(frames_str1);
425     return ret;
426 }
427
428 static int open_null_ctx(AVIOContext **ctx)
429 {
430     int buf_size = 32768;
431     uint8_t *buf = av_malloc(buf_size);
432     if (!buf)
433         return AVERROR(ENOMEM);
434     *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
435     if (!*ctx) {
436         av_free(buf);
437         return AVERROR(ENOMEM);
438     }
439     return 0;
440 }
441
442 static void close_null_ctx(AVIOContext *pb)
443 {
444     av_free(pb->buffer);
445     av_free(pb);
446 }
447
448 static int select_reference_stream(AVFormatContext *s)
449 {
450     SegmentContext *seg = s->priv_data;
451     int ret, i;
452
453     seg->reference_stream_index = -1;
454     if (!strcmp(seg->reference_stream_specifier, "auto")) {
455         /* select first index of type with highest priority */
456         int type_index_map[AVMEDIA_TYPE_NB];
457         static const enum AVMediaType type_priority_list[] = {
458             AVMEDIA_TYPE_VIDEO,
459             AVMEDIA_TYPE_AUDIO,
460             AVMEDIA_TYPE_SUBTITLE,
461             AVMEDIA_TYPE_DATA,
462             AVMEDIA_TYPE_ATTACHMENT
463         };
464         enum AVMediaType type;
465
466         for (i = 0; i < AVMEDIA_TYPE_NB; i++)
467             type_index_map[i] = -1;
468
469         /* select first index for each type */
470         for (i = 0; i < s->nb_streams; i++) {
471             type = s->streams[i]->codec->codec_type;
472             if ((unsigned)type < AVMEDIA_TYPE_NB && type_index_map[type] == -1
473                 /* ignore attached pictures/cover art streams */
474                 && !(s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC))
475                 type_index_map[type] = i;
476         }
477
478         for (i = 0; i < FF_ARRAY_ELEMS(type_priority_list); i++) {
479             type = type_priority_list[i];
480             if ((seg->reference_stream_index = type_index_map[type]) >= 0)
481                 break;
482         }
483     } else {
484         for (i = 0; i < s->nb_streams; i++) {
485             ret = avformat_match_stream_specifier(s, s->streams[i],
486                                                   seg->reference_stream_specifier);
487             if (ret < 0)
488                 return ret;
489             if (ret > 0) {
490                 seg->reference_stream_index = i;
491                 break;
492             }
493         }
494     }
495
496     if (seg->reference_stream_index < 0) {
497         av_log(s, AV_LOG_ERROR, "Could not select stream matching identifier '%s'\n",
498                seg->reference_stream_specifier);
499         return AVERROR(EINVAL);
500     }
501
502     return 0;
503 }
504
505 static int seg_write_header(AVFormatContext *s)
506 {
507     SegmentContext *seg = s->priv_data;
508     AVFormatContext *oc = NULL;
509     int ret;
510
511     seg->segment_count = 0;
512     if (!seg->write_header_trailer)
513         seg->individual_header_trailer = 0;
514
515     if (!!seg->time_str + !!seg->times_str + !!seg->frames_str > 1) {
516         av_log(s, AV_LOG_ERROR,
517                "segment_time, segment_times, and segment_frames options "
518                "are mutually exclusive, select just one of them\n");
519         return AVERROR(EINVAL);
520     }
521
522     if (seg->times_str) {
523         if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
524             return ret;
525     } else if (seg->frames_str) {
526         if ((ret = parse_frames(s, &seg->frames, &seg->nb_frames, seg->frames_str)) < 0)
527             return ret;
528     } else {
529         /* set default value if not specified */
530         if (!seg->time_str)
531             seg->time_str = av_strdup("2");
532         if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
533             av_log(s, AV_LOG_ERROR,
534                    "Invalid time duration specification '%s' for segment_time option\n",
535                    seg->time_str);
536             return ret;
537         }
538     }
539
540     if (seg->time_delta_str) {
541         if ((ret = av_parse_time(&seg->time_delta, seg->time_delta_str, 1)) < 0) {
542             av_log(s, AV_LOG_ERROR,
543                    "Invalid time duration specification '%s' for delta option\n",
544                    seg->time_delta_str);
545             return ret;
546         }
547     }
548
549     if (seg->list) {
550         if (seg->list_type == LIST_TYPE_UNDEFINED) {
551             if      (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
552             else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
553             else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
554             else                                      seg->list_type = LIST_TYPE_FLAT;
555         }
556         if ((ret = segment_list_open(s)) < 0)
557             goto fail;
558     }
559     if (seg->list_type == LIST_TYPE_EXT)
560         av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
561
562     if ((ret = select_reference_stream(s)) < 0)
563         goto fail;
564     av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
565            seg->reference_stream_index,
566            av_get_media_type_string(s->streams[seg->reference_stream_index]->codec->codec_type));
567
568     seg->oformat = av_guess_format(seg->format, s->filename, NULL);
569
570     if (!seg->oformat) {
571         ret = AVERROR_MUXER_NOT_FOUND;
572         goto fail;
573     }
574     if (seg->oformat->flags & AVFMT_NOFILE) {
575         av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
576                seg->oformat->name);
577         ret = AVERROR(EINVAL);
578         goto fail;
579     }
580
581     if ((ret = segment_mux_init(s)) < 0)
582         goto fail;
583     oc = seg->avf;
584
585     if ((ret = set_segment_filename(s)) < 0)
586         goto fail;
587     seg->segment_count++;
588
589     if (seg->write_header_trailer) {
590         if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
591                               &s->interrupt_callback, NULL)) < 0)
592             goto fail;
593     } else {
594         if ((ret = open_null_ctx(&oc->pb)) < 0)
595             goto fail;
596     }
597
598     if ((ret = avformat_write_header(oc, NULL)) < 0) {
599         avio_close(oc->pb);
600         goto fail;
601     }
602     seg->is_first_pkt = 1;
603
604     if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
605         s->avoid_negative_ts = 1;
606
607     if (!seg->write_header_trailer) {
608         close_null_ctx(oc->pb);
609         if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
610                               &s->interrupt_callback, NULL)) < 0)
611             goto fail;
612     }
613
614 fail:
615     if (ret) {
616         if (seg->list)
617             avio_close(seg->list_pb);
618         if (seg->avf)
619             avformat_free_context(seg->avf);
620     }
621     return ret;
622 }
623
624 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
625 {
626     SegmentContext *seg = s->priv_data;
627     AVFormatContext *oc = seg->avf;
628     AVStream *st = s->streams[pkt->stream_index];
629     int64_t end_pts = INT64_MAX;
630     int start_frame = INT_MAX;
631     int ret;
632
633     if (seg->times) {
634         end_pts = seg->segment_count <= seg->nb_times ?
635             seg->times[seg->segment_count-1] : INT64_MAX;
636     } else if (seg->frames) {
637         start_frame = seg->segment_count <= seg->nb_frames ?
638             seg->frames[seg->segment_count-1] : INT_MAX;
639     } else {
640         end_pts = seg->time * seg->segment_count;
641     }
642
643     av_dlog(s, "packet stream:%d pts:%s pts_time:%s is_key:%d frame:%d\n",
644            pkt->stream_index, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
645            pkt->flags & AV_PKT_FLAG_KEY,
646            pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1);
647
648     if (pkt->stream_index == seg->reference_stream_index &&
649         pkt->flags & AV_PKT_FLAG_KEY &&
650         (seg->frame_count >= start_frame ||
651          (pkt->pts != AV_NOPTS_VALUE &&
652           av_compare_ts(pkt->pts, st->time_base,
653                         end_pts-seg->time_delta, AV_TIME_BASE_Q) >= 0))) {
654         ret = segment_end(s, seg->individual_header_trailer, 0);
655
656         if (!ret)
657             ret = segment_start(s, seg->individual_header_trailer);
658
659         if (ret)
660             goto fail;
661
662         oc = seg->avf;
663
664         seg->cur_entry.index = seg->segment_idx;
665         seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
666         seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
667     } else if (pkt->pts != AV_NOPTS_VALUE) {
668         seg->cur_entry.end_time =
669             FFMAX(seg->cur_entry.end_time, (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
670     }
671
672     if (seg->is_first_pkt) {
673         av_log(s, AV_LOG_DEBUG, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
674                seg->avf->filename, pkt->stream_index,
675                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
676         seg->is_first_pkt = 0;
677     }
678
679     if (seg->reset_timestamps) {
680         av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
681                pkt->stream_index,
682                av_ts2timestr(seg->cur_entry.start_pts, &AV_TIME_BASE_Q),
683                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
684                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
685
686         /* compute new timestamps */
687         if (pkt->pts != AV_NOPTS_VALUE)
688             pkt->pts -= av_rescale_q(seg->cur_entry.start_pts, AV_TIME_BASE_Q, st->time_base);
689         if (pkt->dts != AV_NOPTS_VALUE)
690             pkt->dts -= av_rescale_q(seg->cur_entry.start_pts, AV_TIME_BASE_Q, st->time_base);
691
692         av_log(s, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
693                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
694                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
695     }
696
697     ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
698
699 fail:
700     if (pkt->stream_index == seg->reference_stream_index)
701         seg->frame_count++;
702
703     if (ret < 0) {
704         if (seg->list)
705             avio_close(seg->list_pb);
706         avformat_free_context(oc);
707     }
708
709     return ret;
710 }
711
712 static int seg_write_trailer(struct AVFormatContext *s)
713 {
714     SegmentContext *seg = s->priv_data;
715     AVFormatContext *oc = seg->avf;
716     SegmentListEntry *cur, *next;
717
718     int ret;
719     if (!seg->write_header_trailer) {
720         if ((ret = segment_end(s, 0, 1)) < 0)
721             goto fail;
722         open_null_ctx(&oc->pb);
723         ret = av_write_trailer(oc);
724         close_null_ctx(oc->pb);
725     } else {
726         ret = segment_end(s, 1, 1);
727     }
728 fail:
729     if (seg->list)
730         avio_close(seg->list_pb);
731
732     av_opt_free(seg);
733     av_freep(&seg->times);
734     av_freep(&seg->frames);
735
736     cur = seg->segment_list_entries;
737     while (cur) {
738         next = cur->next;
739         av_free(cur);
740         cur = next;
741     }
742
743     avformat_free_context(oc);
744     return ret;
745 }
746
747 #define OFFSET(x) offsetof(SegmentContext, x)
748 #define E AV_OPT_FLAG_ENCODING_PARAM
749 static const AVOption options[] = {
750     { "reference_stream",  "set reference stream", OFFSET(reference_stream_specifier), AV_OPT_TYPE_STRING, {.str = "auto"}, CHAR_MIN, CHAR_MAX, E },
751     { "segment_format",    "set container format used for the segments", OFFSET(format),  AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
752     { "segment_list",      "set the segment list filename",              OFFSET(list),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
753
754     { "segment_list_flags","set flags affecting segment list generation", OFFSET(list_flags), AV_OPT_TYPE_FLAGS, {.i64 = SEGMENT_LIST_FLAG_CACHE }, 0, UINT_MAX, E, "list_flags"},
755     { "cache",             "allow list caching",                                    0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX,   E, "list_flags"},
756     { "live",              "enable live-friendly list generation (useful for HLS)", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_LIVE }, INT_MIN, INT_MAX,    E, "list_flags"},
757
758     { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT,  {.i64 = 0},     0, INT_MAX, E },
759
760     { "segment_list_type", "set the segment list type",                  OFFSET(list_type), AV_OPT_TYPE_INT,  {.i64 = LIST_TYPE_UNDEFINED}, -1, LIST_TYPE_NB-1, E, "list_type" },
761     { "flat", "flat format",     0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, E, "list_type" },
762     { "csv",  "csv format",      0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV  }, INT_MIN, INT_MAX, E, "list_type" },
763     { "ext",  "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT  }, INT_MIN, INT_MAX, E, "list_type" },
764     { "m3u8", "M3U8 format",     0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
765     { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
766
767     { "segment_time",      "set segment duration",                       OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
768     { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta_str), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, E },
769     { "segment_times",     "set segment split time points",              OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL},  0, 0,       E },
770     { "segment_frames",    "set segment split frame numbers",            OFFSET(frames_str),AV_OPT_TYPE_STRING,{.str = NULL},  0, 0,       E },
771     { "segment_wrap",      "set number after which the index wraps",     OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
772     { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
773
774     { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
775     { "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
776     { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E },
777     { NULL },
778 };
779
780 static const AVClass seg_class = {
781     .class_name = "segment muxer",
782     .item_name  = av_default_item_name,
783     .option     = options,
784     .version    = LIBAVUTIL_VERSION_INT,
785 };
786
787 AVOutputFormat ff_segment_muxer = {
788     .name           = "segment",
789     .long_name      = NULL_IF_CONFIG_SMALL("segment"),
790     .priv_data_size = sizeof(SegmentContext),
791     .flags          = AVFMT_NOFILE|AVFMT_GLOBALHEADER,
792     .write_header   = seg_write_header,
793     .write_packet   = seg_write_packet,
794     .write_trailer  = seg_write_trailer,
795     .priv_class     = &seg_class,
796 };
797
798 static const AVClass sseg_class = {
799     .class_name = "stream_segment muxer",
800     .item_name  = av_default_item_name,
801     .option     = options,
802     .version    = LIBAVUTIL_VERSION_INT,
803 };
804
805 AVOutputFormat ff_stream_segment_muxer = {
806     .name           = "stream_segment,ssegment",
807     .long_name      = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
808     .priv_data_size = sizeof(SegmentContext),
809     .flags          = AVFMT_NOFILE,
810     .write_header   = seg_write_header,
811     .write_packet   = seg_write_packet,
812     .write_trailer  = seg_write_trailer,
813     .priv_class     = &sseg_class,
814 };