<div dir="ltr">Hi, <div>thanks for answer.</div><div><br><div>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.</div><div><br></div><div>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?</div></div><div><br></div><div>This is how I read frames and decode them:</div><div><br></div><div><div>static void encodeSoundNext(JNIEnv * env, jobject this) {</div><div><br></div><div><span class="" style="white-space:pre">      </span>if (input_context == NULL)</div><div><span class="" style="white-space:pre">         </span>return;</div><div><br></div><div><span class="" style="white-space:pre">   </span>int samples_size;</div><div><br></div><div><span class="" style="white-space:pre">         </span>frameRead = 0;</div><div><span class="" style="white-space:pre">             </span>char index = 0;</div><div><br></div><div><span class="" style="white-space:pre">           </span>AVFrame *decoded_frame = NULL;</div><div><br></div><div><span class="" style="white-space:pre">            </span>int input_audio_stream_index = get_stream_index(input_context, AVMEDIA_TYPE_AUDIO);</div><div><br></div><div><span class="" style="white-space:pre">               </span>while (frameRead >= 0) {</div><div><br></div><div><span class="" style="white-space:pre">                       </span>AVPacket in_packet;</div><div><br></div><div><span class="" style="white-space:pre">                       </span>index++;</div><div><br></div><div><span class="" style="white-space:pre">                  </span>frameRead = av_read_frame(input_context, &in_packet);</div><div><span class="" style="white-space:pre">  </span></div><div><span class="" style="white-space:pre">                   </span>if (frameRead < 0) {</div><div><span class="" style="white-space:pre">                    </span></div><div><span class="" style="white-space:pre">                           </span>trackCompressionFinished = 1;</div><div><span class="" style="white-space:pre">                              </span>avformat_close_input(&input_context);</div><div><br></div><div><span class="" style="white-space:pre">                 </span>} else {</div><div><br></div><div><span class="" style="white-space:pre">                          </span>if (decoded_frame == NULL) {</div><div><span class="" style="white-space:pre">                                       </span>if (!(decoded_frame = avcodec_alloc_frame())) {</div><div><span class="" style="white-space:pre">                                            </span>LOGE("out of memory");</div><div><span class="" style="white-space:pre">                                           </span>exit(1);</div><div><span class="" style="white-space:pre">                                   </span>}</div><div><span class="" style="white-space:pre">                          </span>} else {</div><div><span class="" style="white-space:pre">                                   </span>avcodec_get_frame_defaults(decoded_frame);</div><div><span class="" style="white-space:pre">                         </span>}</div><div><span class="" style="white-space:pre">                          </span></div><div><span class="" style="white-space:pre">                           </span>int got_frame_ptr;</div><div><span class="" style="white-space:pre">                         </span>samplesBytes = avcodec_decode_audio4(in_audio_st->codec,</div><div><span class="" style="white-space:pre">                                                </span>decoded_frame, &got_frame_ptr, &in_packet);</div><div><span class="" style="white-space:pre">                                </span>if (samplesBytes < 0) {</div><div><span class="" style="white-space:pre">                                 </span>LOGE("Error occurred during decoding.");</div><div><span class="" style="white-space:pre">                                 </span>exit(1);</div><div><span class="" style="white-space:pre">                                   </span>break;</div><div><span class="" style="white-space:pre">                             </span>}</div><div><br></div><div><span class="" style="white-space:pre">                         </span>write_audio_frame(oc, audio_st, decoded_frame);</div><div><span class="" style="white-space:pre">                            </span>av_free_packet(&in_packet);</div><div><br></div><div><span class="" style="white-space:pre">                   </span>}</div><div><span class="" style="white-space:pre">          </span>}</div><div><br></div><div><span class="" style="white-space:pre">         </span>if (decoded_frame != NULL) {</div><div><span class="" style="white-space:pre">                       </span>av_free(decoded_frame);</div><div><span class="" style="white-space:pre">                    </span>decoded_frame = NULL;</div><div><span class="" style="white-space:pre">              </span>}</div><div><span class="" style="white-space:pre">          </span></div><div>}</div></div><div><br></div><div><br></div><div>This is how I am encoding sound to AAC:</div><div><br></div><div><br></div><div><div>static void write_audio_frame(AVFormatContext *oc, AVStream *st,</div><div><span class="" style="white-space:pre">           </span>const AVFrame *frame_to_encode) {</div><div><span class="" style="white-space:pre">          </span></div><div><span class="" style="white-space:pre">   </span>AVCodecContext *c;</div><div><span class="" style="white-space:pre"> </span>AVPacket pkt;</div><div><span class="" style="white-space:pre">      </span>int got_packet_ptr = 0;</div><div><br></div><div><span class="" style="white-space:pre">   </span>av_init_packet(&pkt);</div><div><span class="" style="white-space:pre">  </span>c = st->codec;</div><div><span class="" style="white-space:pre">  </span>pkt.size = 0;</div><div><span class="" style="white-space:pre">      </span>pkt.data = NULL;</div><div><span class="" style="white-space:pre">   </span>int ret = avcodec_encode_audio2(c, &pkt, frame_to_encode, &got_packet_ptr);</div><div><span class="" style="white-space:pre">        </span>if (ret < 0) {</div><div><span class="" style="white-space:pre">          </span>exit(1);</div><div><span class="" style="white-space:pre">   </span>}</div><div><span class="" style="white-space:pre">  </span></div><div><span class="" style="white-space:pre">   </span>if (got_packet_ptr == 1) {</div><div><span class="" style="white-space:pre">         </span>if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE) {</div><div><span class="" style="white-space:pre">                    </span>pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base,</div><div><span class="" style="white-space:pre">                                 </span>st->time_base);</div><div><span class="" style="white-space:pre">         </span>}</div><div><span class="" style="white-space:pre">          </span>pkt.flags |= AV_PKT_FLAG_KEY;</div><div><span class="" style="white-space:pre">              </span>pkt.stream_index = st->index;</div><div><span class="" style="white-space:pre">           </span>// write the compressed frame in the media file</div><div><span class="" style="white-space:pre">            </span>if (av_interleaved_write_frame(oc, &pkt) != 0) {</div><div><span class="" style="white-space:pre">                       </span>LOGE("Error while writing audio frame.");</div><div><span class="" style="white-space:pre">                        </span>exit(1);</div><div><span class="" style="white-space:pre">           </span>}</div><div><span class="" style="white-space:pre">  </span>}</div><div><span class="" style="white-space:pre">  </span>av_free_packet(&pkt);</div><div><span class="" style="white-space:pre">  </span></div><div>}</div></div><div><br></div><div><br></div><div>Audio stream is added to video file in this way:</div><div><br></div><div><br></div><div><div>static AVStream *add_audio_stream(AVFormatContext *oc, enum AVCodecID codec_id) {</div><div><br></div><div><span class="" style="white-space:pre">        </span>AVCodecContext *c;</div><div><span class="" style="white-space:pre"> </span>AVStream *st;</div><div><br></div><div><span class="" style="white-space:pre">     </span>st = avformat_new_stream(oc, NULL);</div><div><br></div><div><span class="" style="white-space:pre">       </span>c = st->codec;</div><div><span class="" style="white-space:pre">  </span>if (!st) {</div><div><span class="" style="white-space:pre">         </span>LOGE("Could not alloc stream.");</div><div><span class="" style="white-space:pre">         </span>return NULL;</div><div><span class="" style="white-space:pre">       </span>}</div><div><br></div><div><span class="" style="white-space:pre"> </span>// AAC is expirimental in FFMPEG2.1</div><div><span class="" style="white-space:pre">        </span>c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;</div><div><br></div><div><span class="" style="white-space:pre"> </span>c->codec_id = codec_id;</div><div><span class="" style="white-space:pre"> </span>c->codec_type = AVMEDIA_TYPE_AUDIO;</div><div><span class="" style="white-space:pre">     </span>c->bit_rate = 23600; // bitrate of the compressed sound (must be higher for stereo)</div><div><br></div><div><span class="" style="white-space:pre">    </span>c->sample_rate = 16000;</div><div><span class="" style="white-space:pre"> </span>c->channels = 1;</div><div><span class="" style="white-space:pre">        </span>c->sample_fmt = AV_SAMPLE_FMT_FLT;</div><div><br></div><div><span class="" style="white-space:pre">     </span>if (oc->oformat->flags & AVFMT_GLOBALHEADER){</div><div><span class="" style="white-space:pre">            </span>c->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div><span class="" style="white-space:pre">   </span>}</div><div><br></div><div><span class="" style="white-space:pre"> </span>return st;</div><div>}</div></div><div><br></div><div>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.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 1 July 2015 at 20:57, Talgorn François-Xavier <span dir="ltr"><<a href="mailto:fxtalgorn-at-yahoo.fr@ffmpeg.org" target="_blank">fxtalgorn-at-yahoo.fr@ffmpeg.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi,<div><br></div><div>I don't know about AMR codec but bitrate definitely impacts on final quality.</div><div>Try to increase bitrate value: I had same poor quality problems with MPEG4 encoding until I set the bitrate to width * height * 4.</div><div>Keep in mind that poor quality might comes from a wide bunch of parameters used to initialize the codec.</div><div>As for example, this is how I initialize an MPEG4 codec (A]), for clarity, in_ctx is initialized via the code in (B])</div><div><br></div><div><div>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.</div><div><br></div><div>I hope this comments will help put you on the road to success :-)</div><div><br></div><div>Good luck.</div></div><div><br></div><div>A]</div><div><div>    //codec found, now we param it</div><div>    o_codec_ctx->codec_id=AV_CODEC_ID_MPEG4;</div><div>    o_codec_ctx->bit_rate=in_ctx->picture_width * in_ctx->picture_height * 4;</div><div>    o_codec_ctx->width=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->width;</div><div>    o_codec_ctx->height=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->height;</div><div>    o_codec_ctx->time_base = in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->time_base;</div><div>    o_codec_ctx->ticks_per_frame = in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->ticks_per_frame;</div><div>    o_codec_ctx->sample_aspect_ratio = in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->sample_aspect_ratio;</div><div>    o_codec_ctx->gop_size=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->gop_size;</div><div>    o_codec_ctx->pix_fmt=AV_PIX_FMT_YUV420P;</div></div><div><br></div><div><br></div><div><br></div><div>B]</div><div><div> // register all formats and codecs</div><div>    av_register_all();</div><div>    avcodec_register_all();</div><div><br></div><div>    // open input file, and allocate format context</div><div>    if (avformat_open_input(&in_fmt_ctx, filename, NULL, NULL) < 0)</div><div>    {</div><div>        fprintf(stderr, "Could not open source file %s\n", filename);</div><div>        exit(1);</div><div>    }</div><div><br></div><div>    // retrieve stream information </div><div>    if (avformat_find_stream_info(in_fmt_ctx, NULL) < 0)</div><div>    {</div><div>        fprintf(stderr, "Could not find stream information\n");</div><div>        exit(1);</div><div>    }</div><div><br></div><div>    if (open_codec_context(&video_stream_idx, in_fmt_ctx, AVMEDIA_TYPE_VIDEO, filename) >= 0)</div><div>    {</div><div>        video_stream = in_fmt_ctx->streams[video_stream_idx];</div><div>        video_dec_ctx = video_stream->codec;</div><div>    }</div><div><br></div><div>    if (open_codec_context(&audio_stream_idx, in_fmt_ctx, AVMEDIA_TYPE_AUDIO, filename) >= 0) {</div><div>        audio_stream = in_fmt_ctx->streams[audio_stream_idx];</div><div>        audio_dec_ctx = audio_stream->codec;</div><div>    }</div><div><br></div><div>    if (!video_stream) {</div><div>        fprintf(stderr, "Could not find video stream in the input, aborting\n");</div><div>        avformat_close_input(&in_fmt_ctx);</div><div>        exit(0);</div><div>    }</div><div><br></div><div>    in_video_ctx->format_ctx=in_fmt_ctx;</div><div>    in_video_ctx->filename=filename;</div><div>    in_video_ctx->codec_name=(char *) in_fmt_ctx->streams[video_stream_idx]->codec->codec->long_name;</div><div>    in_video_ctx->video_stream_idx=video_stream_idx;</div><div>    in_video_ctx->audio_stream_idx=audio_stream_idx;</div><div>    in_video_ctx->picture_width=in_fmt_ctx->streams[video_stream_idx]->codec->width;</div><div>    in_video_ctx->picture_height=in_fmt_ctx->streams[video_stream_idx]->codec->height;</div><div>    in_video_ctx->nb_streams=in_fmt_ctx->nb_streams;</div></div><div><br></div><div><br></div><div><br></div><div> </div><div><div><div><div class="h5"><div>Le 1 juil. 2015 à 10:40, adev dev <<a href="mailto:androiddevmar11@gmail.com" target="_blank">androiddevmar11@gmail.com</a>> a écrit :</div><br></div></div><blockquote type="cite"><div><div class="h5"><div dir="ltr"><span style="font-size:12.8000001907349px">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.</span><div><span style="font-size:12.8000001907349px"><br></span></div><div><span style="font-size:12.8000001907349px">My AMR files are recorded with parameters:</span><div><div><span style="font-size:12.8000001907349px">- sampling rate: 16000,</span></div><div><span style="font-size:12.8000001907349px">- bitrate: 23000.</span></div><div><span style="font-size:12.8000001907349px"><br></span></div><div><span style="font-size:12.8000001907349px">I am setting this parameters in audio stream which is added to video. Sample format is set to </span>AV_SAMPLE_FMT_FLT. When using other formats app crashes with "Unsupported sample format". </div><div><br></div><div>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.</div></div></div></div></div></div>
_______________________________________________<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></blockquote></div><br></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>