52 static int usage(
const char *argv0,
int ret)
54 fprintf(stderr,
"%s [-split] [-n basename] [-path-prefix prefix] " 55 "[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
132 char dirname[2048], filename[2048];
135 for (i = start_index; i < tracks->
nb_tracks; i++) {
137 const char *type = track->
is_video ?
"video" :
"audio";
138 snprintf(dirname,
sizeof(dirname),
"%sQualityLevels(%d)", output_prefix, track->
bitrate);
139 mkdir(dirname, 0777);
140 for (j = 0; j < track->
chunks; j++) {
141 snprintf(filename,
sizeof(filename),
"%s/Fragments(%s=%"PRId64
")",
153 int version, fieldlength, i, j;
163 for (i = start_index; i < tracks->
nb_tracks && !track; i++)
165 track = tracks->
tracks[i];
178 for (i = 0; i < track->
chunks; i++) {
186 for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
188 for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
190 for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
207 const char *file,
int split,
const char *output_prefix)
226 while (!
read_tfra(tracks, start_index, f)) {
237 fprintf(stderr,
"Unable to read the MFRA atom in %s\n", file);
254 uint16_t sps_size, pps_size;
281 const char *output_prefix)
284 int err = 0, i, orig_tracks = tracks->
nb_tracks;
285 char errbuf[50], *ptr;
291 fprintf(stderr,
"Unable to open %s: %s\n", file, errbuf);
298 fprintf(stderr,
"Unable to identify %s: %s\n", file, errbuf);
303 fprintf(stderr,
"No streams found in %s\n", file);
326 if ((ptr = strrchr(file,
'/')))
327 track->
name = ptr + 1;
338 "Track %d in %s is neither video nor audio, skipping\n",
383 err =
read_mfra(tracks, orig_tracks, file, split, output_prefix);
392 const char *output_prefix,
393 const char *path_prefix,
394 const char *ismc_prefix)
400 snprintf(filename,
sizeof(filename),
"%s%s.ism", output_prefix, basename);
401 out = fopen(filename,
"w");
406 fprintf(out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
407 fprintf(out,
"<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
408 fprintf(out,
"\t<head>\n");
409 fprintf(out,
"\t\t<meta name=\"clientManifestRelativePath\" " 410 "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
411 fprintf(out,
"\t</head>\n");
412 fprintf(out,
"\t<body>\n");
413 fprintf(out,
"\t\t<switch>\n");
414 for (i = 0; i < tracks->
nb_tracks; i++) {
416 const char *type = track->
is_video ?
"video" :
"audio";
417 fprintf(out,
"\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
419 fprintf(out,
"\t\t\t\t<param name=\"trackID\" value=\"%d\" " 420 "valueType=\"data\" />\n", track->
track_id);
421 fprintf(out,
"\t\t\t</%s>\n", type);
423 fprintf(out,
"\t\t</switch>\n");
424 fprintf(out,
"\t</body>\n");
425 fprintf(out,
"</smil>\n");
434 for (i = 0; i < track->
chunks; i++) {
435 for (j = main + 1; j < tracks->
nb_tracks; j++) {
438 fprintf(stderr,
"Mismatched duration of %s chunk %d in %s and %s\n",
441 fprintf(out,
"\t\t<c n=\"%d\" d=\"%"PRId64
"\" />\n",
447 const char *output_prefix,
int split)
454 snprintf(filename,
sizeof(filename),
"%sManifest", output_prefix);
456 snprintf(filename,
sizeof(filename),
"%s%s.ismc", output_prefix, basename);
457 out = fopen(filename,
"w");
462 fprintf(out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
463 fprintf(out,
"<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" " 464 "Duration=\"%"PRId64
"\">\n", tracks->
duration * 10);
467 struct Track *first_track = track;
470 "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" " 472 "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
474 for (i = 0; i < tracks->
nb_tracks; i++) {
475 track = tracks->
tracks[i];
479 "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" " 480 "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" " 481 "CodecPrivateData=\"",
485 fprintf(out,
"\" />\n");
488 fprintf(stderr,
"Mismatched number of video chunks in %s and %s\n",
492 fprintf(out,
"\t</StreamIndex>\n");
496 struct Track *first_track = track;
499 "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" " 501 "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
503 for (i = 0; i < tracks->
nb_tracks; i++) {
504 track = tracks->
tracks[i];
508 "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" " 509 "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" " 510 "BitsPerSample=\"16\" PacketSize=\"%d\" " 511 "AudioTag=\"%d\" CodecPrivateData=\"",
516 fprintf(out,
"\" />\n");
519 fprintf(stderr,
"Mismatched number of audio chunks in %s and %s\n",
523 fprintf(out,
"\t</StreamIndex>\n");
525 fprintf(out,
"</SmoothStreamingMedia>\n");
532 for (i = 0; i < tracks->
nb_tracks; i++) {
541 int main(
int argc,
char **argv)
543 const char *basename =
NULL;
544 const char *path_prefix =
"", *ismc_prefix =
"";
545 const char *output_prefix =
"";
546 char output_prefix_buf[2048];
552 for (i = 1; i < argc; i++) {
553 if (!strcmp(argv[i],
"-n")) {
554 basename = argv[i + 1];
556 }
else if (!strcmp(argv[i],
"-path-prefix")) {
557 path_prefix = argv[i + 1];
559 }
else if (!strcmp(argv[i],
"-ismc-prefix")) {
560 ismc_prefix = argv[i + 1];
562 }
else if (!strcmp(argv[i],
"-output")) {
563 output_prefix = argv[i + 1];
565 if (output_prefix[strlen(output_prefix) - 1] !=
'/') {
566 snprintf(output_prefix_buf,
sizeof(output_prefix_buf),
567 "%s/", output_prefix);
568 output_prefix = output_prefix_buf;
570 }
else if (!strcmp(argv[i],
"-split")) {
572 }
else if (argv[i][0] ==
'-') {
573 return usage(argv[0], 1);
575 if (
handle_file(&tracks, argv[i], split, output_prefix))
579 if (!tracks.
nb_tracks || (!basename && !split))
580 return usage(argv[0], 1);
584 path_prefix, ismc_prefix);
static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int64_t avio_size(AVIOContext *s)
Get the filesize.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
static int usage(const char *argv0, int ret)
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
#define AVIO_FLAG_READ
read-only
#define AVIO_FLAG_WRITE
write-only
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static void print_track_chunks(FILE *out, struct Tracks *tracks, int main, const char *type)
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
struct MoofOffset * offsets
miscellaneous OS support macros and functions.
unsigned int avio_rb32(AVIOContext *s)
int id
Format-specific stream ID.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream ** streams
A list of all streams in the file.
#define AVERROR_EOF
End of file.
uint64_t avio_rb64(AVIOContext *s)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
static int write_fragment(const char *filename, AVIOContext *in)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
static void output_client_manifest(struct Tracks *tracks, const char *basename, const char *output_prefix, int split)
static char * split(char *message, char delim)
int avio_r8(AVIOContext *s)
AVCodecContext * codec
Codec context associated with this stream.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
static int handle_file(struct Tracks *tracks, const char *file, int split, const char *output_prefix)
int bit_rate
the average bitrate
int void avio_flush(AVIOContext *s)
unsigned int avio_rb24(AVIOContext *s)
#define AV_TIME_BASE
Internal time base represented as integer.
int width
picture width / height.
enum AVMediaType codec_type
static int write_fragments(struct Tracks *tracks, int start_index, AVIOContext *in, const char *output_prefix)
int sample_rate
samples per second
static int get_private_data(struct Track *track, AVCodecContext *codec)
main external API structure.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
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.
static void clean_tracks(struct Tracks *tracks)
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
int64_t duration
Decoding: duration of the stream, in stream time base.
static int get_video_private_data(struct Track *track, AVCodecContext *codec)
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
#define MKBETAG(a, b, c, d)
int channels
number of audio channels
void avio_wb32(AVIOContext *s, unsigned int val)
static int read_mfra(struct Tracks *tracks, int start_index, const char *file, int split, const char *output_prefix)
static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
int main(int argc, char **argv)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
static void output_server_manifest(struct Tracks *tracks, const char *basename, const char *output_prefix, const char *path_prefix, const char *ismc_prefix)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...