
Recherche avancée
Autres articles (79)
-
Personnaliser en ajoutant son logo, sa bannière ou son image de fond
5 septembre 2013, parCertains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;
-
Ecrire une actualité
21 juin 2013, parPrésentez les changements dans votre MédiaSPIP ou les actualités de vos projets sur votre MédiaSPIP grâce à la rubrique actualités.
Dans le thème par défaut spipeo de MédiaSPIP, les actualités sont affichées en bas de la page principale sous les éditoriaux.
Vous pouvez personnaliser le formulaire de création d’une actualité.
Formulaire de création d’une actualité Dans le cas d’un document de type actualité, les champs proposés par défaut sont : Date de publication ( personnaliser la date de publication ) (...) -
Publier sur MédiaSpip
13 juin 2013Puis-je poster des contenus à partir d’une tablette Ipad ?
Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir
Sur d’autres sites (7089)
-
Decoding and resampling audio with FFmpeg for output with libao
13 mai 2020, par DoctorSelarI'm trying to write a program to read and play an audio file using FFmpeg and libao. I've been following the procedure outlined in the FFmpeg documentation for decoding audio using the new
avcodec_send_packet
andavcodec_receive_frame
functions, but the examples I've been able to find are few and far between (the ones in the FFmpeg documentation either don't use libavformat or use the deprecatedavcodec_decode_audio4
). I've based a lot of my program off of the transcode_aac.c example (up toinit_resampler
) in the FFmpeg documentation, but that also uses the deprecated decoding function.


I believe I have the decoding part of the program working, but I need to resample the audio in order to convert it into an interleaved format to send to libao, for which I'm attempting to use libswresample. Whenever the program is run in its current state, it outputs (many times) "Error resampling : Output changed". The test file I've been using is just a YouTube rip that I had on hand. ffprobe reports the only stream as :



Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)




This is my first program with FFmpeg (and I'm still relatively new to C), so any advice on how to improve/fix other parts of the program would be welcome.



#include
#include<libavcodec></libavcodec>avcodec.h>
#include<libavformat></libavformat>avformat.h>
#include<libavutil></libavutil>avutil.h>
#include<libswresample></libswresample>swresample.h>
#include<ao></ao>ao.h>

#define OUTPUT_CHANNELS 2
#define OUTPUT_RATE 44100
#define BUFFER_SIZE 192000
#define OUTPUT_BITS 16
#define OUTPUT_FMT AV_SAMPLE_FMT_S16

static char *errtext (int err) {
 static char errbuff[256];
 av_strerror(err,errbuff,sizeof(errbuff));
 return errbuff;
}

static int open_audio_file (const char *filename, AVFormatContext **context, AVCodecContext **codec_context) {
 AVCodecContext *avctx;
 AVCodec *codec;
 int ret;
 int stream_id;
 int i;

 // Open input file
 if ((ret = avformat_open_input(context,filename,NULL,NULL)) < 0) {
 fprintf(stderr,"Error opening input file '%s': %s\n",filename,errtext(ret));
 *context = NULL;
 return ret;
 }

 // Get stream info
 if ((ret = avformat_find_stream_info(*context,NULL)) < 0) {
 fprintf(stderr,"Unable to find stream info: %s\n",errtext(ret));
 avformat_close_input(context);
 return ret;
 }

 // Find the best stream
 if ((stream_id = av_find_best_stream(*context,AVMEDIA_TYPE_AUDIO,-1,-1,&codec,0)) < 0) {
 fprintf(stderr,"Unable to find valid audio stream: %s\n",errtext(stream_id));
 avformat_close_input(context);
 return stream_id;
 }

 // Allocate a decoding context
 if (!(avctx = avcodec_alloc_context3(codec))) {
 fprintf(stderr,"Unable to allocate decoder context\n");
 avformat_close_input(context);
 return AVERROR(ENOMEM);
 }

 // Initialize stream parameters
 if ((ret = avcodec_parameters_to_context(avctx,(*context)->streams[stream_id]->codecpar)) < 0) {
 fprintf(stderr,"Unable to get stream parameters: %s\n",errtext(ret));
 avformat_close_input(context);
 avcodec_free_context(&avctx);
 return ret;
 }

 // Open the decoder
 if ((ret = avcodec_open2(avctx,codec,NULL)) < 0) {
 fprintf(stderr,"Could not open codec: %s\n",errtext(ret));
 avformat_close_input(context);
 avcodec_free_context(&avctx);
 return ret;
 }

 *codec_context = avctx;
 return 0;
}

static void init_packet (AVPacket *packet) {
 av_init_packet(packet);
 packet->data = NULL;
 packet->size = 0;
}

static int init_resampler (AVCodecContext *codec_context, SwrContext **resample_context) {
 int ret;

 // Set resampler options
 *resample_context = swr_alloc_set_opts(NULL,
 av_get_default_channel_layout(OUTPUT_CHANNELS),
 OUTPUT_FMT,
 codec_context->sample_rate,
 av_get_default_channel_layout(codec_context->channels),
 codec_context->sample_fmt,
 codec_context->sample_rate,
 0,NULL);
 if (!(*resample_context)) {
 fprintf(stderr,"Unable to allocate resampler context\n");
 return AVERROR(ENOMEM);
 }

 // Open the resampler
 if ((ret = swr_init(*resample_context)) < 0) {
 fprintf(stderr,"Unable to open resampler context: %s\n",errtext(ret));
 swr_free(resample_context);
 return ret;
 }

 return 0;
}

static int init_frame (AVFrame **frame) {
 if (!(*frame = av_frame_alloc())) {
 fprintf(stderr,"Could not allocate frame\n");
 return AVERROR(ENOMEM);
 }
 return 0;
}

int main (int argc, char *argv[]) {
 AVFormatContext *context = 0;
 AVCodecContext *codec_context;
 SwrContext *resample_context = NULL;
 AVPacket packet;
 AVFrame *frame = 0;
 AVFrame *resampled = 0;
 int16_t *buffer;
 int ret, packet_ret, finished;

 ao_device *device;
 ao_sample_format format;
 int default_driver;

 if (argc != 2) {
 fprintf(stderr,"Usage: %s <filename>\n",argv[0]);
 return 1;
 }

 av_register_all();
 printf("Opening file...\n");
 if (open_audio_file(argv[1],&context,&codec_context) < 0)
 return 1;

 printf("Initializing resampler...\n");
 if (init_resampler(codec_context,&resample_context) < 0) {
 avformat_close_input(&context);
 avcodec_free_context(&codec_context);
 return 1;
 }

 // Setup libao
 printf("Starting audio device...\n");
 ao_initialize();
 default_driver = ao_default_driver_id();
 format.bits = OUTPUT_BITS;
 format.channels = OUTPUT_CHANNELS;
 format.rate = codec_context->sample_rate;
 format.byte_format = AO_FMT_NATIVE;
 format.matrix = 0;
 if ((device = ao_open_live(default_driver,&format,NULL)) == NULL) {
 fprintf(stderr,"Error opening audio device\n");
 avformat_close_input(&context);
 avcodec_free_context(&codec_context);
 swr_free(&resample_context);
 return 1;
 }

 // Mainloop
 printf("Beginning mainloop...\n");
 init_packet(&packet);
 // Read packets until done
 while (1) {
 packet_ret = av_read_frame(context,&packet);
 // Send a packet
 if ((ret = avcodec_send_packet(codec_context,&packet)) < 0)
 fprintf(stderr,"Error sending packet to decoder: %s\n",errtext(ret));

 av_packet_unref(&packet);

 while (1) {
 if (!frame)
 frame = av_frame_alloc();

 ret = avcodec_receive_frame(codec_context,frame);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) // Need more input
 break;
 else if (ret < 0) {
 fprintf(stderr,"Error receiving frame: %s\n",errtext(ret));
 break;
 }
 // We have a valid frame, need to resample it
 if (!resampled)
 resampled = av_frame_alloc();

 resampled->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
 resampled->sample_rate = codec_context->sample_rate;
 resampled->format = OUTPUT_FMT;

 if ((ret = swr_convert_frame(resample_context,resampled,frame)) < 0) {
 fprintf(stderr,"Error resampling: %s\n",errtext(ret));
 } else {
 ao_play(device,(char*)resampled->extended_data[0],resampled->linesize[0]);
 }
 av_frame_unref(resampled);
 av_frame_unref(frame);
 }

 if (packet_ret == AVERROR_EOF)
 break;
 }

 printf("Closing file and freeing contexts...\n");
 avformat_close_input(&context);
 avcodec_free_context(&codec_context);
 swr_free(&resample_context);

 printf("Closing audio device...\n");
 ao_close(device);
 ao_shutdown();

 return 0;
}
</filename>



UPDATE : I've got it playing sound now, but it sounds like samples are missing (and MP3 files warn that "Could not update timestamps for skipped samples"). The issue was that the
resampled
frame needed to have certain attributes set before being passed toswr_convert_frame
. I've also addedav_packet_unref
andav_frame_unref
, but I'm still unsure as to where to best locate them.

-
FFMPEG RTSP stream to MPEG4/H264 file using libx264
16 octobre 2020, par PhiHeyo folks,



I'm attempting to transcode/remux an RTSP stream in H264 format into a MPEG4 container, containing just the H264 video stream. Basically, webcam output into a MP4 container.



I can get a poorly coded MP4 produced, using this code :



// Variables here for demo
AVFormatContext * video_file_output_format = nullptr;
AVFormatContext * rtsp_format_context = nullptr;
AVCodecContext * video_file_codec_context = nullptr;
AVCodecContext * rtsp_vidstream_codec_context = nullptr;
AVPacket packet = {0};
AVStream * video_file_stream = nullptr;
AVCodec * rtsp_decoder_codec = nullptr;
int errorNum = 0, video_stream_index = 0;
std::string outputMP4file = "D:\\somemp4file.mp4";

// begin
AVDictionary * opts = nullptr;
av_dict_set(&opts, "rtsp_transport", "tcp", 0);

if ((errorNum = avformat_open_input(&rtsp_format_context, uriANSI.c_str(), NULL, &opts)) < 0) {
 errOut << "Connection failed: avformat_open_input failed with error " << errorNum << ":\r\n" << ErrorRead(errorNum);
 TacticalAbort();
 return;
}

rtsp_format_context->max_analyze_duration = 50000;
if ((errorNum = avformat_find_stream_info(rtsp_format_context, NULL)) < 0) {
 errOut << "Connection failed: avformat_find_stream_info failed with error " << errorNum << ":\r\n" << ErrorRead(errorNum);
 TacticalAbort();
 return;
}

video_stream_index = errorNum = av_find_best_stream(rtsp_format_context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);

if (video_stream_index < 0) {
 errOut << "Connection in unexpected state; made a connection, but there was no video stream.\r\n"
 "Attempts to find a video stream resulted in error " << errorNum << ": " << ErrorRead(errorNum);
 TacticalAbort();
 return;
}

rtsp_vidstream_codec_context = rtsp_format_context->streams[video_stream_index]->codec;

av_init_packet(&packet);

if (!(video_file_output_format = av_guess_format(NULL, outputMP4file.c_str(), NULL))) {
 TacticalAbort();
 throw std::exception("av_guess_format");
}

if (!(rtsp_decoder_codec = avcodec_find_decoder(rtsp_vidstream_codec_context->codec_id))) {
 errOut << "Connection failed: connected, but avcodec_find_decoder returned null.\r\n"
 "Couldn't find codec with an AV_CODEC_ID value of " << rtsp_vidstream_codec_context->codec_id << ".";
 TacticalAbort();
 return;
}

video_file_format_context = avformat_alloc_context();
video_file_format_context->oformat = video_file_output_format;

if (strcpy_s(video_file_format_context->filename, sizeof(video_file_format_context->filename), outputMP4file.c_str())) {
 errOut << "Couldn't open video file: strcpy_s failed with error " << errno << ".";
 std::string log = errOut.str();
 TacticalAbort();
 throw std::exception("strcpy_s");
}

if (!(video_file_encoder_codec = avcodec_find_encoder(video_file_output_format->video_codec))) {
 TacticalAbort();
 throw std::exception("avcodec_find_encoder");
}

// MARKER ONE

if (!outputMP4file.empty() &&
 !(video_file_output_format->flags & AVFMT_NOFILE) &&
 (errorNum = avio_open2(&video_file_format_context->pb, outputMP4file.c_str(), AVIO_FLAG_WRITE, nullptr, &opts)) < 0) {
 errOut << "Couldn't open video file \"" << outputMP4file << "\" for writing : avio_open2 failed with error " << errorNum << ": " << ErrorRead(errorNum);
 TacticalAbort();
 return;
}

// Create stream in MP4 file
if (!(video_file_stream = avformat_new_stream(video_file_format_context, video_file_encoder_codec))) {
 TacticalAbort();
 return;
}

AVCodecContext * video_file_codec_context = video_file_stream->codec;

// MARKER TWO

// error -22/-21 in avio_open2 if this is skipped
if ((errorNum = avcodec_copy_context(video_file_codec_context, rtsp_vidstream_codec_context)) != 0) {
 TacticalAbort();
 throw std::exception("avcodec_copy_context");
}

//video_file_codec_context->codec_tag = 0;

/*
// MARKER 3 - is this not needed? Examples suggest not.
if ((errorNum = avcodec_open2(video_file_codec_context, video_file_encoder_codec, &opts)) < 0)
{
 errOut << "Couldn't open video file codec context: avcodec_open2 failed with error " << errorNum << ": " << ErrorRead(errorNum);
 std::string log = errOut.str();
 TacticalAbort();
 throw std::exception("avcodec_open2, video file");
}*/

//video_file_format_context->flags |= AVFMT_FLAG_GENPTS;
if (video_file_format_context->oformat->flags & AVFMT_GLOBALHEADER)
{
 video_file_codec_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
}

if ((errorNum = avformat_write_header(video_file_format_context, &opts)) < 0) {
 errOut << "Couldn't open video file: avformat_write_header failed with error " << errorNum << ":\r\n" << ErrorRead(errorNum);
 std::string log = errOut.str();
 TacticalAbort();
 return;
}




However, there are several issues :



- 

- I can't pass any x264 options to the output file. The output H264 matches the input H264's profile/level - switching cameras to a different model switches H264 level.
- The timing of the output file is off, noticeably.
- The duration of the output file is off, massively. A few seconds of footage becomes hours, although playtime doesn't match. (FWIW, I'm using VLC to play them.)









Passing x264 options



If I manually increment PTS per packet, and set DTS equal to PTS, it plays too fast, 2-3 seconds' worth of footage in one second playtime, and duration is hours long. The footage also blurs past several seconds, about 10 seconds' footage in a second.



If I let FFMPEG decide (with or without GENPTS flag), the file has a variable frame rate (probably as expected), but it plays the whole file in an instant and has a long duration too (over forty hours for a few seconds). The duration isn't "real", as the file plays in an instant.



At Marker One, I try to set the profile by passing options to
avio_open2
. The options are simply ignored by libx264. I've tried :


av_dict_set(&opts, "vprofile", "main", 0);
av_dict_set(&opts, "profile", "main", 0); // error, missing '('
// FF_PROFILE_H264_MAIN equals 77, so I also tried
av_dict_set(&opts, "vprofile", "77", 0); 
av_dict_set(&opts, "profile", "77", 0);




It does seem to read the profile setting, but it doesn't use them. At Marker Two, I tried to set it after the
avio_open2
, beforeavformat_write_header
.


// I tried all 4 av_dict_set from earlier, passing it to avformat_write_header.
// None had any effect, they weren't consumed.
av_opt_set(video_file_codec_context, "profile", "77", 0);
av_opt_set(video_file_codec_context, "profile", "main", 0);
video_file_codec_context->profile = FF_PROFILE_H264_MAIN;
av_opt_set(video_file_codec_context->priv_data, "profile", "77", 0);
av_opt_set(video_file_codec_context->priv_data, "profile", "main", 0);




Messing with privdata made the program unstable, but I was trying anything at that point.
I'd like to solve issue 1 with passing settings, since I imagine it'd bottleneck any attempt to solve issues 2 or 3.



I've been fiddling with this for the better part of a month now. I've been through dozens of documentation, Q&As, examples. It doesn't help that quite a few are outdated.



Any help would be appreciated.



Cheers


-
dca_parser : Extend DTS core sync word and fix existing check
29 avril 2015, par foo86dca_parser : Extend DTS core sync word and fix existing check
The previous version checked for 14-bit streams and did not properly
work across buffer boundaries.Use the 64-bit parser state to make extended sync word detection work
across buffer boundary and check the extended sync word for 16-bit LE
and BE core streams to reduce probability of alias sync detection.Signed-off-by : Michael Niedermayer <michaelni@gmx.at>
Signed-off-by : Luca Barbato <lu_zero@gentoo.org>