
Recherche avancée
Médias (91)
-
Richard Stallman et le logiciel libre
19 octobre 2011, par
Mis à jour : Mai 2013
Langue : français
Type : Texte
-
Stereo master soundtrack
17 octobre 2011, par
Mis à jour : Octobre 2011
Langue : English
Type : Audio
-
Elephants Dream - Cover of the soundtrack
17 octobre 2011, par
Mis à jour : Octobre 2011
Langue : English
Type : Image
-
#7 Ambience
16 octobre 2011, par
Mis à jour : Juin 2015
Langue : English
Type : Audio
-
#6 Teaser Music
16 octobre 2011, par
Mis à jour : Février 2013
Langue : English
Type : Audio
-
#5 End Title
16 octobre 2011, par
Mis à jour : Février 2013
Langue : English
Type : Audio
Autres articles (69)
-
Websites made with MediaSPIP
2 mai 2011, parThis page lists some websites based on MediaSPIP.
-
Creating farms of unique websites
13 avril 2011, parMediaSPIP platforms can be installed as a farm, with a single "core" hosted on a dedicated server and used by multiple websites.
This allows (among other things) : implementation costs to be shared between several different projects / individuals rapid deployment of multiple unique sites creation of groups of like-minded sites, making it possible to browse media in a more controlled and selective environment than the major "open" (...) -
Les statuts des instances de mutualisation
13 mars 2010, parPour des raisons de compatibilité générale du plugin de gestion de mutualisations avec les fonctions originales de SPIP, les statuts des instances sont les mêmes que pour tout autre objets (articles...), seuls leurs noms dans l’interface change quelque peu.
Les différents statuts possibles sont : prepa (demandé) qui correspond à une instance demandée par un utilisateur. Si le site a déjà été créé par le passé, il est passé en mode désactivé. publie (validé) qui correspond à une instance validée par un (...)
Sur d’autres sites (15706)
-
How to use ffmpeg lib transform mp4(h264&aac) to m3u8 (hls) by C code ?
1er juillet 2020, par itningI used official examples
transcoding.c
but console printpkt->duration = 0, maybe the hls segment duration will not precise
.

I use this code to set duration but invalid。


av_opt_set_int(ofmt_ctx->priv_data, "hls_time", 5, AV_OPT_SEARCH_CHILDREN);


In command line


ffmpeg -i a.mp4 -codec copy -vbsf h264_mp4toannexb -map 0 -f segment -segment_list a.m3u8 -segment_time 10 a-%03d.ts


How to use C code to achieve this command ?


this is my code :


/**
 * @file
 * API example for demuxing, decoding, filtering, encoding and muxing
 * @example transcoding.c
 */

#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavfilter></libavfilter>buffersink.h>
#include <libavfilter></libavfilter>buffersrc.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>pixdesc.h>

static AVFormatContext *ifmt_ctx;
static AVFormatContext *ofmt_ctx;
typedef struct FilteringContext {
 AVFilterContext *buffersink_ctx;
 AVFilterContext *buffersrc_ctx;
 AVFilterGraph *filter_graph;
} FilteringContext;
static FilteringContext *filter_ctx;

typedef struct StreamContext {
 AVCodecContext *dec_ctx;
 AVCodecContext *enc_ctx;
} StreamContext;
static StreamContext *stream_ctx;

static int open_input_file(const char *filename) {
 int ret;
 unsigned int i;

 ifmt_ctx = NULL;
 if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
 return ret;
 }

 if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
 return ret;
 }

 stream_ctx = av_mallocz_array(ifmt_ctx->nb_streams, sizeof(*stream_ctx));
 if (!stream_ctx)
 return AVERROR(ENOMEM);

 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
 AVStream *stream = ifmt_ctx->streams[i];
 AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
 AVCodecContext *codec_ctx;
 if (!dec) {
 av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i);
 return AVERROR_DECODER_NOT_FOUND;
 }
 codec_ctx = avcodec_alloc_context3(dec);
 if (!codec_ctx) {
 av_log(NULL, AV_LOG_ERROR, "Failed to allocate the decoder context for stream #%u\n", i);
 return AVERROR(ENOMEM);
 }
 ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context "
 "for stream #%u\n", i);
 return ret;
 }
 /* Reencode video & audio and remux subtitles etc. */
 if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
 || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
 if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
 codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, stream, NULL);
 /* Open decoder */
 ret = avcodec_open2(codec_ctx, dec, NULL);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
 return ret;
 }
 }
 stream_ctx[i].dec_ctx = codec_ctx;
 }

 av_dump_format(ifmt_ctx, 0, filename, 0);
 return 0;
}

static int open_output_file(const char *filename, enum AVCodecID videoCodecId, enum AVCodecID audioCodecId) {
 AVStream *out_stream;
 AVStream *in_stream;
 AVCodecContext *dec_ctx, *enc_ctx;
 AVCodec *encoder;
 int ret;
 unsigned int i;

 ofmt_ctx = NULL;
 avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
 if (!ofmt_ctx) {
 av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
 return AVERROR_UNKNOWN;
 }

 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
 out_stream = avformat_new_stream(ofmt_ctx, NULL);
 if (!out_stream) {
 av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
 return AVERROR_UNKNOWN;
 }

 in_stream = ifmt_ctx->streams[i];
 dec_ctx = stream_ctx[i].dec_ctx;

 if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
 || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {

 if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
 encoder = avcodec_find_encoder(videoCodecId);
 } else {
 encoder = avcodec_find_encoder(audioCodecId);
 }
 //encoder = avcodec_find_encoder(dec_ctx->codec_id);
 if (!encoder) {
 av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
 return AVERROR_INVALIDDATA;
 }
 enc_ctx = avcodec_alloc_context3(encoder);
 if (!enc_ctx) {
 av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
 return AVERROR(ENOMEM);
 }

 /* In this example, we transcode to same properties (picture size,
 * sample rate etc.). These properties can be changed for output
 * streams easily using filters */
 if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
 enc_ctx->height = dec_ctx->height;
 enc_ctx->width = dec_ctx->width;
 enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
 /* take first format from list of supported formats */
 if (encoder->pix_fmts)
 enc_ctx->pix_fmt = encoder->pix_fmts[0];
 else
 enc_ctx->pix_fmt = dec_ctx->pix_fmt;
 /* video time_base can be set to whatever is handy and supported by encoder */
 enc_ctx->time_base = av_inv_q(dec_ctx->framerate);
 } else {
 enc_ctx->sample_rate = dec_ctx->sample_rate;
 enc_ctx->channel_layout = dec_ctx->channel_layout;
 enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
 /* take first format from list of supported formats */
 enc_ctx->sample_fmt = encoder->sample_fmts[0];
 enc_ctx->time_base = (AVRational) {1, enc_ctx->sample_rate};
 }

 if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
 enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

 /* Third parameter can be used to pass settings to encoder */
 ret = avcodec_open2(enc_ctx, encoder, NULL);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
 return ret;
 }
 ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
 return ret;
 }

 out_stream->time_base = enc_ctx->time_base;
 stream_ctx[i].enc_ctx = enc_ctx;
 } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
 av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
 return AVERROR_INVALIDDATA;
 } else {
 /* if this stream must be remuxed */
 ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Copying parameters for stream #%u failed\n", i);
 return ret;
 }
 out_stream->time_base = in_stream->time_base;
 }

 }
 av_dump_format(ofmt_ctx, 0, filename, 1);

 av_opt_set_int(ofmt_ctx->priv_data, "hls_time", 5, AV_OPT_SEARCH_CHILDREN);

 if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
 ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
 return ret;
 }
 }

 /* init muxer, write output file header */
 ret = avformat_write_header(ofmt_ctx, NULL);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
 return ret;
 }

 return 0;
}

static int init_filter(FilteringContext *fctx, AVCodecContext *dec_ctx,
 AVCodecContext *enc_ctx, const char *filter_spec) {
 char args[512];
 int ret = 0;
 const AVFilter *buffersrc = NULL;
 const AVFilter *buffersink = NULL;
 AVFilterContext *buffersrc_ctx = NULL;
 AVFilterContext *buffersink_ctx = NULL;
 AVFilterInOut *outputs = avfilter_inout_alloc();
 AVFilterInOut *inputs = avfilter_inout_alloc();
 AVFilterGraph *filter_graph = avfilter_graph_alloc();

 if (!outputs || !inputs || !filter_graph) {
 ret = AVERROR(ENOMEM);
 goto end;
 }

 if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
 buffersrc = avfilter_get_by_name("buffer");
 buffersink = avfilter_get_by_name("buffersink");
 if (!buffersrc || !buffersink) {
 av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
 ret = AVERROR_UNKNOWN;
 goto end;
 }

 snprintf(args, sizeof(args),
 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
 dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
 dec_ctx->time_base.num, dec_ctx->time_base.den,
 dec_ctx->sample_aspect_ratio.num,
 dec_ctx->sample_aspect_ratio.den);

 ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
 args, NULL, filter_graph);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
 goto end;
 }

 ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
 NULL, NULL, filter_graph);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
 goto end;
 }

 ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
 (uint8_t *) &enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
 AV_OPT_SEARCH_CHILDREN);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
 goto end;
 }
 } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
 buffersrc = avfilter_get_by_name("abuffer");
 buffersink = avfilter_get_by_name("abuffersink");
 if (!buffersrc || !buffersink) {
 av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
 ret = AVERROR_UNKNOWN;
 goto end;
 }

 if (!dec_ctx->channel_layout)
 dec_ctx->channel_layout =
 av_get_default_channel_layout(dec_ctx->channels);
 snprintf(args, sizeof(args),
 "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
 dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
 av_get_sample_fmt_name(dec_ctx->sample_fmt),
 dec_ctx->channel_layout);
 ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
 args, NULL, filter_graph);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
 goto end;
 }

 ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
 NULL, NULL, filter_graph);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
 goto end;
 }

 ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
 (uint8_t *) &enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
 AV_OPT_SEARCH_CHILDREN);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
 goto end;
 }

 ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
 (uint8_t *) &enc_ctx->channel_layout,
 sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
 goto end;
 }

 ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
 (uint8_t *) &enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
 AV_OPT_SEARCH_CHILDREN);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
 goto end;
 }
 } else {
 ret = AVERROR_UNKNOWN;
 goto end;
 }

 /* Endpoints for the filter graph. */
 outputs->name = av_strdup("in");
 outputs->filter_ctx = buffersrc_ctx;
 outputs->pad_idx = 0;
 outputs->next = NULL;

 inputs->name = av_strdup("out");
 inputs->filter_ctx = buffersink_ctx;
 inputs->pad_idx = 0;
 inputs->next = NULL;

 if (!outputs->name || !inputs->name) {
 ret = AVERROR(ENOMEM);
 goto end;
 }

 if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
 &inputs, &outputs, NULL)) < 0)
 goto end;

 if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
 goto end;

 /* Fill FilteringContext */
 fctx->buffersrc_ctx = buffersrc_ctx;
 fctx->buffersink_ctx = buffersink_ctx;
 fctx->filter_graph = filter_graph;

 end:
 avfilter_inout_free(&inputs);
 avfilter_inout_free(&outputs);

 return ret;
}

static int init_filters(void) {
 const char *filter_spec;
 unsigned int i;
 int ret;
 filter_ctx = av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
 if (!filter_ctx)
 return AVERROR(ENOMEM);

 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
 filter_ctx[i].buffersrc_ctx = NULL;
 filter_ctx[i].buffersink_ctx = NULL;
 filter_ctx[i].filter_graph = NULL;
 if (!(ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
 || ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
 continue;


 if (ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
 filter_spec = "null"; /* passthrough (dummy) filter for video */
 else
 filter_spec = "anull"; /* passthrough (dummy) filter for audio */
 ret = init_filter(&filter_ctx[i], stream_ctx[i].dec_ctx,
 stream_ctx[i].enc_ctx, filter_spec);
 if (ret)
 return ret;
 }
 return 0;
}

static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
 int ret;
 int got_frame_local;
 AVPacket enc_pkt;
 int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
 (ifmt_ctx->streams[stream_index]->codecpar->codec_type ==
 AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2;

 if (!got_frame)
 got_frame = &got_frame_local;

 av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
 /* encode filtered frame */
 enc_pkt.data = NULL;
 enc_pkt.size = 0;
 av_init_packet(&enc_pkt);
 ret = enc_func(stream_ctx[stream_index].enc_ctx, &enc_pkt,
 filt_frame, got_frame);
 av_frame_free(&filt_frame);
 if (ret < 0)
 return ret;
 if (!(*got_frame))
 return 0;

 /* prepare packet for muxing */
 enc_pkt.stream_index = stream_index;
 av_packet_rescale_ts(&enc_pkt,
 stream_ctx[stream_index].enc_ctx->time_base,
 ofmt_ctx->streams[stream_index]->time_base);

 av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
 /* mux encoded frame */
 ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
 return ret;
}

static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index) {
 int ret;
 AVFrame *filt_frame;

 av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
 /* push the decoded frame into the filtergraph */
 ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
 frame, 0);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
 return ret;
 }

 /* pull filtered frames from the filtergraph */
 while (1) {
 filt_frame = av_frame_alloc();
 if (!filt_frame) {
 ret = AVERROR(ENOMEM);
 break;
 }
 av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
 ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
 filt_frame);
 if (ret < 0) {
 /* if no more frames for output - returns AVERROR(EAGAIN)
 * if flushed and no more frames for output - returns AVERROR_EOF
 * rewrite retcode to 0 to show it as normal procedure completion
 */
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
 ret = 0;
 av_frame_free(&filt_frame);
 break;
 }

 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
 ret = encode_write_frame(filt_frame, stream_index, NULL);
 if (ret < 0)
 break;
 }

 return ret;
}

static int flush_encoder(unsigned int stream_index) {
 int ret;
 int got_frame;

 if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
 AV_CODEC_CAP_DELAY))
 return 0;

 while (1) {
 av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
 ret = encode_write_frame(NULL, stream_index, &got_frame);
 if (ret < 0)
 break;
 if (!got_frame)
 return 0;
 }
 return ret;
}

int main() {
 char *inputFile = "D:/20200623_094923.mp4";
 char *outputFile = "D:/test/te.m3u8";
 enum AVCodecID videoCodec = AV_CODEC_ID_H264;
 enum AVCodecID audioCodec = AV_CODEC_ID_AAC;

 int ret;
 AVPacket packet = {.data = NULL, .size = 0};
 AVFrame *frame = NULL;
 enum AVMediaType type;
 unsigned int stream_index;
 unsigned int i;
 int got_frame;
 int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);

 if ((ret = open_input_file(inputFile)) < 0)
 goto end;
 if ((ret = open_output_file(outputFile, videoCodec, audioCodec)) < 0)
 goto end;
 if ((ret = init_filters()) < 0)
 goto end;

 /* read all packets */
 while (1) {
 if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
 break;
 stream_index = packet.stream_index;
 type = ifmt_ctx->streams[packet.stream_index]->codecpar->codec_type;
 av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
 stream_index);

 if (filter_ctx[stream_index].filter_graph) {
 av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
 frame = av_frame_alloc();
 if (!frame) {
 ret = AVERROR(ENOMEM);
 break;
 }
 av_packet_rescale_ts(&packet,
 ifmt_ctx->streams[stream_index]->time_base,
 stream_ctx[stream_index].dec_ctx->time_base);
 dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
 avcodec_decode_audio4;
 ret = dec_func(stream_ctx[stream_index].dec_ctx, frame,
 &got_frame, &packet);
 if (ret < 0) {
 av_frame_free(&frame);
 av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
 break;
 }

 if (got_frame) {
 frame->pts = frame->best_effort_timestamp;
 ret = filter_encode_write_frame(frame, stream_index);
 av_frame_free(&frame);
 if (ret < 0)
 goto end;
 } else {
 av_frame_free(&frame);
 }
 } else {
 /* remux this frame without reencoding */
 av_packet_rescale_ts(&packet,
 ifmt_ctx->streams[stream_index]->time_base,
 ofmt_ctx->streams[stream_index]->time_base);

 ret = av_interleaved_write_frame(ofmt_ctx, &packet);
 if (ret < 0)
 goto end;
 }
 av_packet_unref(&packet);
 }

 /* flush filters and encoders */
 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
 /* flush filter */
 if (!filter_ctx[i].filter_graph)
 continue;
 ret = filter_encode_write_frame(NULL, i);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
 goto end;
 }

 /* flush encoder */
 ret = flush_encoder(i);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
 goto end;
 }
 }

 av_write_trailer(ofmt_ctx);
 end:
 av_packet_unref(&packet);
 av_frame_free(&frame);
 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
 avcodec_free_context(&stream_ctx[i].dec_ctx);
 if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && stream_ctx[i].enc_ctx)
 avcodec_free_context(&stream_ctx[i].enc_ctx);
 if (filter_ctx && filter_ctx[i].filter_graph)
 avfilter_graph_free(&filter_ctx[i].filter_graph);
 }
 av_free(filter_ctx);
 av_free(stream_ctx);
 avformat_close_input(&ifmt_ctx);
 if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
 avio_closep(&ofmt_ctx->pb);
 avformat_free_context(ofmt_ctx);

 if (ret < 0)
 av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));

 return ret ? 1 : 0;
}



-
Muxing Android MediaCodec encoded H264 packets into RTMP
31 décembre 2015, par VadymI am coming from a thread Encoding H.264 from camera with Android MediaCodec. My setup is very similar. However, I attempt to write mux the encoded frames and with javacv and broadcast them via rtmp.
RtmpClient.java
...
private volatile BlockingQueue mFrameQueue = new LinkedBlockingQueue(MAXIMUM_VIDEO_FRAME_BACKLOG);
...
private void startStream() throws FrameRecorder.Exception, IOException {
if (TextUtils.isEmpty(mDestination)) {
throw new IllegalStateException("Cannot start RtmpClient without destination");
}
if (mCamera == null) {
throw new IllegalStateException("Cannot start RtmpClient without camera.");
}
Camera.Parameters cameraParams = mCamera.getParameters();
mRecorder = new FFmpegFrameRecorder(
mDestination,
mVideoQuality.resX,
mVideoQuality.resY,
(mAudioQuality.channelType.equals(AudioQuality.CHANNEL_TYPE_STEREO) ? 2 : 1));
mRecorder.setFormat("flv");
mRecorder.setFrameRate(mVideoQuality.frameRate);
mRecorder.setVideoBitrate(mVideoQuality.bitRate);
mRecorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
mRecorder.setSampleRate(mAudioQuality.samplingRate);
mRecorder.setAudioBitrate(mAudioQuality.bitRate);
mRecorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
mVideoStream = new VideoStream(mRecorder, mVideoQuality, mFrameQueue, mCamera);
mAudioStream = new AudioStream(mRecorder, mAudioQuality);
mRecorder.start();
// Setup a bufferred preview callback
setupCameraCallback(mCamera, mRtmpClient, DEFAULT_PREVIEW_CALLBACK_BUFFERS,
mVideoQuality.resX * mVideoQuality.resY * ImageFormat.getBitsPerPixel(
cameraParams.getPreviewFormat())/8);
try {
mVideoStream.start();
mAudioStream.start();
}
catch(Exception e) {
e.printStackTrace();
stopStream();
}
}
...
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
boolean frameQueued = false;
if (mRecorder == null || data == null) {
return;
}
frameQueued = mFrameQueue.offer(data);
// return the buffer to be reused - done in videostream
//camera.addCallbackBuffer(data);
}
...VideoStream.java
...
@Override
public void run() {
try {
mMediaCodec = MediaCodec.createEncoderByType("video/avc");
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", mVideoQuality.resX, mVideoQuality.resY);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, mVideoQuality.bitRate);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, mVideoQuality.frameRate);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
mMediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();
}
catch(IOException e) {
e.printStackTrace();
}
long startTimestamp = System.currentTimeMillis();
long frameTimestamp = 0;
byte[] rawFrame = null;
try {
while (!Thread.interrupted()) {
rawFrame = mFrameQueue.take();
frameTimestamp = 1000 * (System.currentTimeMillis() - startTimestamp);
encodeFrame(rawFrame, frameTimestamp);
// return the buffer to be reused
mCamera.addCallbackBuffer(rawFrame);
}
}
catch (InterruptedException ignore) {
// ignore interrup while waiting
}
// Clean up video stream allocations
try {
mMediaCodec.stop();
mMediaCodec.release();
mOutputStream.flush();
mOutputStream.close();
} catch (Exception e){
e.printStackTrace();
}
}
...
private void encodeFrame(byte[] input, long timestamp) {
try {
ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mMediaCodec.getOutputBuffers();
int inputBufferIndex = mMediaCodec.dequeueInputBuffer(0);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(input);
mMediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, timestamp, 0);
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
if (outputBufferIndex >= 0) {
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
// Should this be a direct byte buffer?
byte[] outData = new byte[bufferInfo.size - bufferInfo.offset];
outputBuffer.get(outData);
mFrameRecorder.record(outData, bufferInfo.offset, outData.length, timestamp);
mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
}
else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
outputBuffers = mMediaCodec.getOutputBuffers();
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// ignore for now
}
} catch (Throwable t) {
t.printStackTrace();
}
}
...FFmpegFrameRecorder.java
...
// Hackish codec copy frame recording function
public boolean record(byte[] encodedData, int offset, int length, long frameCount) throws Exception {
int ret;
if (encodedData == null) {
return false;
}
av_init_packet(video_pkt);
// this is why i wondered whether I should get outputbuffer data into direct byte buffer
video_outbuf.put(encodedData, 0, encodedData.length);
video_pkt.data(video_outbuf);
video_pkt.size(video_outbuf_size);
video_pkt.pts(frameCount);
video_pkt.dts(frameCount);
video_pkt.stream_index(video_st.index());
synchronized (oc) {
/* write the compressed frame in the media file */
if (interleaved && audio_st != null) {
if ((ret = av_interleaved_write_frame(oc, video_pkt)) < 0) {
throw new Exception("av_interleaved_write_frame() error " + ret + " while writing interleaved video frame.");
}
} else {
if ((ret = av_write_frame(oc, video_pkt)) < 0) {
throw new Exception("av_write_frame() error " + ret + " while writing video frame.");
}
}
}
return (video_pkt.flags() & AV_PKT_FLAG_KEY) == 1;
}
...When I try to stream the video and run ffprobe on it, I get the following output :
ffprobe version 2.5.3 Copyright (c) 2007-2015 the FFmpeg developers
built on Jan 19 2015 12:56:57 with gcc 4.1.2 (GCC) 20080704 (Red Hat 4.1.2-55)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-libass --enable-libdc1394 --enable-libfaac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --enable-libcaca --shlibdir=/usr/lib64 --enable-runtime-cpudetect
libavutil 54. 15.100 / 54. 15.100
libavcodec 56. 13.100 / 56. 13.100
libavformat 56. 15.102 / 56. 15.102
libavdevice 56. 3.100 / 56. 3.100
libavfilter 5. 2.103 / 5. 2.103
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
Metadata:
Server NGINX RTMP (github.com/arut/nginx-rtmp-module)
width 320.00
height 240.00
displayWidth 320.00
displayHeight 240.00
duration 0.00
framerate 0.00
fps 0.00
videodatarate 261.00
videocodecid 7.00
audiodatarate 62.00
audiocodecid 10.00
profile
level
[live_flv @ 0x1edb0820] Could not find codec parameters for stream 0 (Video: none, none, 267 kb/s): unknown codec
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, live_flv, from 'rtmp://<server>/input/<stream>':
Metadata:
Server : NGINX RTMP (github.com/arut/nginx-rtmp-module)
displayWidth : 320
displayHeight : 240
fps : 0
profile :
level :
Duration: 00:00:00.00, start: 16.768000, bitrate: N/A
Stream #0:0: Video: none, none, 267 kb/s, 1k tbr, 1k tbn, 1k tbc
Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp, 63 kb/s
Unsupported codec with id 0 for input stream 0
</stream></server>I am not, by any means, an expert in H264 or video encoding. I know that the encoded frames that come out from MediaCodec contain SPS NAL, PPS NAL, and frame NAL units. I’ve also written the MediaCodec output into a file and was able to play it back (I did have to specify the format and framerate as otherwise it would play too fast).
My assumption is that things should work (see how little I know :)). Knowing that SPS and PPS are written out, decoder should know enough. Yet, ffprobe fails to recognize codec, fps, and other video information. Do I need to pass packet flag information to FFmpegFrameRecorder.java:record() function ? Or should I use direct buffer ? Any suggestion will be appreciated ! I should figure things out with a hint.
PS : I know that some codecs use Planar and other SemiPlanar color formats. That distinction will come later if I get past this. Also, I didn’t go the Surface to MediaCodec way because I need to support API 17 and it requires more changes than this route, which I think helps me understand the more basic flow. Agan, I appreciate any suggestions. Please let me know if something needs to be clarified.
Update #1
So having done more testing, I see that my encoder outputs the following frames :
000000016742800DDA0507E806D0A1350000000168CE06E2
0000000165B840A6F1E7F0EA24000AE73BEB5F51CC7000233A84240...
0000000141E2031364E387FD4F9BB3D67F51CC7000279B9F9CFE811...
0000000141E40304423FFFFF0B7867F89FAFFFFFFFFFFCBE8EF25E6...
0000000141E602899A3512EF8AEAD1379F0650CC3F905131504F839...
...The very first frame contains SPS and PPS. From what I was able to see, these are transmitted only once. The rest are NAL types 1 and 5. So, my assumption is that, for ffprobe to see stream info not only when the stream starts, I should capture SPS and PPS frames and re-transmit them myself periodically, after a certain number of frames, or perhaps before every I-frame. What do you think ?
Update #2
Unable to validate that I’m writing frames successfully. After having tried to read back the written packet, I cannot validate written bytes. As strange, on successful write of IPL image and streaming, I also cannot print out bytes of encoded packet after
avcodec_encode_video2
. Hit the official dead end. -
Xbox Sphinx Protocol
I’ve gone down the rabbit hole of trying to read the Xbox DVD drive from Linux. Honestly, I’m trying to remember why I even care at this point. Perhaps it’s just my metagame of trying to understand how games and related technologies operate. In my last post of the matter, I determined that it is possible to hook an Xbox drive up to a PC using a standard 40-pin IDE interface and read data sectors. However, I learned that just because the Xbox optical drive is reading an Xbox disc, that doesn’t mean it’s just going to read the sectors in response to a host request.
Oh goodness, no. The drive is going to make the host work for those sectors.
To help understand the concept of locked/unlocked sectors on an Xbox disc, I offer this simplistic diagram :
Any DVD drive (including the Xbox drive) is free to read those first 6992 sectors (about 14 MB of data) which just contain a short DVD video asking the user to insert the disc into a proper Xbox console. Reading the remaining sectors involves performing a sequence of SCSI commands that I have taken to calling the “Sphinx Protocol” for reasons I will explain later in this post.
References
Doing a little Googling after my last post on the matter produced this site hosting deep, technical Xbox information. It even has a page about exactly what I am trying to achieve : Use an Xbox DVD Drive in Your PC. The page provides a tool named dvdunlocker written by “The Specialist” to perform the necessary unlocking. The archive includes a compiled Windows binary as well as its source code. The source code is written in Delphi Pascal and leverages Windows SCSI APIs. Still, it is well commented and provides a roadmap, which I will try to describe in this post.Sphinx Protocol
Here is a rough flowchart of the steps that are (probably) involved in the unlocking of those remaining sectors. I reverse engineered this based on the Pascal tool described in the previous section. Disclaimer : at the time of this writing, I haven’t tested all of the steps due to some Linux kernel problems, described later.
Concerning the challenge/response table that the drive sends back, it’s large (0×664 / 1636 bytes), and not all of the bytes’ meanings are known. However, these are the bytes that seem to be necessary (all multi-byte numbers are big endian) :
bytes 0-1 Size of mode page payload data (should be 0x0662) bytes 2-771 Unknown byte 772 Should be 1 byte 773 Number of entries in challenge/response table bytes 774-1026 Encrypted challenge/response table bytes 1027-1186 Unknown bytes 1187-1230 Key basis (44 bytes) bytes 1231-1635 Unknown
The challenge/response table is the interesting part, but it’s encrypted with RC4 a.k.a. ARCFOUR. The key is derived from the 44 bytes I have labeled “key basis”– cryptographic literature probably has a better term for it ; chime in if you know what that might be. An SHA-1 hash is computed over the 44 bytes.
The resulting SHA-1 hash — the first part of it, to be exact — is fed as the key into the RC4 decryption. The output of SHA-1 contains 160 bits of information. 160 / 8 = 20 bytes of information. To express this as a printable hex digest requires 40 characters. The SHA-1 hash is converted to a hex digest and then the first 7 of the characters are fed into the RC4 initialization function as the key. Then, the RC4 decrypter does its work on the 253 bytes of the challenge/response table.
So that’s why I took to calling this the “Sphinx Protocol” — I felt like I was being challenged with a bizarre riddle. Perhaps that describes a lot of cryptosystems, though You have to admit it sounds kind of cool.
The challenge/response table contains 23 11-byte records. The format of this table is (again, multi-byte numbers are big-endian) :
byte 0 This is 1 if this challenge/response pair is valid byte 1 Challenge ID bytes 2-5 Challenge byte 6 Response ID bytes 7-10 Response
Example
It’s useful to note that the challenge/response table and associated key is different for every disc (at least all the ones I have looked at). So this might be data that comes from the disc, since the values will always be the same for a given disc.Let’s examine Official Xbox Magazine disc #16 (Indiana Jones and The Emperor’s Tomb) :
Before I decrypt the challenge/response table, it looks like this :
0 : 180, 172 : 0xEB100059 ; 66 : 0xD56AFB56 1 : 34, 71 : 0x8F9BF03A ; 192 : 0xC32CBDF8 2 : 226, 216 : 0xA29B77F2 ; 12 : 0x4474A6F1 3 : 72, 122 : 0x9F5ABF33 ; 255 : 0xC5E3C304 4 : 1, 103 : 0x76142ADA ; 233 : 0xDE145D42 **** 5 : 49, 193 : 0xA1CD6192 ; 189 : 0x2169DBA5 6 : 182, 250 : 0x9977894F ; 96 : 0x5A929E2B 7 : 148, 71 : 0x6DD10A54 ; 115 : 0xF0BDAC4F 8 : 12, 45 : 0x5D5EB6FD ; 148 : 0x84E60A00 9 : 99, 121 : 0xFEAED372 ; 201 : 0xDA9986F9 10 : 172, 230 : 0xE6C0D0B4 ; 214 : 0x9050C250 11 : 84, 65 : 0x95CB8775 ; 104 : 0x550886C6 12 : 210, 65 : 0x1ED23619 ; 171 : 0x6DF4A35B 13 : 2, 155 : 0xD0AAE1E0 ; 130 : 0x00D1FFCF 14 : 40, 2 : 0x172EFEB8 ; 159 : 0x37E03E50 15 : 49, 15 : 0x43E5E378 ; 223 : 0x267F9C9A 16 : 240, 173 : 0x357D5D1C ; 250 : 0x24965D67 17 : 80, 184 : 0x5E7AF1A3 ; 81 : 0x3A8F69A7 18 : 154, 186 : 0x6626BEAC ; 245 : 0xE639540A 19 : 231, 249 : 0xFABAAFB7 ; 227 : 0x4C686A07 20 : 150, 186 : 0x9A6D7AA3 ; 133 : 0x25971CF0 21 : 236, 192 : 0x5CD97DD4 ; 247 : 0x26655EFB 22 : 68, 173 : 0xE2D372E4 ; 207 : 0x103FBF94 there are 1 valid pairs in the list : 4
My best clue that it’s not right is that there is only 1 valid entry (denoted by my tool using ****). The source I reverse engineered for this data indicates that there needs to be at least 2 valid pairs. After running the RC4 decryption on the table, it looks like this and I get far more valid pairs :
0 : 1, 174 : 0xBD628255 ; 0 : 0x9F0A31AF **** 1 : 2, 176 : 0x3151B341 ; 2 : 0x9C87C180 2 : 3, 105 : 0x018879E5 ; 1 : 0xFF068B5C 3 : 2, 7 : 0x1F316AAF ; 3 : 0xF420D3ED 4 : 3, 73 : 0xC2EBFBE9 ; 0 : 0x17062B5B 5 : 252, 163 : 0xFF14B5CB ; 236 : 0xAF813FBC 6 : 2, 233 : 0x5EE95C49 ; 1 : 0x37AA5511 7 : 1, 126 : 0xBD628255 ; 0 : 0x5BA3FBD4 **** 8 : 3, 4 : 0xB68BFEE6 ; 3 : 0xA8F3B918 9 : 3, 32 : 0xEA614943 ; 2 : 0xA678D715 10 : 2, 248 : 0x1BDD374E ; 0 : 0x8D2AC2C7 11 : 3, 17 : 0x0EABCE81 ; 2 : 0xC90A7242 12 : 1, 186 : 0xBD628255 ; 0 : 0xC4820242 **** 13 : 3, 145 : 0xB178F942 ; 3 : 0x4D78AD62 14 : 3, 37 : 0x4A6CE5E2 ; 2 : 0xBF94E1C6 15 : 1, 102 : 0xBD628255 ; 0 : 0xFFB83D8D **** 16 : 3, 122 : 0xF97B0905 ; 1 : 0x38533125 17 : 3, 197 : 0x57A6865D ; 2 : 0xA61D31EF 18 : 3, 27 : 0xC7227D7C ; 2 : 0xA3F9BA1E 19 : 1, 16 : 0xBD628255 ; 0 : 0x8557CCC8 **** 20 : 2, 53 : 0x1DA9D156 ; 3 : 0xC9051754 21 : 2, 90 : 0x3CD66BEE ; 3 : 0xFD851D3E 22 : 1, 252 : 0xBD628255 ; 0 : 0xB3F22701 **** there are 6 valid pairs in the list : 0 7 12 15 19 22
So, hopefully, I have the decryption correct.
Also of note is that you only get one chance to get this unlocking correct– fail, and the drive won’t return a valid DVD structure block again. You will either need to reboot the Xbox or eject & close the tray before you get to try again.
Problems Making It Work In Linux
There are a couple of ways to play with SCSI protocols under Linux. In more recent kernels, block devices are named /dev/sda, /dev/sdb, etc. Each of these block devices has a corresponding character device named /dev/sg0, /dev/sg1, etc. ‘sg’ stands for SCSI generic. This character devices can be opened as readable and/or writable and SCSI commands can be freely written with write() and data retrieved with read(). Pretty powerful.Except that the one machine I still possess which supports 40-pin IDE/ATAPI devices is running Linux kernel 2.6.24 which dates back to early 2008 and it still enumerates the IDE block devices as /dev/hda, /dev/hdb, etc. There are no corresponding /dev/sgX character devices. What to do ? It seems that a program can still issue SCSI commands using an ioctl() facility named SG_IO.
I was able to make the SG_IO ioctl() work for the most part (except for the discovery that the Xbox drive doesn’t respond to a basic SCSI Inquiry command). However, I ran into a serious limitation– a program can only open a /dev/hdX block device in read-only mode if the device corresponds to a read-only drive like, for example, a DVD-ROM drive. This means that a program can’t issue SCSI mode select commands to the drive, which counts as writing. This means that my tool can’t unlock the drive.
Current Status
So this is where my experiment is blocked right now. I have been trying to compile various Linux kernels to remedy the situation. But I always seem to find myself stuck in one of 2 situations, depending on the configuration options I choose : Either the drives are enumerated with the /dev/hdX convention and I am stuck in read-only mode (with no mode select) ; or the drives are enumerated with /dev/sdX along with corresponding /dev/sgN character devices, in which case the kernel does not recognize the Xbox DVD-ROM drive.This makes me wonder if there’s a discrepancy between the legacy ATA/ATAPI drivers (which sees the drive) and the newer SATA/PATA subsystem (which doesn’t see the drive). I also wonder about hacking the kernel logic to allow SCSI mode select logic to proceed to the device for a read-only file handle.