FFmpeg: libavformat/hdsenc.c Source File
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 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
31
37
43
56
58
61
66
68 const AVClass *
class;
/* Class for private options. */
73
77
79 {
80 if (buf_size < 13)
82 if (memcmp(buf, "FLV", 3))
84 buf += 13;
85 buf_size -= 13;
86 while (buf_size >= 11 + 4) {
99 }
else if (
type == 0x12) {
106 }
109 }
112 return 0;
113 }
114
115 static int hds_write(
void *opaque, uint8_t *buf,
int buf_size)
116 {
120 } else {
123 // Assuming the IO buffer is large enough to fit the
124 // FLV header and all metadata and extradata packets
127 }
128 }
129 return buf_size;
130 }
131
133 {
137 return;
138 for (
i = 0;
i <
s->nb_streams;
i++) {
153 }
155 }
156
158 {
161 char filename[1024], temp_filename[1024];
164
165 if (
c->nb_streams > 0)
167
168 snprintf(filename,
sizeof(filename),
"%s/index.f4m",
s->url);
169 snprintf(temp_filename,
sizeof(temp_filename),
"%s/index.f4m.tmp",
s->url);
174 }
176 avio_printf(
out,
"<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n");
179 final ? "recorded" : "live");
181 if (final)
183 for (
i = 0;
i <
c->nb_streams;
i++) {
187 if (!base64) {
190 }
192
193 avio_printf(
out,
"\t<bootstrapInfo profile=\"named\" url=\"stream%d.abst\" id=\"bootstrap%d\" />\n",
i,
i);
194 avio_printf(
out,
"\t<media bitrate=\"%d\" url=\"stream%d\" bootstrapInfoId=\"bootstrap%d\">\n", os->
bitrate/1000,
i,
i);
198 }
203 }
204
206 {
211 }
212
213 /* Note, the .abst files need to be served with the "binary/octet"
214 * mime type, otherwise at least the OSMF player can easily fail
215 * with "stream not found" when polling for the next fragment. */
217 {
220 char filename[1024], temp_filename[1024];
222 int64_t asrt_pos, afrt_pos;
223 int start = 0, fragments;
225 int64_t cur_media_time = 0;
229 if (final)
233
234 snprintf(filename,
sizeof(filename),
235 "%s/stream%d.abst",
s->url,
index);
236 snprintf(temp_filename,
sizeof(temp_filename),
237 "%s/stream%d.abst.tmp",
s->url,
index);
242 }
247 avio_w8(
out,
final ? 0 : 0x20);
// profile, live, update
251 avio_w8(
out, 0);
// MovieIdentifer (null string)
278 }
283 }
284
286 {
297 }
298 return 0;
299 }
300
302 {
308 }
309
311 {
315
316 if (mkdir(
s->url, 0777) == -1 && errno != EEXIST) {
319 }
320
322 if (!oformat) {
324 }
325
326 c->streams =
av_calloc(
s->nb_streams,
sizeof(*
c->streams));
329 }
330
331 for (
i = 0;
i <
s->nb_streams;
i++) {
335
339 }
343 os++;
344 }
349 os++;
350 }
352 } else {
355 }
356 os->
bitrate +=
s->streams[
i]->codecpar->bit_rate;
357
363 }
368
370 1, os,
374 }
375 } else {
377 }
378 s->streams[
i]->id =
c->nb_streams;
379
382 }
387 }
388 if (
c->streams[
c->nb_streams].ctx)
390
391 for (
i = 0;
i <
c->nb_streams;
i++) {
393 int j;
396 }
401
403 "%s/stream%d_temp",
s->url,
i);
407
408 if (!os->
has_video &&
c->min_frag_duration <= 0) {
410 "No video stream in output stream %d and no min frag duration set\n",
i);
411 }
414 }
416
418 }
419
422 {
434 }
435 }
437 if (!frag)
445 return 0;
446 }
447
449 int64_t end_ts)
450 {
453 char target_filename[1024];
455
457 return 0;
458
462
463 snprintf(target_filename,
sizeof(target_filename),
469
470 if (!final) {
474 }
475
476 if (
c->window_size || (
final &&
c->remove_at_exit)) {
477 int remove = os->
nb_fragments -
c->window_size -
c->extra_window_size;
478 if (
final &&
c->remove_at_exit)
480 if (remove > 0) {
481 for (
i = 0;
i < remove;
i++) {
484 }
488 }
489 }
490
494 }
495
497 {
504
507
512
515 }
516
517 // Note, these fragment start timestamps, that represent a whole
518 // OutputStream, assume all streams in it have the same time base.
522
525 }
526
528 {
531
532 for (
i = 0;
i <
c->nb_streams;
i++)
535
536 if (
c->remove_at_exit) {
537 char filename[1024];
538 snprintf(filename,
sizeof(filename),
"%s/index.f4m",
s->url);
539 unlink(filename);
540 for (
i = 0;
i <
c->nb_streams;
i++) {
541 snprintf(filename,
sizeof(filename),
"%s/stream%d.abst",
s->url,
i);
542 unlink(filename);
543 }
545 }
546
547 return 0;
548 }
549
550 #define OFFSET(x) offsetof(HDSContext, x)
551 #define E AV_OPT_FLAG_ENCODING_PARAM
553 {
"window_size",
"number of fragments kept in the manifest",
OFFSET(window_size),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
E },
554 {
"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 },
555 {
"min_frag_duration",
"minimum fragment duration (in microseconds)",
OFFSET(min_frag_duration),
AV_OPT_TYPE_INT64, { .i64 = 10000000 }, 0, INT_MAX,
E },
556 {
"remove_at_exit",
"remove all fragments when finished",
OFFSET(remove_at_exit),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
E },
558 };
559
565 };
566
579 };
#define AV_LOG_WARNING
Something somehow does not look correct.
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
enum AVMediaType codec_type
General type of the encoded data.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
int64_t first_dts
Timestamp corresponding to the last dts sync point.
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
atomic_uint_least64_t packets_written
static int hds_write_header(AVFormatContext *s)
AVStream ** streams
A list of all streams in the file.
static void deinit(AVFormatContext *s)
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
const char * av_basename(const char *path)
Thread safe basename.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
static void hds_free(AVFormatContext *s)
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
static int write_manifest(AVFormatContext *s, int final)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
int ff_rename(const char *url_src, const char *url_dst, void *logctx)
Wrap ffurl_move() and log if error happens.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
int flags
Flags modifying the (de)muxer behaviour.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
static int hds_write_packet(AVFormatContext *s, AVPacket *pkt)
#define AVIO_FLAG_WRITE
write-only
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
static int write_trailer(AVFormatContext *s1)
static const AVClass hds_class
const char * av_default_item_name(void *ptr)
Return the context name.
AVIOContext * pb
I/O context.
void avio_w8(AVIOContext *s, int b)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
static int write_abst(AVFormatContext *s, OutputStream *os, int final)
static int parse_header(OutputStream *os, const uint8_t *buf, int buf_size)
int extra_packet_sizes[2]
static int64_t start_time
static void update_size(AVIOContext *out, int64_t pos)
#define AV_NOPTS_VALUE
Undefined timestamp value.
uint8_t * extra_packets[2]
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
void avio_wb32(AVIOContext *s, unsigned int val)
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
void avio_wl32(AVIOContext *s, unsigned int val)
int flags
A combination of AV_PKT_FLAG values.
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
#define i(width, name, range_min, range_max)
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
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.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static int hds_write_trailer(AVFormatContext *s)
const FFOutputFormat ff_hds_muxer
void * av_calloc(size_t nmemb, size_t size)
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
static int add_fragment(OutputStream *os, const char *file, int64_t start_time, int64_t duration)
static void close_file(AVFormatContext *s, OutputStream *os)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
const struct AVOutputFormat * oformat
The output container format.
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
static int hds_flush(AVFormatContext *s, OutputStream *os, int final, int64_t end_ts)
void avio_wb64(AVIOContext *s, uint64_t val)
const 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...
This structure stores compressed data.
static const AVOption options[]
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.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define MKTAG(a, b, c, d)
static void write_header(FFV1Context *f)
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
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
static int hds_write(void *opaque, uint8_t *buf, int buf_size)
Generated on Tue Feb 28 2023 21:34:04 for FFmpeg by
doxygen
1.8.17