FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hdsenc.c
Date: 2026-04-22 18:56:46
Exec Total Coverage
Lines: 0 327 0.0%
Functions: 0 13 0.0%
Branches: 0 170 0.0%

Line Branch Exec Source
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 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #include "avformat.h"
28 #include "internal.h"
29 #include "mux.h"
30 #include "os_support.h"
31
32 #include "libavutil/attributes_internal.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/base64.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/mem.h"
38 #include "libavutil/opt.h"
39
40 typedef struct Fragment {
41 char file[1024];
42 int64_t start_time, duration;
43 int n;
44 } Fragment;
45
46 typedef struct OutputStream {
47 int bitrate;
48 int first_stream;
49 AVFormatContext *ctx;
50 int ctx_inited;
51 uint8_t iobuf[32768];
52 char temp_filename[1024];
53 int64_t frag_start_ts, last_ts;
54 AVIOContext *out;
55 int packets_written;
56 int nb_fragments, fragments_size, fragment_index;
57 Fragment **fragments;
58
59 int has_audio, has_video;
60
61 uint8_t *metadata;
62 int metadata_size;
63
64 uint8_t *extra_packets[2];
65 int extra_packet_sizes[2];
66 int nb_extra_packets;
67 } OutputStream;
68
69 typedef struct HDSContext {
70 const AVClass *class; /* Class for private options. */
71 int window_size;
72 int extra_window_size;
73 int min_frag_duration;
74 int remove_at_exit;
75
76 OutputStream *streams;
77 int nb_streams;
78 } HDSContext;
79
80 static int parse_header(OutputStream *os, const uint8_t *buf, int buf_size)
81 {
82 if (buf_size < 13)
83 return AVERROR_INVALIDDATA;
84 if (memcmp(buf, "FLV", 3))
85 return AVERROR_INVALIDDATA;
86 buf += 13;
87 buf_size -= 13;
88 while (buf_size >= 11 + 4) {
89 int type = buf[0];
90 int size = AV_RB24(&buf[1]) + 11 + 4;
91 if (size > buf_size)
92 return AVERROR_INVALIDDATA;
93 if (type == 8 || type == 9) {
94 if (os->nb_extra_packets >= FF_ARRAY_ELEMS(os->extra_packets))
95 return AVERROR_INVALIDDATA;
96 os->extra_packet_sizes[os->nb_extra_packets] = size;
97 os->extra_packets[os->nb_extra_packets] = av_memdup(buf, size);
98 if (!os->extra_packets[os->nb_extra_packets])
99 return AVERROR(ENOMEM);
100 os->nb_extra_packets++;
101 } else if (type == 0x12) {
102 if (os->metadata)
103 return AVERROR_INVALIDDATA;
104 os->metadata_size = size - 11 - 4;
105 os->metadata = av_memdup(buf + 11, os->metadata_size);
106 if (!os->metadata)
107 return AVERROR(ENOMEM);
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, const 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 ff_format_io_close(s, &os->out);
144 if (os->ctx && os->ctx_inited)
145 av_write_trailer(os->ctx);
146 if (os->ctx)
147 avio_context_free(&os->ctx->pb);
148 avformat_free_context(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->url);
171 snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", s->url);
172 ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
173 if (ret < 0) {
174 av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
175 return ret;
176 }
177 avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
178 avio_printf(out, "<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n");
179 avio_printf(out, "\t<id>%s</id>\n", av_basename(s->url));
180 avio_printf(out, "\t<streamType>%s</streamType>\n",
181 final ? "recorded" : "live");
182 avio_printf(out, "\t<deliveryType>streaming</deliveryType>\n");
183 if (final)
184 avio_printf(out, "\t<duration>%f</duration>\n", duration);
185 for (i = 0; i < c->nb_streams; i++) {
186 OutputStream *os = &c->streams[i];
187 int b64_size = AV_BASE64_SIZE(os->metadata_size);
188 char *base64 = av_malloc(b64_size);
189 if (!base64) {
190 ff_format_io_close(s, &out);
191 return AVERROR(ENOMEM);
192 }
193 av_base64_encode(base64, b64_size, os->metadata, os->metadata_size);
194
195 avio_printf(out, "\t<bootstrapInfo profile=\"named\" url=\"stream%d.abst\" id=\"bootstrap%d\" />\n", i, i);
196 avio_printf(out, "\t<media bitrate=\"%d\" url=\"stream%d\" bootstrapInfoId=\"bootstrap%d\">\n", os->bitrate/1000, i, i);
197 avio_printf(out, "\t\t<metadata>%s</metadata>\n", base64);
198 avio_printf(out, "\t</media>\n");
199 av_free(base64);
200 }
201 avio_printf(out, "</manifest>\n");
202 avio_flush(out);
203 ff_format_io_close(s, &out);
204 return ff_rename(temp_filename, filename, s);
205 }
206
207 static void update_size(AVIOContext *out, int64_t pos)
208 {
209 int64_t end = avio_tell(out);
210 avio_seek(out, pos, SEEK_SET);
211 avio_wb32(out, end - pos);
212 avio_seek(out, end, SEEK_SET);
213 }
214
215 /* Note, the .abst files need to be served with the "binary/octet"
216 * mime type, otherwise at least the OSMF player can easily fail
217 * with "stream not found" when polling for the next fragment. */
218 static int write_abst(AVFormatContext *s, OutputStream *os, int final)
219 {
220 HDSContext *c = s->priv_data;
221 AVIOContext *out;
222 char filename[1024], temp_filename[1024];
223 int i, ret;
224 int64_t asrt_pos, afrt_pos;
225 int start = 0, fragments;
226 int index = s->streams[os->first_stream]->id;
227 int64_t cur_media_time = 0;
228 if (c->window_size)
229 start = FFMAX(os->nb_fragments - c->window_size, 0);
230 fragments = os->nb_fragments - start;
231 if (final)
232 cur_media_time = os->last_ts;
233 else if (os->nb_fragments)
234 cur_media_time = os->fragments[os->nb_fragments - 1]->start_time;
235
236 snprintf(filename, sizeof(filename),
237 "%s/stream%d.abst", s->url, index);
238 snprintf(temp_filename, sizeof(temp_filename),
239 "%s/stream%d.abst.tmp", s->url, index);
240 ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
241 if (ret < 0) {
242 av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
243 return ret;
244 }
245 avio_wb32(out, 0); // abst size
246 avio_wl32(out, MKTAG('a','b','s','t'));
247 avio_wb32(out, 0); // version + flags
248 avio_wb32(out, os->fragment_index - 1); // BootstrapinfoVersion
249 avio_w8(out, final ? 0 : 0x20); // profile, live, update
250 avio_wb32(out, 1000); // timescale
251 avio_wb64(out, cur_media_time);
252 avio_wb64(out, 0); // SmpteTimeCodeOffset
253 avio_w8(out, 0); // MovieIdentifer (null string)
254 avio_w8(out, 0); // ServerEntryCount
255 avio_w8(out, 0); // QualityEntryCount
256 avio_w8(out, 0); // DrmData (null string)
257 avio_w8(out, 0); // MetaData (null string)
258 avio_w8(out, 1); // SegmentRunTableCount
259 asrt_pos = avio_tell(out);
260 avio_wb32(out, 0); // asrt size
261 avio_wl32(out, MKTAG('a','s','r','t'));
262 avio_wb32(out, 0); // version + flags
263 avio_w8(out, 0); // QualityEntryCount
264 avio_wb32(out, 1); // SegmentRunEntryCount
265 avio_wb32(out, 1); // FirstSegment
266 avio_wb32(out, final ? (os->fragment_index - 1) : 0xffffffff); // FragmentsPerSegment
267 update_size(out, asrt_pos);
268 avio_w8(out, 1); // FragmentRunTableCount
269 afrt_pos = avio_tell(out);
270 avio_wb32(out, 0); // afrt size
271 avio_wl32(out, MKTAG('a','f','r','t'));
272 avio_wb32(out, 0); // version + flags
273 avio_wb32(out, 1000); // timescale
274 avio_w8(out, 0); // QualityEntryCount
275 avio_wb32(out, fragments); // FragmentRunEntryCount
276 for (i = start; i < os->nb_fragments; i++) {
277 avio_wb32(out, os->fragments[i]->n);
278 avio_wb64(out, os->fragments[i]->start_time);
279 avio_wb32(out, os->fragments[i]->duration);
280 }
281 update_size(out, afrt_pos);
282 update_size(out, 0);
283 ff_format_io_close(s, &out);
284 return ff_rename(temp_filename, filename, s);
285 }
286
287 static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
288 {
289 int ret, i;
290 ret = s->io_open(s, &os->out, os->temp_filename, AVIO_FLAG_WRITE, NULL);
291 if (ret < 0)
292 return ret;
293 avio_wb32(os->out, 0);
294 avio_wl32(os->out, MKTAG('m','d','a','t'));
295 for (i = 0; i < os->nb_extra_packets; i++) {
296 AV_WB24(os->extra_packets[i] + 4, start_ts);
297 os->extra_packets[i][7] = (start_ts >> 24) & 0x7f;
298 avio_write(os->out, os->extra_packets[i], os->extra_packet_sizes[i]);
299 }
300 return 0;
301 }
302
303 static void close_file(AVFormatContext *s, OutputStream *os)
304 {
305 int64_t pos = avio_tell(os->out);
306 avio_seek(os->out, 0, SEEK_SET);
307 avio_wb32(os->out, pos);
308 avio_flush(os->out);
309 ff_format_io_close(s, &os->out);
310 }
311
312 static int hds_write_header(AVFormatContext *s)
313 {
314 HDSContext *c = s->priv_data;
315 int ret = 0, i;
316
317 if (mkdir(s->url, 0777) == -1 && errno != EEXIST) {
318 av_log(s, AV_LOG_ERROR , "Failed to create directory %s\n", s->url);
319 return AVERROR(errno);
320 }
321
322
323 c->streams = av_calloc(s->nb_streams, sizeof(*c->streams));
324 if (!c->streams) {
325 return AVERROR(ENOMEM);
326 }
327
328 for (i = 0; i < s->nb_streams; i++) {
329 OutputStream *os = &c->streams[c->nb_streams];
330 AVFormatContext *ctx;
331 AVStream *st = s->streams[i];
332
333 if (!st->codecpar->bit_rate) {
334 av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);
335 return AVERROR(EINVAL);
336 }
337 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
338 if (os->has_video) {
339 c->nb_streams++;
340 os++;
341 }
342 os->has_video = 1;
343 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
344 if (os->has_audio) {
345 c->nb_streams++;
346 os++;
347 }
348 os->has_audio = 1;
349 } else {
350 av_log(s, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", i);
351 return AVERROR(EINVAL);
352 }
353 os->bitrate += s->streams[i]->codecpar->bit_rate;
354
355 if (!os->ctx) {
356 os->first_stream = i;
357 ctx = avformat_alloc_context();
358 if (!ctx) {
359 return AVERROR(ENOMEM);
360 }
361 os->ctx = ctx;
362 EXTERN const FFOutputFormat ff_flv_muxer;
363 ctx->oformat = &ff_flv_muxer.p;
364 ctx->interrupt_callback = s->interrupt_callback;
365 ctx->flags = s->flags;
366
367 ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf),
368 1, os,
369 NULL, hds_write, NULL);
370 if (!ctx->pb) {
371 return AVERROR(ENOMEM);
372 }
373 } else {
374 ctx = os->ctx;
375 }
376 s->streams[i]->id = c->nb_streams;
377
378 if (!(st = avformat_new_stream(ctx, NULL))) {
379 return AVERROR(ENOMEM);
380 }
381 avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
382 st->codecpar->codec_tag = 0;
383 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
384 st->time_base = s->streams[i]->time_base;
385 }
386 if (c->streams[c->nb_streams].ctx)
387 c->nb_streams++;
388
389 for (i = 0; i < c->nb_streams; i++) {
390 OutputStream *os = &c->streams[i];
391 int j;
392 if ((ret = avformat_write_header(os->ctx, NULL)) < 0) {
393 return ret;
394 }
395 os->ctx_inited = 1;
396 avio_flush(os->ctx->pb);
397 for (j = 0; j < os->ctx->nb_streams; j++)
398 s->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base;
399
400 snprintf(os->temp_filename, sizeof(os->temp_filename),
401 "%s/stream%d_temp", s->url, i);
402 ret = init_file(s, os, 0);
403 if (ret < 0)
404 return ret;
405
406 if (!os->has_video && c->min_frag_duration <= 0) {
407 av_log(s, AV_LOG_WARNING,
408 "No video stream in output stream %d and no min frag duration set\n", i);
409 }
410 os->fragment_index = 1;
411 write_abst(s, os, 0);
412 }
413 ret = write_manifest(s, 0);
414
415 return ret;
416 }
417
418 static int add_fragment(OutputStream *os, const char *file,
419 int64_t start_time, int64_t duration)
420 {
421 Fragment *frag;
422 if (duration == 0)
423 duration = 1;
424 if (os->nb_fragments >= os->fragments_size) {
425 int ret;
426 os->fragments_size = (os->fragments_size + 1) * 2;
427 if ((ret = av_reallocp_array(&os->fragments, os->fragments_size,
428 sizeof(*os->fragments))) < 0) {
429 os->fragments_size = 0;
430 os->nb_fragments = 0;
431 return ret;
432 }
433 }
434 frag = av_mallocz(sizeof(*frag));
435 if (!frag)
436 return AVERROR(ENOMEM);
437 av_strlcpy(frag->file, file, sizeof(frag->file));
438 frag->start_time = start_time;
439 frag->duration = duration;
440 frag->n = os->fragment_index;
441 os->fragments[os->nb_fragments++] = frag;
442 os->fragment_index++;
443 return 0;
444 }
445
446 static int hds_flush(AVFormatContext *s, OutputStream *os, int final,
447 int64_t end_ts)
448 {
449 HDSContext *c = s->priv_data;
450 int i, ret = 0;
451 char target_filename[1024];
452 int index = s->streams[os->first_stream]->id;
453
454 if (!os->packets_written)
455 return 0;
456
457 avio_flush(os->ctx->pb);
458 os->packets_written = 0;
459 close_file(s, os);
460
461 snprintf(target_filename, sizeof(target_filename),
462 "%s/stream%dSeg1-Frag%d", s->url, index, os->fragment_index);
463 ret = ff_rename(os->temp_filename, target_filename, s);
464 if (ret < 0)
465 return ret;
466 add_fragment(os, target_filename, os->frag_start_ts, end_ts - os->frag_start_ts);
467
468 if (!final) {
469 ret = init_file(s, os, end_ts);
470 if (ret < 0)
471 return ret;
472 }
473
474 if (c->window_size || (final && c->remove_at_exit)) {
475 int remove = os->nb_fragments - c->window_size - c->extra_window_size;
476 if (final && c->remove_at_exit)
477 remove = os->nb_fragments;
478 if (remove > 0) {
479 for (i = 0; i < remove; i++) {
480 unlink(os->fragments[i]->file);
481 av_freep(&os->fragments[i]);
482 }
483 os->nb_fragments -= remove;
484 memmove(os->fragments, os->fragments + remove,
485 os->nb_fragments * sizeof(*os->fragments));
486 }
487 }
488
489 if (ret >= 0)
490 ret = write_abst(s, os, final);
491 return ret;
492 }
493
494 static int hds_write_packet(AVFormatContext *s, AVPacket *pkt)
495 {
496 HDSContext *c = s->priv_data;
497 AVStream *st = s->streams[pkt->stream_index];
498 FFStream *const sti = ffstream(st);
499 OutputStream *os = &c->streams[s->streams[pkt->stream_index]->id];
500 int64_t end_dts = os->fragment_index * (int64_t)c->min_frag_duration;
501 int ret;
502
503 if (sti->first_dts == AV_NOPTS_VALUE)
504 sti->first_dts = pkt->dts;
505
506 if ((!os->has_video || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
507 av_compare_ts(pkt->dts - sti->first_dts, st->time_base,
508 end_dts, AV_TIME_BASE_Q) >= 0 &&
509 pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
510
511 if ((ret = hds_flush(s, os, 0, pkt->dts)) < 0)
512 return ret;
513 }
514
515 // Note, these fragment start timestamps, that represent a whole
516 // OutputStream, assume all streams in it have the same time base.
517 if (!os->packets_written)
518 os->frag_start_ts = pkt->dts;
519 os->last_ts = pkt->dts;
520
521 os->packets_written++;
522 return ff_write_chained(os->ctx, pkt->stream_index - os->first_stream, pkt, s, 0);
523 }
524
525 static int hds_write_trailer(AVFormatContext *s)
526 {
527 HDSContext *c = s->priv_data;
528 int i;
529
530 for (i = 0; i < c->nb_streams; i++)
531 hds_flush(s, &c->streams[i], 1, c->streams[i].last_ts);
532 write_manifest(s, 1);
533
534 if (c->remove_at_exit) {
535 char filename[1024];
536 snprintf(filename, sizeof(filename), "%s/index.f4m", s->url);
537 unlink(filename);
538 for (i = 0; i < c->nb_streams; i++) {
539 snprintf(filename, sizeof(filename), "%s/stream%d.abst", s->url, i);
540 unlink(filename);
541 }
542 rmdir(s->url);
543 }
544
545 return 0;
546 }
547
548 #define OFFSET(x) offsetof(HDSContext, x)
549 #define E AV_OPT_FLAG_ENCODING_PARAM
550 static const AVOption options[] = {
551 { "window_size", "number of fragments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
552 { "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 },
553 { "min_frag_duration", "minimum fragment duration (in microseconds)", OFFSET(min_frag_duration), AV_OPT_TYPE_INT64, { .i64 = 10000000 }, 0, INT_MAX, E },
554 { "remove_at_exit", "remove all fragments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
555 { NULL },
556 };
557
558 static const AVClass hds_class = {
559 .class_name = "HDS muxer",
560 .item_name = av_default_item_name,
561 .option = options,
562 .version = LIBAVUTIL_VERSION_INT,
563 };
564
565 const FFOutputFormat ff_hds_muxer = {
566 .p.name = "hds",
567 .p.long_name = NULL_IF_CONFIG_SMALL("HDS Muxer"),
568 .p.audio_codec = AV_CODEC_ID_AAC,
569 .p.video_codec = AV_CODEC_ID_H264,
570 .p.flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
571 .p.priv_class = &hds_class,
572 .priv_data_size = sizeof(HDSContext),
573 .write_header = hds_write_header,
574 .write_packet = hds_write_packet,
575 .write_trailer = hds_write_trailer,
576 .deinit = hds_free,
577 };
578