* Encode one frame worth of audio to the output file. * @param frame Samples to be encoded * @param output_format_context Format context of the output file * @param output_codec_context Codec context of the output file * @param[out] data_present Indicates whether data has been * encoded * @return Error code (0 if succ
| 653 | * @return Error code (0 if successful) |
| 654 | */ |
| 655 | static int encode_audio_frame(AVFrame *frame, |
| 656 | AVFormatContext *output_format_context, |
| 657 | AVCodecContext *output_codec_context, |
| 658 | int *data_present) |
| 659 | { |
| 660 | /* Packet used for temporary storage. */ |
| 661 | AVPacket *output_packet; |
| 662 | int error; |
| 663 | |
| 664 | error = init_packet(&output_packet); |
| 665 | if (error < 0) |
| 666 | return error; |
| 667 | |
| 668 | /* Set a timestamp based on the sample rate for the container. */ |
| 669 | if (frame) { |
| 670 | frame->pts = pts; |
| 671 | pts += frame->nb_samples; |
| 672 | } |
| 673 | |
| 674 | *data_present = 0; |
| 675 | /* Send the audio frame stored in the temporary packet to the encoder. |
| 676 | * The output audio stream encoder is used to do this. */ |
| 677 | error = avcodec_send_frame(output_codec_context, frame); |
| 678 | /* Check for errors, but proceed with fetching encoded samples if the |
| 679 | * encoder signals that it has nothing more to encode. */ |
| 680 | if (error < 0 && error != AVERROR_EOF) { |
| 681 | fprintf(stderr, "Could not send packet for encoding (error '%s')\n", |
| 682 | av_err2str(error)); |
| 683 | goto cleanup; |
| 684 | } |
| 685 | |
| 686 | /* Receive one encoded frame from the encoder. */ |
| 687 | error = avcodec_receive_packet(output_codec_context, output_packet); |
| 688 | /* If the encoder asks for more data to be able to provide an |
| 689 | * encoded frame, return indicating that no data is present. */ |
| 690 | if (error == AVERROR(EAGAIN)) { |
| 691 | error = 0; |
| 692 | goto cleanup; |
| 693 | /* If the last frame has been encoded, stop encoding. */ |
| 694 | } else if (error == AVERROR_EOF) { |
| 695 | error = 0; |
| 696 | goto cleanup; |
| 697 | } else if (error < 0) { |
| 698 | fprintf(stderr, "Could not encode frame (error '%s')\n", |
| 699 | av_err2str(error)); |
| 700 | goto cleanup; |
| 701 | /* Default case: Return encoded data. */ |
| 702 | } else { |
| 703 | *data_present = 1; |
| 704 | } |
| 705 | |
| 706 | /* Write one audio frame from the temporary packet to the output file. */ |
| 707 | if (*data_present && |
| 708 | (error = av_write_frame(output_format_context, output_packet)) < 0) { |
| 709 | fprintf(stderr, "Could not write frame (error '%s')\n", |
| 710 | av_err2str(error)); |
| 711 | goto cleanup; |
| 712 | } |
no test coverage detected