|
|
@ -35,6 +35,7 @@ |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <string.h> |
|
|
|
#include <string.h> |
|
|
|
#include <sys/stat.h> |
|
|
|
#include <sys/stat.h> |
|
|
|
|
|
|
|
|
|
|
|
#include "libavformat/avformat.h" |
|
|
|
#include "libavformat/avformat.h" |
|
|
|
#include "libavutil/intreadwrite.h" |
|
|
|
#include "libavutil/intreadwrite.h" |
|
|
|
#include "libavutil/mathematics.h" |
|
|
|
#include "libavutil/mathematics.h" |
|
|
@ -82,7 +83,7 @@ static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name) |
|
|
|
int32_t size, tag; |
|
|
|
int32_t size, tag; |
|
|
|
|
|
|
|
|
|
|
|
size = avio_rb32(in); |
|
|
|
size = avio_rb32(in); |
|
|
|
tag = avio_rb32(in); |
|
|
|
tag = avio_rb32(in); |
|
|
|
avio_wb32(out, size); |
|
|
|
avio_wb32(out, size); |
|
|
|
avio_wb32(out, tag); |
|
|
|
avio_wb32(out, tag); |
|
|
|
if (tag != tag_name) |
|
|
|
if (tag != tag_name) |
|
|
@ -123,7 +124,7 @@ static int write_fragments(struct VideoFiles *files, int start_index, |
|
|
|
|
|
|
|
|
|
|
|
for (i = start_index; i < files->nb_files; i++) { |
|
|
|
for (i = start_index; i < files->nb_files; i++) { |
|
|
|
struct VideoFile *vf = files->files[i]; |
|
|
|
struct VideoFile *vf = files->files[i]; |
|
|
|
const char *type = vf->is_video ? "video" : "audio"; |
|
|
|
const char *type = vf->is_video ? "video" : "audio"; |
|
|
|
snprintf(dirname, sizeof(dirname), "QualityLevels(%d)", vf->bitrate); |
|
|
|
snprintf(dirname, sizeof(dirname), "QualityLevels(%d)", vf->bitrate); |
|
|
|
mkdir(dirname, 0777); |
|
|
|
mkdir(dirname, 0777); |
|
|
|
for (j = 0; j < vf->chunks; j++) { |
|
|
|
for (j = 0; j < vf->chunks; j++) { |
|
|
@ -140,7 +141,7 @@ static int read_tfra(struct VideoFiles *files, int start_index, AVIOContext *f) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int ret = AVERROR_EOF, track_id; |
|
|
|
int ret = AVERROR_EOF, track_id; |
|
|
|
int version, fieldlength, i, j; |
|
|
|
int version, fieldlength, i, j; |
|
|
|
int64_t pos = avio_tell(f); |
|
|
|
int64_t pos = avio_tell(f); |
|
|
|
uint32_t size = avio_rb32(f); |
|
|
|
uint32_t size = avio_rb32(f); |
|
|
|
struct VideoFile *vf = NULL; |
|
|
|
struct VideoFile *vf = NULL; |
|
|
|
|
|
|
|
|
|
|
@ -149,17 +150,16 @@ static int read_tfra(struct VideoFiles *files, int start_index, AVIOContext *f) |
|
|
|
version = avio_r8(f); |
|
|
|
version = avio_r8(f); |
|
|
|
avio_rb24(f); |
|
|
|
avio_rb24(f); |
|
|
|
track_id = avio_rb32(f); /* track id */ |
|
|
|
track_id = avio_rb32(f); /* track id */ |
|
|
|
for (i = start_index; i < files->nb_files && !vf; i++) { |
|
|
|
for (i = start_index; i < files->nb_files && !vf; i++) |
|
|
|
if (files->files[i]->track_id == track_id) |
|
|
|
if (files->files[i]->track_id == track_id) |
|
|
|
vf = files->files[i]; |
|
|
|
vf = files->files[i]; |
|
|
|
} |
|
|
|
|
|
|
|
if (!vf) { |
|
|
|
if (!vf) { |
|
|
|
/* Ok, continue parsing the next atom */ |
|
|
|
/* Ok, continue parsing the next atom */ |
|
|
|
ret = 0; |
|
|
|
ret = 0; |
|
|
|
goto fail; |
|
|
|
goto fail; |
|
|
|
} |
|
|
|
} |
|
|
|
fieldlength = avio_rb32(f); |
|
|
|
fieldlength = avio_rb32(f); |
|
|
|
vf->chunks = avio_rb32(f); |
|
|
|
vf->chunks = avio_rb32(f); |
|
|
|
vf->offsets = av_mallocz(sizeof(*vf->offsets) * vf->chunks); |
|
|
|
vf->offsets = av_mallocz(sizeof(*vf->offsets) * vf->chunks); |
|
|
|
if (!vf->offsets) { |
|
|
|
if (!vf->offsets) { |
|
|
|
ret = AVERROR(ENOMEM); |
|
|
|
ret = AVERROR(ENOMEM); |
|
|
@ -167,10 +167,10 @@ static int read_tfra(struct VideoFiles *files, int start_index, AVIOContext *f) |
|
|
|
} |
|
|
|
} |
|
|
|
for (i = 0; i < vf->chunks; i++) { |
|
|
|
for (i = 0; i < vf->chunks; i++) { |
|
|
|
if (version == 1) { |
|
|
|
if (version == 1) { |
|
|
|
vf->offsets[i].time = avio_rb64(f); |
|
|
|
vf->offsets[i].time = avio_rb64(f); |
|
|
|
vf->offsets[i].offset = avio_rb64(f); |
|
|
|
vf->offsets[i].offset = avio_rb64(f); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
vf->offsets[i].time = avio_rb32(f); |
|
|
|
vf->offsets[i].time = avio_rb32(f); |
|
|
|
vf->offsets[i].offset = avio_rb32(f); |
|
|
|
vf->offsets[i].offset = avio_rb32(f); |
|
|
|
} |
|
|
|
} |
|
|
|
for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++) |
|
|
|
for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++) |
|
|
@ -187,6 +187,7 @@ static int read_tfra(struct VideoFiles *files, int start_index, AVIOContext *f) |
|
|
|
vf->offsets[vf->chunks - 1].duration = vf->duration - |
|
|
|
vf->offsets[vf->chunks - 1].duration = vf->duration - |
|
|
|
vf->offsets[vf->chunks - 1].time; |
|
|
|
vf->offsets[vf->chunks - 1].time; |
|
|
|
ret = 0; |
|
|
|
ret = 0; |
|
|
|
|
|
|
|
|
|
|
|
fail: |
|
|
|
fail: |
|
|
|
avio_seek(f, pos + size, SEEK_SET); |
|
|
|
avio_seek(f, pos + size, SEEK_SET); |
|
|
|
return ret; |
|
|
|
return ret; |
|
|
@ -224,7 +225,7 @@ fail: |
|
|
|
static int get_private_data(struct VideoFile *vf, AVCodecContext *codec) |
|
|
|
static int get_private_data(struct VideoFile *vf, AVCodecContext *codec) |
|
|
|
{ |
|
|
|
{ |
|
|
|
vf->codec_private_size = codec->extradata_size; |
|
|
|
vf->codec_private_size = codec->extradata_size; |
|
|
|
vf->codec_private = av_mallocz(codec->extradata_size); |
|
|
|
vf->codec_private = av_mallocz(codec->extradata_size); |
|
|
|
if (!vf->codec_private) |
|
|
|
if (!vf->codec_private) |
|
|
|
return AVERROR(ENOMEM); |
|
|
|
return AVERROR(ENOMEM); |
|
|
|
memcpy(vf->codec_private, codec->extradata, codec->extradata_size); |
|
|
|
memcpy(vf->codec_private, codec->extradata, codec->extradata_size); |
|
|
@ -254,6 +255,7 @@ static int get_video_private_data(struct VideoFile *vf, AVCodecContext *codec) |
|
|
|
avio_wb32(io, 0x00000001); |
|
|
|
avio_wb32(io, 0x00000001); |
|
|
|
avio_write(io, &codec->extradata[11 + sps_size], pps_size); |
|
|
|
avio_write(io, &codec->extradata[11 + sps_size], pps_size); |
|
|
|
err = 0; |
|
|
|
err = 0; |
|
|
|
|
|
|
|
|
|
|
|
fail: |
|
|
|
fail: |
|
|
|
vf->codec_private_size = avio_close_dyn_buf(io, &vf->codec_private); |
|
|
|
vf->codec_private_size = avio_close_dyn_buf(io, &vf->codec_private); |
|
|
|
return err; |
|
|
|
return err; |
|
|
@ -291,24 +293,25 @@ static int handle_file(struct VideoFiles *files, const char *file, int split) |
|
|
|
AVStream *st = ctx->streams[i]; |
|
|
|
AVStream *st = ctx->streams[i]; |
|
|
|
vf = av_mallocz(sizeof(*vf)); |
|
|
|
vf = av_mallocz(sizeof(*vf)); |
|
|
|
files->files = av_realloc(files->files, |
|
|
|
files->files = av_realloc(files->files, |
|
|
|
sizeof(*files->files)*(files->nb_files + 1)); |
|
|
|
sizeof(*files->files) * (files->nb_files + 1)); |
|
|
|
files->files[files->nb_files] = vf; |
|
|
|
files->files[files->nb_files] = vf; |
|
|
|
|
|
|
|
|
|
|
|
vf->name = file; |
|
|
|
vf->name = file; |
|
|
|
if ((ptr = strrchr(file, '/')) != NULL) |
|
|
|
if ((ptr = strrchr(file, '/')) != NULL) |
|
|
|
vf->name = ptr + 1; |
|
|
|
vf->name = ptr + 1; |
|
|
|
|
|
|
|
|
|
|
|
vf->bitrate = st->codec->bit_rate; |
|
|
|
vf->bitrate = st->codec->bit_rate; |
|
|
|
vf->track_id = st->id; |
|
|
|
vf->track_id = st->id; |
|
|
|
vf->timescale = st->time_base.den; |
|
|
|
vf->timescale = st->time_base.den; |
|
|
|
vf->duration = av_rescale_rnd(ctx->duration, vf->timescale, |
|
|
|
vf->duration = av_rescale_rnd(ctx->duration, vf->timescale, |
|
|
|
AV_TIME_BASE, AV_ROUND_UP); |
|
|
|
AV_TIME_BASE, AV_ROUND_UP); |
|
|
|
vf->is_audio = st->codec->codec_type == AVMEDIA_TYPE_AUDIO; |
|
|
|
vf->is_audio = st->codec->codec_type == AVMEDIA_TYPE_AUDIO; |
|
|
|
vf->is_video = st->codec->codec_type == AVMEDIA_TYPE_VIDEO; |
|
|
|
vf->is_video = st->codec->codec_type == AVMEDIA_TYPE_VIDEO; |
|
|
|
|
|
|
|
|
|
|
|
if (!vf->is_audio && !vf->is_video) { |
|
|
|
if (!vf->is_audio && !vf->is_video) { |
|
|
|
fprintf(stderr, "Track %d in %s is neither video nor audio, " |
|
|
|
fprintf(stderr, |
|
|
|
"skipping\n", vf->track_id, file); |
|
|
|
"Track %d in %s is neither video nor audio, skipping\n", |
|
|
|
|
|
|
|
vf->track_id, file); |
|
|
|
av_freep(&files->files[files->nb_files]); |
|
|
|
av_freep(&files->files[files->nb_files]); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
@ -317,15 +320,15 @@ static int handle_file(struct VideoFiles *files, const char *file, int split) |
|
|
|
if (files->audio_file < 0) |
|
|
|
if (files->audio_file < 0) |
|
|
|
files->audio_file = files->nb_files; |
|
|
|
files->audio_file = files->nb_files; |
|
|
|
files->nb_audio_files++; |
|
|
|
files->nb_audio_files++; |
|
|
|
vf->channels = st->codec->channels; |
|
|
|
vf->channels = st->codec->channels; |
|
|
|
vf->sample_rate = st->codec->sample_rate; |
|
|
|
vf->sample_rate = st->codec->sample_rate; |
|
|
|
if (st->codec->codec_id == CODEC_ID_AAC) { |
|
|
|
if (st->codec->codec_id == CODEC_ID_AAC) { |
|
|
|
vf->fourcc = "AACL"; |
|
|
|
vf->fourcc = "AACL"; |
|
|
|
vf->tag = 255; |
|
|
|
vf->tag = 255; |
|
|
|
vf->blocksize = 4; |
|
|
|
vf->blocksize = 4; |
|
|
|
} else if (st->codec->codec_id == CODEC_ID_WMAPRO) { |
|
|
|
} else if (st->codec->codec_id == CODEC_ID_WMAPRO) { |
|
|
|
vf->fourcc = "WMAP"; |
|
|
|
vf->fourcc = "WMAP"; |
|
|
|
vf->tag = st->codec->codec_tag; |
|
|
|
vf->tag = st->codec->codec_tag; |
|
|
|
vf->blocksize = st->codec->block_align; |
|
|
|
vf->blocksize = st->codec->block_align; |
|
|
|
} |
|
|
|
} |
|
|
|
get_private_data(vf, st->codec); |
|
|
|
get_private_data(vf, st->codec); |
|
|
@ -334,7 +337,7 @@ static int handle_file(struct VideoFiles *files, const char *file, int split) |
|
|
|
if (files->video_file < 0) |
|
|
|
if (files->video_file < 0) |
|
|
|
files->video_file = files->nb_files; |
|
|
|
files->video_file = files->nb_files; |
|
|
|
files->nb_video_files++; |
|
|
|
files->nb_video_files++; |
|
|
|
vf->width = st->codec->width; |
|
|
|
vf->width = st->codec->width; |
|
|
|
vf->height = st->codec->height; |
|
|
|
vf->height = st->codec->height; |
|
|
|
if (st->codec->codec_id == CODEC_ID_H264) |
|
|
|
if (st->codec->codec_id == CODEC_ID_H264) |
|
|
|
vf->fourcc = "H264"; |
|
|
|
vf->fourcc = "H264"; |
|
|
@ -379,7 +382,7 @@ static void output_server_manifest(struct VideoFiles *files, |
|
|
|
fprintf(out, "\t\t<switch>\n"); |
|
|
|
fprintf(out, "\t\t<switch>\n"); |
|
|
|
for (i = 0; i < files->nb_files; i++) { |
|
|
|
for (i = 0; i < files->nb_files; i++) { |
|
|
|
struct VideoFile *vf = files->files[i]; |
|
|
|
struct VideoFile *vf = files->files[i]; |
|
|
|
const char *type = vf->is_video ? "video" : "audio"; |
|
|
|
const char *type = vf->is_video ? "video" : "audio"; |
|
|
|
fprintf(out, "\t\t\t<%s src=\"%s\" systemBitrate=\"%d\">\n", |
|
|
|
fprintf(out, "\t\t\t<%s src=\"%s\" systemBitrate=\"%d\">\n", |
|
|
|
type, vf->name, vf->bitrate); |
|
|
|
type, vf->name, vf->bitrate); |
|
|
|
fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" " |
|
|
|
fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" " |
|
|
@ -410,22 +413,24 @@ static void output_client_manifest(struct VideoFiles *files, |
|
|
|
} |
|
|
|
} |
|
|
|
fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); |
|
|
|
fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); |
|
|
|
fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" " |
|
|
|
fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" " |
|
|
|
"Duration=\"%"PRId64"\">\n", files->duration * 10); |
|
|
|
"Duration=\"%"PRId64 "\">\n", files->duration * 10); |
|
|
|
if (files->video_file >= 0) { |
|
|
|
if (files->video_file >= 0) { |
|
|
|
struct VideoFile *vf = files->files[files->video_file]; |
|
|
|
struct VideoFile *vf = files->files[files->video_file]; |
|
|
|
int index = 0; |
|
|
|
int index = 0; |
|
|
|
fprintf(out, "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" " |
|
|
|
fprintf(out, |
|
|
|
"Chunks=\"%d\" " |
|
|
|
"\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" " |
|
|
|
"Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n", |
|
|
|
"Chunks=\"%d\" " |
|
|
|
files->nb_video_files, vf->chunks); |
|
|
|
"Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n", |
|
|
|
|
|
|
|
files->nb_video_files, vf->chunks); |
|
|
|
for (i = 0; i < files->nb_files; i++) { |
|
|
|
for (i = 0; i < files->nb_files; i++) { |
|
|
|
vf = files->files[i]; |
|
|
|
vf = files->files[i]; |
|
|
|
if (!vf->is_video) |
|
|
|
if (!vf->is_video) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
fprintf(out, "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" " |
|
|
|
fprintf(out, |
|
|
|
"FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" " |
|
|
|
"\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" " |
|
|
|
"CodecPrivateData=\"", |
|
|
|
"FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" " |
|
|
|
index, vf->bitrate, vf->fourcc, vf->width, vf->height); |
|
|
|
"CodecPrivateData=\"", |
|
|
|
|
|
|
|
index, vf->bitrate, vf->fourcc, vf->width, vf->height); |
|
|
|
for (j = 0; j < vf->codec_private_size; j++) |
|
|
|
for (j = 0; j < vf->codec_private_size; j++) |
|
|
|
fprintf(out, "%02X", vf->codec_private[j]); |
|
|
|
fprintf(out, "%02X", vf->codec_private[j]); |
|
|
|
fprintf(out, "\" />\n"); |
|
|
|
fprintf(out, "\" />\n"); |
|
|
@ -440,20 +445,22 @@ static void output_client_manifest(struct VideoFiles *files, |
|
|
|
if (files->audio_file >= 0) { |
|
|
|
if (files->audio_file >= 0) { |
|
|
|
struct VideoFile *vf = files->files[files->audio_file]; |
|
|
|
struct VideoFile *vf = files->files[files->audio_file]; |
|
|
|
int index = 0; |
|
|
|
int index = 0; |
|
|
|
fprintf(out, "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" " |
|
|
|
fprintf(out, |
|
|
|
"Chunks=\"%d\" " |
|
|
|
"\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" " |
|
|
|
"Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n", |
|
|
|
"Chunks=\"%d\" " |
|
|
|
files->nb_audio_files, vf->chunks); |
|
|
|
"Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n", |
|
|
|
|
|
|
|
files->nb_audio_files, vf->chunks); |
|
|
|
for (i = 0; i < files->nb_files; i++) { |
|
|
|
for (i = 0; i < files->nb_files; i++) { |
|
|
|
vf = files->files[i]; |
|
|
|
vf = files->files[i]; |
|
|
|
if (!vf->is_audio) |
|
|
|
if (!vf->is_audio) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
fprintf(out, "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" " |
|
|
|
fprintf(out, |
|
|
|
"FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" " |
|
|
|
"\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" " |
|
|
|
"BitsPerSample=\"16\" PacketSize=\"%d\" " |
|
|
|
"FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" " |
|
|
|
"AudioTag=\"%d\" CodecPrivateData=\"", |
|
|
|
"BitsPerSample=\"16\" PacketSize=\"%d\" " |
|
|
|
index, vf->bitrate, vf->fourcc, vf->sample_rate, |
|
|
|
"AudioTag=\"%d\" CodecPrivateData=\"", |
|
|
|
vf->channels, vf->blocksize, vf->tag); |
|
|
|
index, vf->bitrate, vf->fourcc, vf->sample_rate, |
|
|
|
|
|
|
|
vf->channels, vf->blocksize, vf->tag); |
|
|
|
for (j = 0; j < vf->codec_private_size; j++) |
|
|
|
for (j = 0; j < vf->codec_private_size; j++) |
|
|
|
fprintf(out, "%02X", vf->codec_private[j]); |
|
|
|
fprintf(out, "%02X", vf->codec_private[j]); |
|
|
|
fprintf(out, "\" />\n"); |
|
|
|
fprintf(out, "\" />\n"); |
|
|
|