annotate ffmpeg/libavformat/segment.c @ 13:844d341cf643 tip

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