57static int usage(
const char *argv0,
int ret)
59 fprintf(stderr,
"%s [-split] [-ismf] [-n basename] [-path-prefix prefix] "
60 "[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
97 if (got_tag != expected_tag) {
98 char got_tag_str[4], expected_tag_str[4];
100 AV_WB32(expected_tag_str, expected_tag);
101 fprintf(stderr,
"wanted tag %.4s, got %.4s\n", expected_tag_str,
124 fprintf(stderr,
"short read, wanted %d, got %d\n",
len, got);
152 fprintf(stderr,
"Unable to open %s: %s\n", filename,
av_err2str(ret));
176 int split,
int ismf,
const char* output_prefix)
178 char dirname[2048], filename[2048], idxname[2048];
179 int i, j, ret = 0, fragment_ret;
183 snprintf(idxname,
sizeof(idxname),
"%s%s.ismf", output_prefix, basename);
184 out = fopen(idxname,
"w");
194 snprintf(dirname,
sizeof(dirname),
"%sQualityLevels(%d)", output_prefix, track->
bitrate);
196 if (mkdir(dirname, 0777) == -1 && errno != EEXIST) {
202 for (j = 0; j < track->
chunks; j++) {
203 snprintf(filename,
sizeof(filename),
"%s/Fragments(%s=%"PRId64
")",
214 if (fragment_ret != 0) {
215 fprintf(stderr,
"failed fragment %d in track %d (%s)\n", j,
239 fprintf(stderr,
"No sample duration in trun flags\n");
248 for (
i = 0;
i < entries &&
pos < end;
i++) {
249 int sample_duration = default_duration;
255 if (sample_duration < 0) {
256 fprintf(stderr,
"Negative sample duration %d\n", sample_duration);
261 max_pts =
FFMAX(max_pts,
pts + sample_duration);
262 dts += sample_duration;
266 return max_pts - first_pts;
274 int default_duration = 0;
288 while (
pos < traf_pos + traf_size) {
310 fprintf(stderr,
"Couldn't find trun\n");
315 fprintf(stderr,
"Couldn't find traf\n");
334 for (
i = start_index;
i < tracks->
nb_tracks && !track;
i++)
360 for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
362 for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
364 for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
386 fprintf(stderr,
"Calculated last chunk duration for track %d "
387 "was non-positive (%"PRId64
"), probably due to missing "
396 fprintf(stderr,
"corrected to %"PRId64
"\n",
401 fprintf(stderr,
"Track duration corrected to %"PRId64
"\n",
413 const char *file,
int split,
int ismf,
414 const char *basename,
const char* output_prefix)
417 const char* err_str =
"";
428 err_str =
"mfra size mismatch";
433 err_str =
"mfra tag mismatch";
443 err_str =
"error in write_fragments";
449 fprintf(stderr,
"Unable to read the MFRA atom in %s (%s)\n", file, err_str);
467 uint16_t sps_size, pps_size;
496 int ismf,
const char *basename,
497 const char* output_prefix)
500 int err = 0,
i, orig_tracks = tracks->
nb_tracks;
506 fprintf(stderr,
"Unable to open %s: %s\n", file,
av_err2str(err));
512 fprintf(stderr,
"Unable to identify %s: %s\n", file,
av_err2str(err));
516 if (
ctx->nb_streams < 1) {
517 fprintf(stderr,
"No streams found in %s\n", file);
521 for (
i = 0;
i <
ctx->nb_streams;
i++) {
526 fprintf(stderr,
"Skipping track %d in %s as it has zero bitrate\n",
548 if ((ptr = strrchr(file,
'/')))
549 track->
name = ptr + 1;
560 "Track %d in %s is neither video nor audio, skipping\n",
615 const char *output_prefix,
616 const char *path_prefix,
617 const char *ismc_prefix)
623 snprintf(filename,
sizeof(filename),
"%s%s.ism", output_prefix, basename);
624 out = fopen(filename,
"w");
629 fprintf(
out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
630 fprintf(
out,
"<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
631 fprintf(
out,
"\t<head>\n");
632 fprintf(
out,
"\t\t<meta name=\"clientManifestRelativePath\" "
633 "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
634 fprintf(
out,
"\t</head>\n");
635 fprintf(
out,
"\t<body>\n");
636 fprintf(
out,
"\t\t<switch>\n");
640 fprintf(
out,
"\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
642 fprintf(
out,
"\t\t\t\t<param name=\"trackID\" value=\"%d\" "
643 "valueType=\"data\" />\n", track->
track_id);
644 fprintf(
out,
"\t\t\t</%s>\n",
type);
646 fprintf(
out,
"\t\t</switch>\n");
647 fprintf(
out,
"\t</body>\n");
648 fprintf(
out,
"</smil>\n");
658 int should_print_time_mismatch = 1;
664 fprintf(stderr,
"Mismatched duration of %s chunk %d in %s (%d) and %s (%d)\n",
666 should_print_time_mismatch = 1;
669 if (should_print_time_mismatch)
670 fprintf(stderr,
"Mismatched (start) time of %s chunk %d in %s (%d) and %s (%d)\n",
672 should_print_time_mismatch = 0;
676 fprintf(
out,
"\t\t<c n=\"%d\" d=\"%"PRId64
"\" ",
683 fprintf(
out,
"/>\n");
688 const char *output_prefix,
int split)
695 snprintf(filename,
sizeof(filename),
"%sManifest", output_prefix);
697 snprintf(filename,
sizeof(filename),
"%s%s.ismc", output_prefix, basename);
698 out = fopen(filename,
"w");
703 fprintf(
out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
704 fprintf(
out,
"<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
705 "Duration=\"%"PRId64
"\">\n", tracks->
duration * 10);
708 struct Track *first_track = track;
711 "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
713 "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
720 "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
721 "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
722 "CodecPrivateData=\"",
726 fprintf(
out,
"\" />\n");
729 fprintf(stderr,
"Mismatched number of video chunks in %s (id: %d, chunks %d) and %s (id: %d, chunks %d)\n",
733 fprintf(
out,
"\t</StreamIndex>\n");
737 struct Track *first_track = track;
740 "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
742 "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
749 "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
750 "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
751 "BitsPerSample=\"16\" PacketSize=\"%d\" "
752 "AudioTag=\"%d\" CodecPrivateData=\"",
757 fprintf(
out,
"\" />\n");
760 fprintf(stderr,
"Mismatched number of audio chunks in %s and %s\n",
764 fprintf(
out,
"\t</StreamIndex>\n");
766 fprintf(
out,
"</SmoothStreamingMedia>\n");
784 const char *basename =
NULL;
785 const char *path_prefix =
"", *ismc_prefix =
"";
786 const char *output_prefix =
"";
787 char output_prefix_buf[2048];
788 int split = 0, ismf = 0,
i;
789 struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
791 for (
i = 1;
i < argc;
i++) {
792 if (!strcmp(argv[
i],
"-n")) {
793 basename = argv[
i + 1];
795 }
else if (!strcmp(argv[
i],
"-path-prefix")) {
796 path_prefix = argv[
i + 1];
798 }
else if (!strcmp(argv[
i],
"-ismc-prefix")) {
799 ismc_prefix = argv[
i + 1];
801 }
else if (!strcmp(argv[
i],
"-output")) {
802 output_prefix = argv[
i + 1];
804 if (output_prefix[strlen(output_prefix) - 1] !=
'/') {
805 snprintf(output_prefix_buf,
sizeof(output_prefix_buf),
806 "%s/", output_prefix);
807 output_prefix = output_prefix_buf;
809 }
else if (!strcmp(argv[
i],
"-split")) {
811 }
else if (!strcmp(argv[
i],
"-ismf")) {
813 }
else if (argv[
i][0] ==
'-') {
814 return usage(argv[0], 1);
819 basename, output_prefix))
824 return usage(argv[0], 1);
828 path_prefix, ismc_prefix);
static char * split(char *message, char delim)
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
int avio_open2(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
uint64_t avio_rb64(AVIOContext *s)
void avio_wb32(AVIOContext *s, unsigned int val)
#define AVIO_FLAG_READ
read-only
int64_t avio_size(AVIOContext *s)
Get the filesize.
#define AVIO_FLAG_WRITE
write-only
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
unsigned int avio_rb24(AVIOContext *s)
unsigned int avio_rb32(AVIOContext *s)
int avio_r8(AVIOContext *s)
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
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
Invalid data found when processing input.
#define AVERROR_EOF
End of file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
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.
@ AV_ROUND_UP
Round toward +infinity.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
#define AV_TIME_BASE
Internal time base represented as integer.
static int skip_fragment(AVIOContext *in)
static int write_fragment(const char *filename, AVIOContext *in)
static void output_server_manifest(struct Tracks *tracks, const char *basename, const char *output_prefix, const char *path_prefix, const char *ismc_prefix)
static int skip_tag(AVIOContext *in, int32_t tag_name)
static int get_video_private_data(struct Track *track, AVCodecParameters *codecpar)
static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
static void output_client_manifest(struct Tracks *tracks, const char *basename, const char *output_prefix, int split)
static int write_fragments(struct Tracks *tracks, int start_index, AVIOContext *in, const char *basename, int split, int ismf, const char *output_prefix)
static int get_private_data(struct Track *track, AVCodecParameters *codecpar)
static int64_t read_trun_duration(AVIOContext *in, int default_duration, int64_t end)
static int64_t read_moof_duration(AVIOContext *in, int64_t offset)
static void clean_tracks(struct Tracks *tracks)
static int read_mfra(struct Tracks *tracks, int start_index, const char *file, int split, int ismf, const char *basename, const char *output_prefix)
static int handle_file(struct Tracks *tracks, const char *file, int split, int ismf, const char *basename, const char *output_prefix)
static int expect_tag(int32_t got_tag, int32_t expected_tag)
static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
static void print_track_chunks(FILE *out, struct Tracks *tracks, int main, const char *type)
#define MOV_TRUN_SAMPLE_DURATION
#define MOV_TRUN_SAMPLE_CTS
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
#define MOV_TRUN_SAMPLE_FLAGS
#define MOV_TRUN_DATA_OFFSET
#define MOV_TFHD_BASE_DATA_OFFSET
#define MOV_TRUN_SAMPLE_SIZE
#define MOV_TFHD_DEFAULT_DURATION
#define MKBETAG(a, b, c, d)
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
miscellaneous OS support macros and functions.
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
int extradata_size
Size of the extradata content in bytes.
int height
The height of the video frame in pixels.
AVChannelLayout ch_layout
The channel layout and number of channels.
int width
The width of the video frame in pixels.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
enum AVMediaType codec_type
General type of the encoded data.
int block_align
The number of bytes per coded audio frame, required by some formats.
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int sample_rate
The number of audio samples per second.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int64_t duration
Decoding: duration of the stream, in stream time base.
int id
Format-specific stream ID.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
struct MoofOffset * offsets
static AVFormatContext * ctx