40 #include "libavformat/avformat.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/mathematics.h"
47 fprintf(stderr,
"%s [-split] [-n basename] file1 [file2] ...\n", argv0);
124 char dirname[100], filename[500];
127 for (i = start_index; i < tracks->
nb_tracks; i++) {
130 snprintf(dirname,
sizeof(dirname),
"QualityLevels(%d)", track->
bitrate);
131 if (mkdir(dirname, 0777) == -1)
133 for (j = 0; j < track->
chunks; j++) {
134 snprintf(filename,
sizeof(filename),
"%s/Fragments(%s=%"PRId64
")",
146 int version, fieldlength, i, j;
149 struct Track *track = NULL;
156 for (i = start_index; i < tracks->
nb_tracks && !track; i++)
158 track = tracks->
tracks[i];
171 for (i = 0; i < track->
chunks; i++) {
179 for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
181 for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
183 for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
200 const char *file,
int split)
219 while (!
read_tfra(tracks, start_index, f)) {
230 fprintf(stderr,
"Unable to read the MFRA atom in %s\n", file);
247 uint16_t sps_size, pps_size;
279 int err = 0, i, orig_tracks = tracks->
nb_tracks;
280 char errbuf[50], *ptr;
286 fprintf(stderr,
"Unable to open %s: %s\n", file, errbuf);
293 fprintf(stderr,
"Unable to identify %s: %s\n", file, errbuf);
298 fprintf(stderr,
"No streams found in %s\n", file);
321 if ((ptr = strrchr(file,
'/')) != NULL)
322 track->
name = ptr + 1;
333 "Track %d in %s is neither video nor audio, skipping\n",
378 err =
read_mfra(tracks, orig_tracks, file, split);
387 const char *basename)
393 snprintf(filename,
sizeof(filename),
"%s.ism", basename);
394 out = fopen(filename,
"w");
399 fprintf(out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
400 fprintf(out,
"<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
401 fprintf(out,
"\t<head>\n");
402 fprintf(out,
"\t\t<meta name=\"clientManifestRelativePath\" "
403 "content=\"%s.ismc\" />\n", basename);
404 fprintf(out,
"\t</head>\n");
405 fprintf(out,
"\t<body>\n");
406 fprintf(out,
"\t\t<switch>\n");
407 for (i = 0; i < tracks->
nb_tracks; i++) {
410 fprintf(out,
"\t\t\t<%s src=\"%s\" systemBitrate=\"%d\">\n",
412 fprintf(out,
"\t\t\t\t<param name=\"trackID\" value=\"%d\" "
413 "valueType=\"data\" />\n", track->
track_id);
414 fprintf(out,
"\t\t\t</%s>\n", type);
416 fprintf(out,
"\t\t</switch>\n");
417 fprintf(out,
"\t</body>\n");
418 fprintf(out,
"</smil>\n");
427 for (i = 0; i < track->
chunks; i++) {
428 for (j = main + 1; j < tracks->
nb_tracks; j++) {
431 fprintf(stderr,
"Mismatched duration of %s chunk %d in %s and %s\n",
434 fprintf(out,
"\t\t<c n=\"%d\" d=\"%"PRId64
"\" />\n",
440 const char *basename,
int split)
447 snprintf(filename,
sizeof(filename),
"Manifest");
449 snprintf(filename,
sizeof(filename),
"%s.ismc", basename);
450 out = fopen(filename,
"w");
455 fprintf(out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
456 fprintf(out,
"<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
457 "Duration=\"%"PRId64
"\">\n", tracks->
duration * 10);
460 struct Track *first_track = track;
463 "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
465 "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
467 for (i = 0; i < tracks->
nb_tracks; i++) {
468 track = tracks->
tracks[i];
472 "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
473 "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
474 "CodecPrivateData=\"",
478 fprintf(out,
"\" />\n");
481 fprintf(stderr,
"Mismatched number of video chunks in %s and %s\n",
485 fprintf(out,
"\t</StreamIndex>\n");
489 struct Track *first_track = track;
492 "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
494 "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
496 for (i = 0; i < tracks->
nb_tracks; i++) {
497 track = tracks->
tracks[i];
501 "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
502 "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
503 "BitsPerSample=\"16\" PacketSize=\"%d\" "
504 "AudioTag=\"%d\" CodecPrivateData=\"",
509 fprintf(out,
"\" />\n");
512 fprintf(stderr,
"Mismatched number of audio chunks in %s and %s\n",
516 fprintf(out,
"\t</StreamIndex>\n");
518 fprintf(out,
"</SmoothStreamingMedia>\n");
525 for (i = 0; i < tracks->
nb_tracks; i++) {
534 int main(
int argc,
char **argv)
536 const char *basename = NULL;
542 for (i = 1; i < argc; i++) {
543 if (!strcmp(argv[i],
"-n")) {
544 basename = argv[i + 1];
546 }
else if (!strcmp(argv[i],
"-split")) {
548 }
else if (argv[i][0] ==
'-') {
549 return usage(argv[0], 1);
555 if (!tracks.
nb_tracks || (!basename && !split))
556 return usage(argv[0], 1);
static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
static int usage(const char *argv0, int ret)
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding) av_const
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.
AVCodecContext * codec
Codec context associated with this stream.
static void print_track_chunks(FILE *out, struct Tracks *tracks, int main, const char *type)
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static int write_fragments(struct Tracks *tracks, int start_index, AVIOContext *in)
struct MoofOffset * offsets
miscellaneous OS support macros and functions.
int id
Format-specific stream ID.
static int write_fragment(const char *filename, AVIOContext *in)
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,(*(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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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-> in
static char * split(char *message, char delim)
static int read_mfra(struct Tracks *tracks, int start_index, const char *file, int split)
unsigned int nb_streams
A list of all streams in the file.
int bit_rate
the average bitrate
#define AV_TIME_BASE
Internal time base represented as integer.
void * av_realloc(void *ptr, size_t size) 1(2)
Allocate or reallocate a block of memory.
int width
picture width / height.
static void output_client_manifest(struct Tracks *tracks, const char *basename, int split)
static void output_server_manifest(struct Tracks *tracks, const char *basename)
enum AVMediaType codec_type
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').
static void clean_tracks(struct Tracks *tracks)
#define MKBETAG(a, b, c, d)
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)
static int handle_file(struct Tracks *tracks, const char *file, int split)
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 AVERROR_INVALIDDATA
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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
int channels
number of audio channels
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.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...