FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hdsenc.c
Go to the documentation of this file.
1 /*
2  * Live HDS fragmenter
3  * Copyright (c) 2013 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 #include <float.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 
28 #include "avformat.h"
29 #include "internal.h"
30 #include "os_support.h"
31 
32 #include "libavutil/avstring.h"
33 #include "libavutil/base64.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/opt.h"
37 
38 typedef struct Fragment {
39  char file[1024];
40  int64_t start_time, duration;
41  int n;
42 } Fragment;
43 
44 typedef struct OutputStream {
45  int bitrate;
48  int ctx_inited;
49  uint8_t iobuf[32768];
50  char temp_filename[1024];
53  int packets_written;
56 
58 
61 
65 } OutputStream;
66 
67 typedef struct HDSContext {
68  const AVClass *class; /* Class for private options. */
73 
76 } HDSContext;
77 
78 static int parse_header(OutputStream *os, const uint8_t *buf, int buf_size)
79 {
80  if (buf_size < 13)
81  return AVERROR_INVALIDDATA;
82  if (memcmp(buf, "FLV", 3))
83  return AVERROR_INVALIDDATA;
84  buf += 13;
85  buf_size -= 13;
86  while (buf_size >= 11 + 4) {
87  int type = buf[0];
88  int size = AV_RB24(&buf[1]) + 11 + 4;
89  if (size > buf_size)
90  return AVERROR_INVALIDDATA;
91  if (type == 8 || type == 9) {
93  return AVERROR_INVALIDDATA;
95  os->extra_packets[os->nb_extra_packets] = av_malloc(size);
96  if (!os->extra_packets[os->nb_extra_packets])
97  return AVERROR(ENOMEM);
98  memcpy(os->extra_packets[os->nb_extra_packets], buf, size);
99  os->nb_extra_packets++;
100  } else if (type == 0x12) {
101  if (os->metadata)
102  return AVERROR_INVALIDDATA;
103  os->metadata_size = size - 11 - 4;
104  os->metadata = av_malloc(os->metadata_size);
105  if (!os->metadata)
106  return AVERROR(ENOMEM);
107  memcpy(os->metadata, buf + 11, os->metadata_size);
108  }
109  buf += size;
110  buf_size -= size;
111  }
112  if (!os->metadata)
113  return AVERROR_INVALIDDATA;
114  return 0;
115 }
116 
117 static int hds_write(void *opaque, uint8_t *buf, int buf_size)
118 {
119  OutputStream *os = opaque;
120  if (os->out) {
121  avio_write(os->out, buf, buf_size);
122  } else {
123  if (!os->metadata_size) {
124  int ret;
125  // Assuming the IO buffer is large enough to fit the
126  // FLV header and all metadata and extradata packets
127  if ((ret = parse_header(os, buf, buf_size)) < 0)
128  return ret;
129  }
130  }
131  return buf_size;
132 }
133 
135 {
136  HDSContext *c = s->priv_data;
137  int i, j;
138  if (!c->streams)
139  return;
140  for (i = 0; i < s->nb_streams; i++) {
141  OutputStream *os = &c->streams[i];
142  avio_closep(&os->out);
143  if (os->ctx && os->ctx_inited)
144  av_write_trailer(os->ctx);
145  if (os->ctx)
146  av_freep(&os->ctx->pb);
147  if (os->ctx)
149  av_freep(&os->metadata);
150  for (j = 0; j < os->nb_extra_packets; j++)
151  av_freep(&os->extra_packets[j]);
152  for (j = 0; j < os->nb_fragments; j++)
153  av_freep(&os->fragments[j]);
154  av_freep(&os->fragments);
155  }
156  av_freep(&c->streams);
157 }
158 
159 static int write_manifest(AVFormatContext *s, int final)
160 {
161  HDSContext *c = s->priv_data;
162  AVIOContext *out;
163  char filename[1024], temp_filename[1024];
164  int ret, i;
165  double duration = 0;
166 
167  if (c->nb_streams > 0)
168  duration = c->streams[0].last_ts * av_q2d(s->streams[0]->time_base);
169 
170  snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
171  snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", s->filename);
172  ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
173  &s->interrupt_callback, NULL);
174  if (ret < 0) {
175  av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
176  return ret;
177  }
178  avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
179  avio_printf(out, "<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n");
180  avio_printf(out, "\t<id>%s</id>\n", av_basename(s->filename));
181  avio_printf(out, "\t<streamType>%s</streamType>\n",
182  final ? "recorded" : "live");
183  avio_printf(out, "\t<deliveryType>streaming</deliveryType>\n");
184  if (final)
185  avio_printf(out, "\t<duration>%f</duration>\n", duration);
186  for (i = 0; i < c->nb_streams; i++) {
187  OutputStream *os = &c->streams[i];
188  int b64_size = AV_BASE64_SIZE(os->metadata_size);
189  char *base64 = av_malloc(b64_size);
190  if (!base64) {
191  avio_close(out);
192  return AVERROR(ENOMEM);
193  }
194  av_base64_encode(base64, b64_size, os->metadata, os->metadata_size);
195 
196  avio_printf(out, "\t<bootstrapInfo profile=\"named\" url=\"stream%d.abst\" id=\"bootstrap%d\" />\n", i, i);
197  avio_printf(out, "\t<media bitrate=\"%d\" url=\"stream%d\" bootstrapInfoId=\"bootstrap%d\">\n", os->bitrate/1000, i, i);
198  avio_printf(out, "\t\t<metadata>%s</metadata>\n", base64);
199  avio_printf(out, "\t</media>\n");
200  av_free(base64);
201  }
202  avio_printf(out, "</manifest>\n");
203  avio_flush(out);
204  avio_close(out);
205  return ff_rename(temp_filename, filename, s);
206 }
207 
208 static void update_size(AVIOContext *out, int64_t pos)
209 {
210  int64_t end = avio_tell(out);
211  avio_seek(out, pos, SEEK_SET);
212  avio_wb32(out, end - pos);
213  avio_seek(out, end, SEEK_SET);
214 }
215 
216 /* Note, the .abst files need to be served with the "binary/octet"
217  * mime type, otherwise at least the OSMF player can easily fail
218  * with "stream not found" when polling for the next fragment. */
219 static int write_abst(AVFormatContext *s, OutputStream *os, int final)
220 {
221  HDSContext *c = s->priv_data;
222  AVIOContext *out;
223  char filename[1024], temp_filename[1024];
224  int i, ret;
225  int64_t asrt_pos, afrt_pos;
226  int start = 0, fragments;
227  int index = s->streams[os->first_stream]->id;
228  int64_t cur_media_time = 0;
229  if (c->window_size)
230  start = FFMAX(os->nb_fragments - c->window_size, 0);
231  fragments = os->nb_fragments - start;
232  if (final)
233  cur_media_time = os->last_ts;
234  else if (os->nb_fragments)
235  cur_media_time = os->fragments[os->nb_fragments - 1]->start_time;
236 
237  snprintf(filename, sizeof(filename),
238  "%s/stream%d.abst", s->filename, index);
239  snprintf(temp_filename, sizeof(temp_filename),
240  "%s/stream%d.abst.tmp", s->filename, index);
241  ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
242  &s->interrupt_callback, NULL);
243  if (ret < 0) {
244  av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
245  return ret;
246  }
247  avio_wb32(out, 0); // abst size
248  avio_wl32(out, MKTAG('a','b','s','t'));
249  avio_wb32(out, 0); // version + flags
250  avio_wb32(out, os->fragment_index - 1); // BootstrapinfoVersion
251  avio_w8(out, final ? 0 : 0x20); // profile, live, update
252  avio_wb32(out, 1000); // timescale
253  avio_wb64(out, cur_media_time);
254  avio_wb64(out, 0); // SmpteTimeCodeOffset
255  avio_w8(out, 0); // MovieIdentifer (null string)
256  avio_w8(out, 0); // ServerEntryCount
257  avio_w8(out, 0); // QualityEntryCount
258  avio_w8(out, 0); // DrmData (null string)
259  avio_w8(out, 0); // MetaData (null string)
260  avio_w8(out, 1); // SegmentRunTableCount
261  asrt_pos = avio_tell(out);
262  avio_wb32(out, 0); // asrt size
263  avio_wl32(out, MKTAG('a','s','r','t'));
264  avio_wb32(out, 0); // version + flags
265  avio_w8(out, 0); // QualityEntryCount
266  avio_wb32(out, 1); // SegmentRunEntryCount
267  avio_wb32(out, 1); // FirstSegment
268  avio_wb32(out, final ? (os->fragment_index - 1) : 0xffffffff); // FragmentsPerSegment
269  update_size(out, asrt_pos);
270  avio_w8(out, 1); // FragmentRunTableCount
271  afrt_pos = avio_tell(out);
272  avio_wb32(out, 0); // afrt size
273  avio_wl32(out, MKTAG('a','f','r','t'));
274  avio_wb32(out, 0); // version + flags
275  avio_wb32(out, 1000); // timescale
276  avio_w8(out, 0); // QualityEntryCount
277  avio_wb32(out, fragments); // FragmentRunEntryCount
278  for (i = start; i < os->nb_fragments; i++) {
279  avio_wb32(out, os->fragments[i]->n);
280  avio_wb64(out, os->fragments[i]->start_time);
281  avio_wb32(out, os->fragments[i]->duration);
282  }
283  update_size(out, afrt_pos);
284  update_size(out, 0);
285  avio_close(out);
286  return ff_rename(temp_filename, filename, s);
287 }
288 
289 static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
290 {
291  int ret, i;
292  ret = avio_open2(&os->out, os->temp_filename, AVIO_FLAG_WRITE,
293  &s->interrupt_callback, NULL);
294  if (ret < 0)
295  return ret;
296  avio_wb32(os->out, 0);
297  avio_wl32(os->out, MKTAG('m','d','a','t'));
298  for (i = 0; i < os->nb_extra_packets; i++) {
299  AV_WB24(os->extra_packets[i] + 4, start_ts);
300  os->extra_packets[i][7] = (start_ts >> 24) & 0x7f;
301  avio_write(os->out, os->extra_packets[i], os->extra_packet_sizes[i]);
302  }
303  return 0;
304 }
305 
306 static void close_file(OutputStream *os)
307 {
308  int64_t pos = avio_tell(os->out);
309  avio_seek(os->out, 0, SEEK_SET);
310  avio_wb32(os->out, pos);
311  avio_flush(os->out);
312  avio_closep(&os->out);
313 }
314 
316 {
317  HDSContext *c = s->priv_data;
318  int ret = 0, i;
319  AVOutputFormat *oformat;
320 
321  if (mkdir(s->filename, 0777) == -1 && errno != EEXIST) {
322  ret = AVERROR(errno);
323  av_log(s, AV_LOG_ERROR , "Failed to create directory %s\n", s->filename);
324  goto fail;
325  }
326 
327  oformat = av_guess_format("flv", NULL, NULL);
328  if (!oformat) {
330  goto fail;
331  }
332 
333  c->streams = av_mallocz_array(s->nb_streams, sizeof(*c->streams));
334  if (!c->streams) {
335  ret = AVERROR(ENOMEM);
336  goto fail;
337  }
338 
339  for (i = 0; i < s->nb_streams; i++) {
340  OutputStream *os = &c->streams[c->nb_streams];
341  AVFormatContext *ctx;
342  AVStream *st = s->streams[i];
343 
344  if (!st->codec->bit_rate) {
345  av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);
346  ret = AVERROR(EINVAL);
347  goto fail;
348  }
349  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
350  if (os->has_video) {
351  c->nb_streams++;
352  os++;
353  }
354  os->has_video = 1;
355  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
356  if (os->has_audio) {
357  c->nb_streams++;
358  os++;
359  }
360  os->has_audio = 1;
361  } else {
362  av_log(s, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", i);
363  ret = AVERROR(EINVAL);
364  goto fail;
365  }
366  os->bitrate += s->streams[i]->codec->bit_rate;
367 
368  if (!os->ctx) {
369  os->first_stream = i;
370  ctx = avformat_alloc_context();
371  if (!ctx) {
372  ret = AVERROR(ENOMEM);
373  goto fail;
374  }
375  os->ctx = ctx;
376  ctx->oformat = oformat;
378 
379  ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf),
380  AVIO_FLAG_WRITE, os,
381  NULL, hds_write, NULL);
382  if (!ctx->pb) {
383  ret = AVERROR(ENOMEM);
384  goto fail;
385  }
386  } else {
387  ctx = os->ctx;
388  }
389  s->streams[i]->id = c->nb_streams;
390 
391  if (!(st = avformat_new_stream(ctx, NULL))) {
392  ret = AVERROR(ENOMEM);
393  goto fail;
394  }
396  st->codec->codec_tag = 0;
398  st->time_base = s->streams[i]->time_base;
399  }
400  if (c->streams[c->nb_streams].ctx)
401  c->nb_streams++;
402 
403  for (i = 0; i < c->nb_streams; i++) {
404  OutputStream *os = &c->streams[i];
405  int j;
406  if ((ret = avformat_write_header(os->ctx, NULL)) < 0) {
407  goto fail;
408  }
409  os->ctx_inited = 1;
410  avio_flush(os->ctx->pb);
411  for (j = 0; j < os->ctx->nb_streams; j++)
412  s->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base;
413 
414  snprintf(os->temp_filename, sizeof(os->temp_filename),
415  "%s/stream%d_temp", s->filename, i);
416  ret = init_file(s, os, 0);
417  if (ret < 0)
418  goto fail;
419 
420  if (!os->has_video && c->min_frag_duration <= 0) {
422  "No video stream in output stream %d and no min frag duration set\n", i);
423  ret = AVERROR(EINVAL);
424  }
425  os->fragment_index = 1;
426  write_abst(s, os, 0);
427  }
428  ret = write_manifest(s, 0);
429 
430 fail:
431  if (ret)
432  hds_free(s);
433  return ret;
434 }
435 
436 static int add_fragment(OutputStream *os, const char *file,
437  int64_t start_time, int64_t duration)
438 {
439  Fragment *frag;
440  if (duration == 0)
441  duration = 1;
442  if (os->nb_fragments >= os->fragments_size) {
443  int ret;
444  os->fragments_size = (os->fragments_size + 1) * 2;
445  if ((ret = av_reallocp_array(&os->fragments, os->fragments_size,
446  sizeof(*os->fragments))) < 0) {
447  os->fragments_size = 0;
448  os->nb_fragments = 0;
449  return ret;
450  }
451  }
452  frag = av_mallocz(sizeof(*frag));
453  if (!frag)
454  return AVERROR(ENOMEM);
455  av_strlcpy(frag->file, file, sizeof(frag->file));
456  frag->start_time = start_time;
457  frag->duration = duration;
458  frag->n = os->fragment_index;
459  os->fragments[os->nb_fragments++] = frag;
460  os->fragment_index++;
461  return 0;
462 }
463 
464 static int hds_flush(AVFormatContext *s, OutputStream *os, int final,
465  int64_t end_ts)
466 {
467  HDSContext *c = s->priv_data;
468  int i, ret = 0;
469  char target_filename[1024];
470  int index = s->streams[os->first_stream]->id;
471 
472  if (!os->packets_written)
473  return 0;
474 
475  avio_flush(os->ctx->pb);
476  os->packets_written = 0;
477  close_file(os);
478 
479  snprintf(target_filename, sizeof(target_filename),
480  "%s/stream%dSeg1-Frag%d", s->filename, index, os->fragment_index);
481  ret = ff_rename(os->temp_filename, target_filename, s);
482  if (ret < 0)
483  return ret;
484  add_fragment(os, target_filename, os->frag_start_ts, end_ts - os->frag_start_ts);
485 
486  if (!final) {
487  ret = init_file(s, os, end_ts);
488  if (ret < 0)
489  return ret;
490  }
491 
492  if (c->window_size || (final && c->remove_at_exit)) {
493  int remove = os->nb_fragments - c->window_size - c->extra_window_size;
494  if (final && c->remove_at_exit)
495  remove = os->nb_fragments;
496  if (remove > 0) {
497  for (i = 0; i < remove; i++) {
498  unlink(os->fragments[i]->file);
499  av_freep(&os->fragments[i]);
500  }
501  os->nb_fragments -= remove;
502  memmove(os->fragments, os->fragments + remove,
503  os->nb_fragments * sizeof(*os->fragments));
504  }
505  }
506 
507  if (ret >= 0)
508  ret = write_abst(s, os, final);
509  return ret;
510 }
511 
513 {
514  HDSContext *c = s->priv_data;
515  AVStream *st = s->streams[pkt->stream_index];
516  OutputStream *os = &c->streams[s->streams[pkt->stream_index]->id];
517  int64_t end_dts = os->fragment_index * (int64_t)c->min_frag_duration;
518  int ret;
519 
520  if (st->first_dts == AV_NOPTS_VALUE)
521  st->first_dts = pkt->dts;
522 
523  if ((!os->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
524  av_compare_ts(pkt->dts - st->first_dts, st->time_base,
525  end_dts, AV_TIME_BASE_Q) >= 0 &&
526  pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
527 
528  if ((ret = hds_flush(s, os, 0, pkt->dts)) < 0)
529  return ret;
530  }
531 
532  // Note, these fragment start timestamps, that represent a whole
533  // OutputStream, assume all streams in it have the same time base.
534  if (!os->packets_written)
535  os->frag_start_ts = pkt->dts;
536  os->last_ts = pkt->dts;
537 
538  os->packets_written++;
539  return ff_write_chained(os->ctx, pkt->stream_index - os->first_stream, pkt, s, 0);
540 }
541 
543 {
544  HDSContext *c = s->priv_data;
545  int i;
546 
547  for (i = 0; i < c->nb_streams; i++)
548  hds_flush(s, &c->streams[i], 1, c->streams[i].last_ts);
549  write_manifest(s, 1);
550 
551  if (c->remove_at_exit) {
552  char filename[1024];
553  snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
554  unlink(filename);
555  for (i = 0; i < c->nb_streams; i++) {
556  snprintf(filename, sizeof(filename), "%s/stream%d.abst", s->filename, i);
557  unlink(filename);
558  }
559  rmdir(s->filename);
560  }
561 
562  hds_free(s);
563  return 0;
564 }
565 
566 #define OFFSET(x) offsetof(HDSContext, x)
567 #define E AV_OPT_FLAG_ENCODING_PARAM
568 static const AVOption options[] = {
569  { "window_size", "number of fragments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
570  { "extra_window_size", "number of fragments kept outside of the manifest before removing from disk", OFFSET(extra_window_size), AV_OPT_TYPE_INT, { .i64 = 5 }, 0, INT_MAX, E },
571  { "min_frag_duration", "minimum fragment duration (in microseconds)", OFFSET(min_frag_duration), AV_OPT_TYPE_INT64, { .i64 = 10000000 }, 0, INT_MAX, E },
572  { "remove_at_exit", "remove all fragments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
573  { NULL },
574 };
575 
576 static const AVClass hds_class = {
577  .class_name = "HDS muxer",
578  .item_name = av_default_item_name,
579  .option = options,
580  .version = LIBAVUTIL_VERSION_INT,
581 };
582 
584  .name = "hds",
585  .long_name = NULL_IF_CONFIG_SMALL("HDS Muxer"),
586  .priv_data_size = sizeof(HDSContext),
587  .audio_codec = AV_CODEC_ID_AAC,
588  .video_codec = AV_CODEC_ID_H264,
593  .priv_class = &hds_class,
594 };
int nb_fragments
Definition: hdsenc.c:54
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:411
int64_t first_dts
Timestamp corresponding to the last dts sync point.
Definition: avformat.h:1018
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
uint8_t * metadata
Definition: hdsenc.c:59
static void hds_free(AVFormatContext *s)
Definition: hdsenc.c:134
int64_t start_time
Definition: hdsenc.c:40
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1523
AVOption.
Definition: opt.h:255
static int hds_write_header(AVFormatContext *s)
Definition: hdsenc.c:315
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:447
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
int n
Definition: hdsenc.c:41
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src, int interleave)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:1041
int has_audio
Definition: hdsenc.c:57
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:914
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:486
int64_t last_ts
Definition: hdsenc.c:51
int first_stream
Definition: hdsenc.c:46
AVFormatContext * ctx
Definition: dashenc.c:61
uint8_t * extra_packets[2]
Definition: hdsenc.c:62
static AVPacket pkt
AVOutputFormat ff_hds_muxer
Definition: hdsenc.c:583
uint64_t packets_written
Definition: ffmpeg.h:457
static int write_manifest(AVFormatContext *s, int final)
Definition: hdsenc.c:159
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:182
int64_t frag_start_ts
Definition: hdsenc.c:51
int nb_streams
Definition: hdsenc.c:75
Format I/O context.
Definition: avformat.h:1273
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
static int64_t start_time
Definition: ffplay.c:325
const char * av_basename(const char *path)
Thread safe basename.
Definition: avstring.c:234
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:319
uint8_t
#define av_malloc(s)
int fragment_index
Definition: hdsenc.c:54
AVOptions.
miscellaneous OS support macros and functions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int id
Format-specific stream ID.
Definition: avformat.h:849
static void close_file(OutputStream *os)
Definition: hdsenc.c:306
int ctx_inited
Definition: dashenc.c:62
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:111
char temp_filename[1024]
Definition: hdsenc.c:50
uint8_t iobuf[32768]
Definition: dashenc.c:63
int extra_packet_sizes[2]
Definition: hdsenc.c:63
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
static const AVClass hds_class
Definition: hdsenc.c:576
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
static int64_t duration
Definition: ffplay.c:326
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:178
#define av_log(a,...)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1292
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:112
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:941
static int hds_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hdsenc.c:512
int metadata_size
Definition: hdsenc.c:60
URLContext * out
Definition: dashenc.c:64
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition: base64.c:138
#define FFMAX(a, b)
Definition: common.h:79
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:57
int extra_window_size
Definition: hdsenc.c:70
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1429
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:145
static int hds_write_trailer(AVFormatContext *s)
Definition: hdsenc.c:542
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1329
int bit_rate
the average bitrate
Definition: avcodec.h:1567
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:198
char filename[1024]
input or output filename
Definition: avformat.h:1349
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
Definition: base64.h:61
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:471
#define E
Definition: hdsenc.c:567
const char * name
Definition: avformat.h:513
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
static void update_size(AVIOContext *out, int64_t pos)
Definition: hdsenc.c:208
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:94
static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
Definition: hdsenc.c:289
static int hds_flush(AVFormatContext *s, OutputStream *os, int final, int64_t end_ts)
Definition: hdsenc.c:464
#define FF_ARRAY_ELEMS(a)
Stream structure.
Definition: avformat.h:842
int fragments_size
Definition: hdsenc.c:54
static int write_abst(AVFormatContext *s, OutputStream *os, int final)
Definition: hdsenc.c:219
enum AVMediaType codec_type
Definition: avcodec.h:1510
Fragment ** fragments
Definition: hdsenc.c:55
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
int min_frag_duration
Definition: hdsenc.c:71
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
int window_size
Definition: hdsenc.c:69
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:156
int64_t duration
Definition: hdsenc.c:40
#define OFFSET(x)
Definition: hdsenc.c:566
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1534
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
int bitrate
Definition: hdsenc.c:45
static int ff_rename(const char *oldpath, const char *newpath, void *logctx)
Wrap errno on rename() error.
Definition: internal.h:447
Describe the class of an AVClass context structure.
Definition: log.h:67
AVIOContext * out
Definition: hdsenc.c:52
int index
Definition: gxfenc.c:89
static int hds_write(void *opaque, uint8_t *buf, int buf_size)
Definition: hdsenc.c:117
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:918
#define snprintf
Definition: snprintf.h:34
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3686
static int flags
Definition: cpu.c:47
static int add_fragment(OutputStream *os, const char *file, int64_t start_time, int64_t duration)
Definition: hdsenc.c:436
int remove_at_exit
Definition: hdsenc.c:72
Main libavformat public API header.
OutputStream * streams
Definition: hdsenc.c:74
if(ret< 0)
Definition: vf_mcdeint.c:280
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
static double c[64]
static int parse_header(OutputStream *os, const uint8_t *buf, int buf_size)
Definition: hdsenc.c:78
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1301
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1422
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:986
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:327
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:60
int stream_index
Definition: avcodec.h:1425
static const AVOption options[]
Definition: hdsenc.c:568
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:884
#define MKTAG(a, b, c, d)
Definition: common.h:330
int nb_extra_packets
Definition: hdsenc.c:64
This structure stores compressed data.
Definition: avcodec.h:1400
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:959
int has_video
Definition: hdsenc.c:57
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
char file[1024]
Definition: hdsenc.c:39
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2