Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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;
49  uint8_t iobuf[32768];
50  char temp_filename[1024];
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 
134 static void hds_free(AVFormatContext *s)
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  if (os->out)
143  avio_close(os->out);
144  os->out = NULL;
145  if (os->ctx && os->ctx_inited)
146  av_write_trailer(os->ctx);
147  if (os->ctx && os->ctx->pb)
148  av_free(os->ctx->pb);
149  if (os->ctx)
151  av_free(os->metadata);
152  for (j = 0; j < os->nb_extra_packets; j++)
153  av_free(os->extra_packets[j]);
154  for (j = 0; j < os->nb_fragments; j++)
155  av_free(os->fragments[j]);
156  av_free(os->fragments);
157  }
158  av_freep(&c->streams);
159 }
160 
161 static int write_manifest(AVFormatContext *s, int final)
162 {
163  HDSContext *c = s->priv_data;
164  AVIOContext *out;
165  char filename[1024], temp_filename[1024];
166  int ret, i;
167  float duration = 0;
168 
169  if (c->nb_streams > 0)
170  duration = c->streams[0].last_ts * av_q2d(s->streams[0]->time_base);
171 
172  snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
173  snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", s->filename);
174  ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
175  &s->interrupt_callback, NULL);
176  if (ret < 0) {
177  av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", filename);
178  return ret;
179  }
180  avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
181  avio_printf(out, "<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n");
182  avio_printf(out, "\t<id>%s</id>\n", av_basename(s->filename));
183  avio_printf(out, "\t<streamType>%s</streamType>\n",
184  final ? "recorded" : "live");
185  avio_printf(out, "\t<deliveryType>streaming</deliveryType>\n");
186  if (final)
187  avio_printf(out, "\t<duration>%f</duration>\n", duration);
188  for (i = 0; i < c->nb_streams; i++) {
189  OutputStream *os = &c->streams[i];
190  int b64_size = AV_BASE64_SIZE(os->metadata_size);
191  char *base64 = av_malloc(b64_size);
192  if (!base64) {
193  avio_close(out);
194  return AVERROR(ENOMEM);
195  }
196  av_base64_encode(base64, b64_size, os->metadata, os->metadata_size);
197 
198  avio_printf(out, "\t<bootstrapInfo profile=\"named\" url=\"stream%d.abst\" id=\"bootstrap%d\" />\n", i, i);
199  avio_printf(out, "\t<media bitrate=\"%d\" url=\"stream%d\" bootstrapInfoId=\"bootstrap%d\">\n", os->bitrate/1000, i, i);
200  avio_printf(out, "\t\t<metadata>%s</metadata>\n", base64);
201  avio_printf(out, "\t</media>\n");
202  av_free(base64);
203  }
204  avio_printf(out, "</manifest>\n");
205  avio_flush(out);
206  avio_close(out);
207  return ff_rename(temp_filename, filename);
208 }
209 
210 static void update_size(AVIOContext *out, int64_t pos)
211 {
212  int64_t end = avio_tell(out);
213  avio_seek(out, pos, SEEK_SET);
214  avio_wb32(out, end - pos);
215  avio_seek(out, end, SEEK_SET);
216 }
217 
218 /* Note, the .abst files need to be served with the "binary/octet"
219  * mime type, otherwise at least the OSMF player can easily fail
220  * with "stream not found" when polling for the next fragment. */
221 static int write_abst(AVFormatContext *s, OutputStream *os, int final)
222 {
223  HDSContext *c = s->priv_data;
224  AVIOContext *out;
225  char filename[1024], temp_filename[1024];
226  int i, ret;
227  int64_t asrt_pos, afrt_pos;
228  int start = 0, fragments;
229  int index = s->streams[os->first_stream]->id;
230  int64_t cur_media_time = 0;
231  if (c->window_size)
232  start = FFMAX(os->nb_fragments - c->window_size, 0);
233  fragments = os->nb_fragments - start;
234  if (final)
235  cur_media_time = os->last_ts;
236  else if (os->nb_fragments)
237  cur_media_time = os->fragments[os->nb_fragments - 1]->start_time;
238 
239  snprintf(filename, sizeof(filename),
240  "%s/stream%d.abst", s->filename, index);
241  snprintf(temp_filename, sizeof(temp_filename),
242  "%s/stream%d.abst.tmp", s->filename, index);
243  ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
244  &s->interrupt_callback, NULL);
245  if (ret < 0) {
246  av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
247  return ret;
248  }
249  avio_wb32(out, 0); // abst size
250  avio_wl32(out, MKTAG('a','b','s','t'));
251  avio_wb32(out, 0); // version + flags
252  avio_wb32(out, os->fragment_index - 1); // BootstrapinfoVersion
253  avio_w8(out, final ? 0 : 0x20); // profile, live, update
254  avio_wb32(out, 1000); // timescale
255  avio_wb64(out, cur_media_time);
256  avio_wb64(out, 0); // SmpteTimeCodeOffset
257  avio_w8(out, 0); // MovieIdentifer (null string)
258  avio_w8(out, 0); // ServerEntryCount
259  avio_w8(out, 0); // QualityEntryCount
260  avio_w8(out, 0); // DrmData (null string)
261  avio_w8(out, 0); // MetaData (null string)
262  avio_w8(out, 1); // SegmentRunTableCount
263  asrt_pos = avio_tell(out);
264  avio_wb32(out, 0); // asrt size
265  avio_wl32(out, MKTAG('a','s','r','t'));
266  avio_wb32(out, 0); // version + flags
267  avio_w8(out, 0); // QualityEntryCount
268  avio_wb32(out, 1); // SegmentRunEntryCount
269  avio_wb32(out, 1); // FirstSegment
270  avio_wb32(out, final ? (os->fragment_index - 1) : 0xffffffff); // FragmentsPerSegment
271  update_size(out, asrt_pos);
272  avio_w8(out, 1); // FragmentRunTableCount
273  afrt_pos = avio_tell(out);
274  avio_wb32(out, 0); // afrt size
275  avio_wl32(out, MKTAG('a','f','r','t'));
276  avio_wb32(out, 0); // version + flags
277  avio_wb32(out, 1000); // timescale
278  avio_w8(out, 0); // QualityEntryCount
279  avio_wb32(out, fragments); // FragmentRunEntryCount
280  for (i = start; i < os->nb_fragments; i++) {
281  avio_wb32(out, os->fragments[i]->n);
282  avio_wb64(out, os->fragments[i]->start_time);
283  avio_wb32(out, os->fragments[i]->duration);
284  }
285  update_size(out, afrt_pos);
286  update_size(out, 0);
287  avio_close(out);
288  return ff_rename(temp_filename, filename);
289 }
290 
291 static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
292 {
293  int ret, i;
294  ret = avio_open2(&os->out, os->temp_filename, AVIO_FLAG_WRITE,
295  &s->interrupt_callback, NULL);
296  if (ret < 0)
297  return ret;
298  avio_wb32(os->out, 0);
299  avio_wl32(os->out, MKTAG('m','d','a','t'));
300  for (i = 0; i < os->nb_extra_packets; i++) {
301  AV_WB24(os->extra_packets[i] + 4, start_ts);
302  os->extra_packets[i][7] = (start_ts >> 24) & 0x7f;
303  avio_write(os->out, os->extra_packets[i], os->extra_packet_sizes[i]);
304  }
305  return 0;
306 }
307 
308 static void close_file(OutputStream *os)
309 {
310  int64_t pos = avio_tell(os->out);
311  avio_seek(os->out, 0, SEEK_SET);
312  avio_wb32(os->out, pos);
313  avio_flush(os->out);
314  avio_close(os->out);
315  os->out = NULL;
316 }
317 
319 {
320  HDSContext *c = s->priv_data;
321  int ret = 0, i;
322  AVOutputFormat *oformat;
323 
324  mkdir(s->filename, 0777);
325 
326  oformat = av_guess_format("flv", NULL, NULL);
327  if (!oformat) {
329  goto fail;
330  }
331 
332  c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
333  if (!c->streams) {
334  ret = AVERROR(ENOMEM);
335  goto fail;
336  }
337 
338  for (i = 0; i < s->nb_streams; i++) {
339  OutputStream *os = &c->streams[c->nb_streams];
340  AVFormatContext *ctx;
341  AVStream *st = s->streams[i];
342 
343  if (!st->codec->bit_rate) {
344  av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);
345  ret = AVERROR(EINVAL);
346  goto fail;
347  }
348  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
349  if (os->has_video) {
350  c->nb_streams++;
351  os++;
352  }
353  os->has_video = 1;
354  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
355  if (os->has_audio) {
356  c->nb_streams++;
357  os++;
358  }
359  os->has_audio = 1;
360  } else {
361  av_log(s, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", i);
362  ret = AVERROR(EINVAL);
363  goto fail;
364  }
365  os->bitrate += s->streams[i]->codec->bit_rate;
366 
367  if (!os->ctx) {
368  os->first_stream = i;
369  ctx = avformat_alloc_context();
370  if (!ctx) {
371  ret = AVERROR(ENOMEM);
372  goto fail;
373  }
374  os->ctx = ctx;
375  ctx->oformat = oformat;
377 
378  ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf),
379  AVIO_FLAG_WRITE, os,
380  NULL, hds_write, NULL);
381  if (!ctx->pb) {
382  ret = AVERROR(ENOMEM);
383  goto fail;
384  }
385  } else {
386  ctx = os->ctx;
387  }
388  s->streams[i]->id = c->nb_streams;
389 
390  if (!(st = avformat_new_stream(ctx, NULL))) {
391  ret = AVERROR(ENOMEM);
392  goto fail;
393  }
396  }
397  if (c->streams[c->nb_streams].ctx)
398  c->nb_streams++;
399 
400  for (i = 0; i < c->nb_streams; i++) {
401  OutputStream *os = &c->streams[i];
402  int j;
403  if ((ret = avformat_write_header(os->ctx, NULL)) < 0) {
404  goto fail;
405  }
406  os->ctx_inited = 1;
407  avio_flush(os->ctx->pb);
408  for (j = 0; j < os->ctx->nb_streams; j++)
409  s->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base;
410 
411  snprintf(os->temp_filename, sizeof(os->temp_filename),
412  "%s/stream%d_temp", s->filename, i);
413  ret = init_file(s, os, 0);
414  if (ret < 0)
415  goto fail;
416 
417  if (!os->has_video && c->min_frag_duration <= 0) {
419  "No video stream in output stream %d and no min frag duration set\n", i);
420  ret = AVERROR(EINVAL);
421  }
422  os->fragment_index = 1;
423  write_abst(s, os, 0);
424  }
425  ret = write_manifest(s, 0);
426 
427 fail:
428  if (ret)
429  hds_free(s);
430  return ret;
431 }
432 
433 static int add_fragment(OutputStream *os, const char *file,
434  int64_t start_time, int64_t duration)
435 {
436  Fragment *frag;
437  if (duration == 0)
438  duration = 1;
439  if (os->nb_fragments >= os->fragments_size) {
440  int ret;
441  os->fragments_size = (os->fragments_size + 1) * 2;
442  if ((ret = av_reallocp_array(&os->fragments, os->fragments_size,
443  sizeof(*os->fragments))) < 0) {
444  os->fragments_size = 0;
445  os->nb_fragments = 0;
446  return ret;
447  }
448  }
449  frag = av_mallocz(sizeof(*frag));
450  if (!frag)
451  return AVERROR(ENOMEM);
452  av_strlcpy(frag->file, file, sizeof(frag->file));
453  frag->start_time = start_time;
454  frag->duration = duration;
455  frag->n = os->fragment_index;
456  os->fragments[os->nb_fragments++] = frag;
457  os->fragment_index++;
458  return 0;
459 }
460 
461 static int hds_flush(AVFormatContext *s, OutputStream *os, int final,
462  int64_t end_ts)
463 {
464  HDSContext *c = s->priv_data;
465  int i, ret = 0;
466  char target_filename[1024];
467  int index = s->streams[os->first_stream]->id;
468 
469  if (!os->packets_written)
470  return 0;
471 
472  avio_flush(os->ctx->pb);
473  os->packets_written = 0;
474  close_file(os);
475 
476  snprintf(target_filename, sizeof(target_filename),
477  "%s/stream%dSeg1-Frag%d", s->filename, index, os->fragment_index);
478  ret = ff_rename(os->temp_filename, target_filename);
479  if (ret < 0)
480  return ret;
481  add_fragment(os, target_filename, os->frag_start_ts, end_ts - os->frag_start_ts);
482 
483  if (!final) {
484  ret = init_file(s, os, end_ts);
485  if (ret < 0)
486  return ret;
487  }
488 
489  if (c->window_size || (final && c->remove_at_exit)) {
490  int remove = os->nb_fragments - c->window_size - c->extra_window_size;
491  if (final && c->remove_at_exit)
492  remove = os->nb_fragments;
493  if (remove > 0) {
494  for (i = 0; i < remove; i++) {
495  unlink(os->fragments[i]->file);
496  av_free(os->fragments[i]);
497  }
498  os->nb_fragments -= remove;
499  memmove(os->fragments, os->fragments + remove,
500  os->nb_fragments * sizeof(*os->fragments));
501  }
502  }
503 
504  if (ret >= 0)
505  ret = write_abst(s, os, final);
506  return ret;
507 }
508 
510 {
511  HDSContext *c = s->priv_data;
512  AVStream *st = s->streams[pkt->stream_index];
513  OutputStream *os = &c->streams[s->streams[pkt->stream_index]->id];
514  int64_t end_dts = os->fragment_index * (int64_t) c->min_frag_duration;
515  int ret;
516 
517  if (st->first_dts == AV_NOPTS_VALUE)
518  st->first_dts = pkt->dts;
519 
520  if ((!os->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
521  av_compare_ts(pkt->dts - st->first_dts, st->time_base,
522  end_dts, AV_TIME_BASE_Q) >= 0 &&
523  pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
524 
525  if ((ret = hds_flush(s, os, 0, pkt->dts)) < 0)
526  return ret;
527  }
528 
529  // Note, these fragment start timestamps, that represent a whole
530  // OutputStream, assume all streams in it have the same time base.
531  if (!os->packets_written)
532  os->frag_start_ts = pkt->dts;
533  os->last_ts = pkt->dts;
534 
535  os->packets_written++;
536  return ff_write_chained(os->ctx, pkt->stream_index - os->first_stream, pkt, s);
537 }
538 
540 {
541  HDSContext *c = s->priv_data;
542  int i;
543 
544  for (i = 0; i < c->nb_streams; i++)
545  hds_flush(s, &c->streams[i], 1, c->streams[i].last_ts);
546  write_manifest(s, 1);
547 
548  if (c->remove_at_exit) {
549  char filename[1024];
550  snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
551  unlink(filename);
552  for (i = 0; i < c->nb_streams; i++) {
553  snprintf(filename, sizeof(filename), "%s/stream%d.abst", s->filename, i);
554  unlink(filename);
555  }
556  rmdir(s->filename);
557  }
558 
559  hds_free(s);
560  return 0;
561 }
562 
563 #define OFFSET(x) offsetof(HDSContext, x)
564 #define E AV_OPT_FLAG_ENCODING_PARAM
565 static const AVOption options[] = {
566  { "window_size", "number of fragments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
567  { "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 },
568  { "min_frag_duration", "minimum fragment duration (in microseconds)", OFFSET(min_frag_duration), AV_OPT_TYPE_INT64, { .i64 = 10000000 }, 0, INT_MAX, E },
569  { "remove_at_exit", "remove all fragments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
570  { NULL },
571 };
572 
573 static const AVClass hds_class = {
574  .class_name = "HDS muxer",
575  .item_name = av_default_item_name,
576  .option = options,
577  .version = LIBAVUTIL_VERSION_INT,
578 };
579 
581  .name = "hds",
582  .long_name = NULL_IF_CONFIG_SMALL("HDS Muxer"),
583  .priv_data_size = sizeof(HDSContext),
584  .audio_codec = AV_CODEC_ID_AAC,
585  .video_codec = AV_CODEC_ID_H264,
590  .priv_class = &hds_class,
591 };
int nb_fragments
Definition: hdsenc.c:54
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:330
int64_t first_dts
Definition: avformat.h:850
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
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:1163
AVOption.
Definition: opt.h:234
static int hds_write_header(AVFormatContext *s)
Definition: hdsenc.c:318
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:238
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int n
Definition: hdsenc.c:41
int has_audio
Definition: hdsenc.c:57
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:769
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
#define AV_RB24
Definition: intreadwrite.h:64
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:293
int64_t last_ts
Definition: hdsenc.c:51
int first_stream
Definition: hdsenc.c:46
#define FF_ARRAY_ELEMS(a)
AVFormatContext * ctx
Definition: hdsenc.c:47
uint8_t * extra_packets[2]
Definition: hdsenc.c:62
AVOutputFormat ff_hds_muxer
Definition: hdsenc.c:580
int packets_written
Definition: hdsenc.c:53
static int write_manifest(AVFormatContext *s, int final)
Definition: hdsenc.c:161
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:154
static int64_t duration
Definition: avplay.c:246
int64_t frag_start_ts
Definition: hdsenc.c:51
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
int nb_streams
Definition: hdsenc.c:75
Format I/O context.
Definition: avformat.h:922
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:38
const char * av_basename(const char *path)
Thread safe basename.
Definition: avstring.c:177
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
uint8_t
int fragment_index
Definition: hdsenc.c:54
AVOptions.
miscellaneous OS support macros and functions.
int id
Format-specific stream ID.
Definition: avformat.h:706
static void close_file(OutputStream *os)
Definition: hdsenc.c:308
int ctx_inited
Definition: hdsenc.c:48
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:98
char temp_filename[1024]
Definition: hdsenc.c:50
uint8_t iobuf[32768]
Definition: hdsenc.c:49
int extra_packet_sizes[2]
Definition: hdsenc.c:63
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
static int flags
Definition: log.c:44
static const AVClass hds_class
Definition: hdsenc.c:573
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:941
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
static int ff_rename(const char *oldpath, const char *newpath)
Wrap errno on rename() error.
Definition: internal.h:363
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:107
static int64_t start_time
Definition: avplay.c:245
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:167
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#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:145
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:800
static int hds_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hdsenc.c:509
int metadata_size
Definition: hdsenc.c:60
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
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:72
#define FFMAX(a, b)
Definition: common.h:55
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:81
int extra_window_size
Definition: hdsenc.c:70
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
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:134
static int hds_write_trailer(AVFormatContext *s)
Definition: hdsenc.c:539
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
int bit_rate
the average bitrate
Definition: avcodec.h:1114
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
char filename[1024]
input or output filename
Definition: avformat.h:998
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes.
Definition: base64.h:59
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:415
#define E
Definition: hdsenc.c:564
const char * name
Definition: avformat.h:446
#define AV_WB24(p, d)
Definition: intreadwrite.h:412
static void update_size(AVIOContext *out, int64_t pos)
Definition: hdsenc.c:210
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:104
static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
Definition: hdsenc.c:291
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int hds_flush(AVFormatContext *s, OutputStream *os, int final, int64_t end_ts)
Definition: hdsenc.c:461
if(ac->has_optimized_func)
Stream structure.
Definition: avformat.h:699
int fragments_size
Definition: hdsenc.c:54
static int write_abst(AVFormatContext *s, OutputStream *os, int final)
Definition: hdsenc.c:221
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
Fragment ** fragments
Definition: hdsenc.c:55
int min_frag_duration
Definition: hdsenc.c:71
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:240
AVIOContext * pb
I/O context.
Definition: avformat.h:964
av_default_item_name
Definition: dnxhdenc.c:52
int window_size
Definition: hdsenc.c:69
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
int64_t duration
Definition: hdsenc.c:40
#define OFFSET(x)
Definition: hdsenc.c:563
int bitrate
Definition: hdsenc.c:45
Describe the class of an AVClass context structure.
Definition: log.h:33
AVIOContext * out
Definition: hdsenc.c:52
int index
Definition: gxfenc.c:72
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:783
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2445
static int add_fragment(OutputStream *os, const char *file, int64_t start_time, int64_t duration)
Definition: hdsenc.c:433
int remove_at_exit
Definition: hdsenc.c:72
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_dlog(ac->avr,"%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
Main libavformat public API header.
OutputStream * streams
Definition: hdsenc.c:74
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:628
static int parse_header(OutputStream *os, const uint8_t *buf, int buf_size)
Definition: hdsenc.c:78
void * priv_data
Format private data.
Definition: avformat.h:950
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:589
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:55
int stream_index
Definition: avcodec.h:975
static const AVOption options[]
Definition: hdsenc.c:565
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
#define MKTAG(a, b, c, d)
Definition: common.h:238
int nb_extra_packets
Definition: hdsenc.c:64
This structure stores compressed data.
Definition: avcodec.h:950
int has_video
Definition: hdsenc.c:57
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:205
char file[1024]
Definition: hdsenc.c:39
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2