<div dir="ltr">I was not clear enough. Sound is not bad quality. It is damaged. Please have a look on video file which I uploaded to YouTube:<div><br></div><div><a href="https://www.youtube.com/watch?v=1UcGQwvtr9s">https://www.youtube.com/watch?v=1UcGQwvtr9s</a></div><div><br></div><div>Video length is 4 seconds. Adding this sound makes it longer to 17 seconds. Looks like some parameters are wrong. Yes, AMR is recorded in mono so sample format converting is not needed. Thanks for help.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 2 July 2015 at 10:14, Paul B Mahol <span dir="ltr"><<a href="mailto:onemda@gmail.com" target="_blank">onemda@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p><br>
Dana 2. 7. 2015. 07:58 osoba "adev dev" <<a href="mailto:androiddevmar11@gmail.com" target="_blank">androiddevmar11@gmail.com</a>> napisala je:</p><div><div class="h5"><br>
><br>
> Hi, <br>
> thanks for answer.<br>
><br>
> I cannot increase sound bitrate. I am using Android MediaRecorder and AMR codec for recording audio. AMR is needed because I am doing Chrome version where AAC codec is not working. This AMR codec at least in Android can only record with maximum bitrate 23600. It is not much but sound should be good. Now my result is that sound is totally crappy. There are strange pulses and if I record speech it is impossible to recognise words.<br>
><br>
> I wonder what else could be the problem. When I am adding AAC files to output video it is working correctly. Decoding AMR files and encoding them again to AAC is not working. For the first glance it looks that AMR decoding is not working correctly. Or the frame is in format (not planar) and this makes problem. What do you think?<br>
><br>
> This is how I read frames and decode them:<br>
><br>
> static void encodeSoundNext(JNIEnv * env, jobject this) {<br>
><br>
> if (input_context == NULL)<br>
> return;<br>
><br>
> int samples_size;<br>
><br>
> frameRead = 0;<br>
> char index = 0;<br>
><br>
> AVFrame *decoded_frame = NULL;<br>
><br>
> int input_audio_stream_index = get_stream_index(input_context, AVMEDIA_TYPE_AUDIO);<br>
><br>
> while (frameRead >= 0) {<br>
><br>
> AVPacket in_packet;<br>
><br>
> index++;<br>
><br>
> frameRead = av_read_frame(input_context, &in_packet);<br>
> if (frameRead < 0) {<br>
> trackCompressionFinished = 1;<br>
> avformat_close_input(&input_context);<br>
><br>
> } else {<br>
><br>
> if (decoded_frame == NULL) {<br>
> if (!(decoded_frame = avcodec_alloc_frame())) {<br>
> LOGE("out of memory");<br>
> exit(1);<br>
> }<br>
> } else {<br>
> avcodec_get_frame_defaults(decoded_frame);<br>
> }<br>
> int got_frame_ptr;<br>
> samplesBytes = avcodec_decode_audio4(in_audio_st->codec,<br>
> decoded_frame, &got_frame_ptr, &in_packet);<br>
> if (samplesBytes < 0) {<br>
> LOGE("Error occurred during decoding.");<br>
> exit(1);<br>
> break;<br>
> }<br>
><br>
> write_audio_frame(oc, audio_st, decoded_frame);<br>
> av_free_packet(&in_packet);<br>
><br>
> }<br>
> }<br>
><br>
> if (decoded_frame != NULL) {<br>
> av_free(decoded_frame);<br>
> decoded_frame = NULL;<br>
> }<br>
> }<br>
><br>
><br>
> This is how I am encoding sound to AAC:<br>
><br>
><br>
> static void write_audio_frame(AVFormatContext *oc, AVStream *st,<br>
> const AVFrame *frame_to_encode) {<br>
> AVCodecContext *c;<br>
> AVPacket pkt;<br>
> int got_packet_ptr = 0;<br>
><br>
> av_init_packet(&pkt);<br>
> c = st->codec;<br>
> pkt.size = 0;<br>
> pkt.data = NULL;<br>
> int ret = avcodec_encode_audio2(c, &pkt, frame_to_encode, &got_packet_ptr);<br>
> if (ret < 0) {<br>
> exit(1);<br>
> }<br>
> if (got_packet_ptr == 1) {<br>
> if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE) {<br>
> pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base,<br>
> st->time_base);<br>
> }<br>
> pkt.flags |= AV_PKT_FLAG_KEY;<br>
> pkt.stream_index = st->index;<br>
> // write the compressed frame in the media file<br>
> if (av_interleaved_write_frame(oc, &pkt) != 0) {<br>
> LOGE("Error while writing audio frame.");<br>
> exit(1);<br>
> }<br>
> }<br>
> av_free_packet(&pkt);<br>
> }<br>
><br>
><br>
> Audio stream is added to video file in this way:<br>
><br>
><br>
> static AVStream *add_audio_stream(AVFormatContext *oc, enum AVCodecID codec_id) {<br>
><br>
> AVCodecContext *c;<br>
> AVStream *st;<br>
><br>
> st = avformat_new_stream(oc, NULL);<br>
><br>
> c = st->codec;<br>
> if (!st) {<br>
> LOGE("Could not alloc stream.");<br>
> return NULL;<br>
> }<br>
><br>
> // AAC is expirimental in FFMPEG2.1<br>
> c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;<br>
><br>
> c->codec_id = codec_id;<br>
> c->codec_type = AVMEDIA_TYPE_AUDIO;<br>
> c->bit_rate = 23600; // bitrate of the compressed sound (must be higher for stereo)<br>
><br>
> c->sample_rate = 16000;<br>
> c->channels = 1;<br>
> c->sample_fmt = AV_SAMPLE_FMT_FLT;<br>
><br>
> if (oc->oformat->flags & AVFMT_GLOBALHEADER){<br>
> c->flags |= CODEC_FLAG_GLOBAL_HEADER;<br>
> }<br>
><br>
> return st;<br>
> }<br>
><br>
> What I noticed so far is that when I am decoding AAC files and encoding them again to audio stream in video files AAC frames has format AV_SAMPLE_FMT_FLTP. AMR frames are in AV_SAMPLE_FMT_FLT format. Do you think I have to convert some how from AV_SAMPLE_FMT_FLT to AV_SAMPLE_FMT_FLTP?? Thanks for all hints.<br>
></div></div><p></p>
<p>For mono, single channel, conversion is not needed. If recording is of bad quality encoding you can only use some other amr encoder.</p><div class="HOEnZb"><div class="h5">
<p>><br>
><br>
> On 1 July 2015 at 20:57, Talgorn François-Xavier <<a href="mailto:fxtalgorn-at-yahoo.fr@ffmpeg.org" target="_blank">fxtalgorn-at-yahoo.fr@ffmpeg.org</a>> wrote:<br>
>><br>
>> Hi,<br>
>><br>
>> I don't know about AMR codec but bitrate definitely impacts on final quality.<br>
>> Try to increase bitrate value: I had same poor quality problems with MPEG4 encoding until I set the bitrate to width * height * 4.<br>
>> Keep in mind that poor quality might comes from a wide bunch of parameters used to initialize the codec.<br>
>> As for example, this is how I initialize an MPEG4 codec (A]), for clarity, in_ctx is initialized via the code in (B])<br>
>><br>
>> Concerning the delay issue: I also faced such a problem. I solved it using av_packet_rescale_ts() which relies on time_base, instead of setting timestamps myself manually.<br>
>><br>
>> I hope this comments will help put you on the road to success :-)<br>
>><br>
>> Good luck.<br>
>><br>
>> A]<br>
>>     //codec found, now we param it<br>
>>     o_codec_ctx->codec_id=AV_CODEC_ID_MPEG4;<br>
>>     o_codec_ctx->bit_rate=in_ctx->picture_width * in_ctx->picture_height * 4;<br>
>>     o_codec_ctx->width=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->width;<br>
>>     o_codec_ctx->height=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->height;<br>
>>     o_codec_ctx->time_base = in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->time_base;<br>
>>     o_codec_ctx->ticks_per_frame = in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->ticks_per_frame;<br>
>>     o_codec_ctx->sample_aspect_ratio = in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->sample_aspect_ratio;<br>
>>     o_codec_ctx->gop_size=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->gop_size;<br>
>>     o_codec_ctx->pix_fmt=AV_PIX_FMT_YUV420P;<br>
>><br>
>><br>
>><br>
>> B]<br>
>>  // register all formats and codecs<br>
>>     av_register_all();<br>
>>     avcodec_register_all();<br>
>><br>
>>     // open input file, and allocate format context<br>
>>     if (avformat_open_input(&in_fmt_ctx, filename, NULL, NULL) < 0)<br>
>>     {<br>
>>         fprintf(stderr, "Could not open source file %s\n", filename);<br>
>>         exit(1);<br>
>>     }<br>
>><br>
>>     // retrieve stream information <br>
>>     if (avformat_find_stream_info(in_fmt_ctx, NULL) < 0)<br>
>>     {<br>
>>         fprintf(stderr, "Could not find stream information\n");<br>
>>         exit(1);<br>
>>     }<br>
>><br>
>>     if (open_codec_context(&video_stream_idx, in_fmt_ctx, AVMEDIA_TYPE_VIDEO, filename) >= 0)<br>
>>     {<br>
>>         video_stream = in_fmt_ctx->streams[video_stream_idx];<br>
>>         video_dec_ctx = video_stream->codec;<br>
>>     }<br>
>><br>
>>     if (open_codec_context(&audio_stream_idx, in_fmt_ctx, AVMEDIA_TYPE_AUDIO, filename) >= 0) {<br>
>>         audio_stream = in_fmt_ctx->streams[audio_stream_idx];<br>
>>         audio_dec_ctx = audio_stream->codec;<br>
>>     }<br>
>><br>
>>     if (!video_stream) {<br>
>>         fprintf(stderr, "Could not find video stream in the input, aborting\n");<br>
>>         avformat_close_input(&in_fmt_ctx);<br>
>>         exit(0);<br>
>>     }<br>
>><br>
>>     in_video_ctx->format_ctx=in_fmt_ctx;<br>
>>     in_video_ctx->filename=filename;<br>
>>     in_video_ctx->codec_name=(char *) in_fmt_ctx->streams[video_stream_idx]->codec->codec->long_name;<br>
>>     in_video_ctx->video_stream_idx=video_stream_idx;<br>
>>     in_video_ctx->audio_stream_idx=audio_stream_idx;<br>
>>     in_video_ctx->picture_width=in_fmt_ctx->streams[video_stream_idx]->codec->width;<br>
>>     in_video_ctx->picture_height=in_fmt_ctx->streams[video_stream_idx]->codec->height;<br>
>>     in_video_ctx->nb_streams=in_fmt_ctx->nb_streams;<br>
>><br>
>><br>
>><br>
>>  <br>
>> Le 1 juil. 2015 à 10:40, adev dev <<a href="mailto:androiddevmar11@gmail.com" target="_blank">androiddevmar11@gmail.com</a>> a écrit :<br>
>><br>
>>> I am compressing movies from bitmaps and audio files. With AAC files it is working correctly. But when I have AMR_WB files sound is corrupted. I can recognise correct words in video file but it is delayed and with very bad quality.<br>
>>><br>
>>> My AMR files are recorded with parameters:<br>
>>> - sampling rate: 16000,<br>
>>> - bitrate: 23000.<br>
>>><br>
>>> I am setting this parameters in audio stream which is added to video. Sample format is set to AV_SAMPLE_FMT_FLT. When using other formats app crashes with "Unsupported sample format". <br>
>>><br>
>>> What needs to be done to correctly add AMR stream to video file? Do I have to reencode it to AAC and add as AAC audio stream?? Thank you for all hints.<br>
>>> _______________________________________________<br>
>>> Libav-user mailing list<br>
>>> <a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
>>> <a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
>><br>
>><br>
>><br>
>> _______________________________________________<br>
>> Libav-user mailing list<br>
>> <a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
>> <a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
>><br>
><br>
><br>
> _______________________________________________<br>
> Libav-user mailing list<br>
> <a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
> <a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
><br>
</p>
</div></div><br>_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
<br></blockquote></div><br></div>