Skip to content

Conversation

@cho-m
Copy link
Contributor

@cho-m cho-m commented Aug 27, 2025

See FFmpeg/FFmpeg@0d48da2 for avcodec_close() removal in FFmpeg 8.0

The deprecation comment for avcodec_close() said:

Do not use this function. Use avcodec_free_context() to destroy a
codec context (either open or closed). Opening and closing a codec context
multiple times is not supported anymore -- use multiple codec contexts
instead.

avcodec_free_context() has been around since FFmpeg 2.3 (lavc 55.52.0, FFmpeg/FFmpeg@fd05602). Seems old enough for most versions of FFmpeg is active use but can add conditional if older versions are needed.

Documentation at https://ffmpeg.org/doxygen/2.3/group__lavc__core.html#gaf869d0829ed607cec3a4a02a1c7026b3 says:

Free the codec context and everything associated with it and write NULL to the provided pointer.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 27, 2025

Walkthrough

Replaced explicit codec close and manual nulling with a single FFmpeg API call: avcodec_free_context(&m_codec_ctx) in FFmpegAudioReader::Close(). No other logic or public signatures were changed. (48 words)

Changes

Cohort / File(s) Summary of Changes
FFmpeg context cleanup
src/audio/ffmpeg_audio_reader.h
In Close(), replaced avcodec_close(m_codec_ctx); m_codec_ctx = nullptr; with avcodec_free_context(&m_codec_ctx); so the codec context is freed and the pointer nulled by the FFmpeg API.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 375aae2 and c0870e3.

📒 Files selected for processing (1)
  • src/audio/ffmpeg_audio_reader.h (1 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (7)
src/audio/ffmpeg_audio_reader.h (7)

221-234: Close() leaves m_opened and runtime state stale; also keeps converter/buffer alive.

After Close(), IsOpen() can still return true, and buffers persist across re-opens. Reset state and release converter/buffer here.

 inline void FFmpegAudioReader::Close() {
   av_frame_free(&m_frame);
   av_packet_free(&m_packet);

   m_stream_index = -1;

-  if (m_codec_ctx) {
-    avcodec_free_context(&m_codec_ctx);
-  }
+  avcodec_free_context(&m_codec_ctx);

   if (m_format_ctx) {
     avformat_close_input(&m_format_ctx);
   }
+
+  // Reset runtime state
+  m_opened = false;
+  m_has_more_packets = false;
+  m_has_more_frames = false;
+  m_error.clear();
+  m_error_code = 0;
+  m_nb_packets = 0;
+  m_decode_error = 0;
+
+  // Release converter and its buffer
+  m_converter.reset();
+  av_freep(&m_convert_buffer[0]);
+  m_convert_buffer_nb_samples = 0;
 }

176-176: Remove or guard av_dump_format; hardcoded "foo" is noisy.

Either drop the call or compile-gate it and print the real filename.

-  av_dump_format(m_format_ctx, 0, "foo", 0);
+#if defined(CHROMAPRINT_DEBUG)
+  av_dump_format(m_format_ctx, 0, file_name.c_str(), 0);
+#endif

148-148: Fix typo in error message.

-    SetError("Coud not find stream information in the file", ret);
+    SetError("Could not find stream information in the file", ret);

268-274: Flush decoder at EOF to avoid dropping trailing frames.

On AVERROR_EOF from av_read_frame, send a NULL packet to trigger decoder drain.

-        if (ret < 0) {
+        if (ret < 0) {
           if (ret == AVERROR_EOF) {
             needs_packet = false;
             m_has_more_packets = false;
+            // Flush decoder to drain delayed frames
+            (void)avcodec_send_packet(m_codec_ctx, nullptr);
             break;
           }

Please verify with a file that has codec delay (e.g., AAC/MP3) to ensure the last frames are delivered.


114-117: Use snprintf instead of sprintf to avoid potential overflow.

-  char buf[64];
-  sprintf(buf, "%d", sample_rate);
+  char buf[64];
+  snprintf(buf, sizeof(buf), "%d", sample_rate);

120-128: ch_layout string for arbitrary N channels: prefer "%dc" and snprintf.

"3 channels" may not parse; "%dc" (e.g., "3c") is the generic unknown layout form. Also switch to snprintf.

-  char buf[64];
-    if (channels == 1)
-        sprintf(buf, "%s", "mono");
-    else if (channels == 2)
-        sprintf(buf, "%s", "stereo");
-    else
-        sprintf(buf, "%d channels", channels);
-    return av_dict_set(&m_input_opts, "ch_layout", buf, 0) >= 0;
+  char buf[64];
+  if (channels == 1) {
+    snprintf(buf, sizeof(buf), "%s", "mono");
+  } else if (channels == 2) {
+    snprintf(buf, sizeof(buf), "%s", "stereo");
+  } else {
+    snprintf(buf, sizeof(buf), "%dc", channels);
+  }
+  return av_dict_set(&m_input_opts, "ch_layout", buf, 0) >= 0;

Confirm your targeted FFmpeg versions accept the "%dc" form for ch_layout.


317-321: Nit: rename linsize → linesize for clarity.

-          int linsize;
+          int linesize;
-          ret = av_samples_alloc(m_convert_buffer, &linsize, m_codec_ctx->ch_layout.nb_channels, m_convert_buffer_nb_samples, AV_SAMPLE_FMT_S16, 1);
+          ret = av_samples_alloc(m_convert_buffer, &linesize, m_codec_ctx->ch_layout.nb_channels, m_convert_buffer_nb_samples, AV_SAMPLE_FMT_S16, 1);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between ac31acc and 375aae2.

📒 Files selected for processing (1)
  • src/audio/ffmpeg_audio_reader.h (1 hunks)
🔇 Additional comments (1)
src/audio/ffmpeg_audio_reader.h (1)

227-229: FFmpeg 8 migration: correct switch to avcodec_free_context.

Good replacement; it closes, frees, and NULLs the context and fixes the FFmpeg 8.0 removal of avcodec_close().

@lalinsky
Copy link
Member

Thank you!

@lalinsky lalinsky merged commit 122328a into acoustid:master Aug 28, 2025
14 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants