FFmpeg is like a black box when you compile FFmpeg to get the binary, and then you call the binary library. As a programmer, wouldn’t you like to explore the implementation of FFmpeg? For example, how to get the song information, how to decode, how to push the stream, and so on.

Looking at the source code is one way to understand the implementation of the code, and step debugging can help you in another dimension. This article shows you how to step through FFmpeg code.

(I) Why debug

The benefits of debugging are that you can understand the execution flow of the program and help locate problems.

Compared to looking at the source code, single step debugging, you can see the changes in the data, will be much more profound, and locate the problem is also intuitive and fast, you can even modify the value of variables or conditional breakpoints, give full play to the debugger function.

So debugging is an effective way to understand code and locate problems.

(2) How to debug

Cheng uses a MAC. First complete FFmpeg source download and compilation of the preparatory work.

(0) download FFmpeg source code

Git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg or:  curl -0 http://ffmpeg.org/releases/ffmpeg-\${VERSION}.tar.bz2 tar jxvf ffmpeg-\${VERSION}.tar.bz2 VERSION Set to the latest FFMPEG VERSION number.Copy the code

As for what tools to use to open these source files, you should consider tools that are appropriate for you and for large projects, such as SourceInsight, Sublime, Vim, Emacs, Xcode, Android Studio, etc.

(1) Compile FFmpeg

To compile using a compiler, the easiest way to install Xcode on a MAC is to ensure that the clang compiler is present.

For debugging purposes, this compilation can be as simple as removing the optimization option and then making:

make clean
./configure --disable-optimizations
make 
Copy the code

Configure is a configuration script provided by FFmpeg, used to generate makefiles and config.h files. Makefiles are used at compile time, and config.h is used by FFmpeg source code, which also affects FFmpeg feature editing.

For configure arguments, you can also view all the options with./configure –help, and then find the optimizations.

Make clean, if you haven’t compiled before, you don’t need to do it.

In addition, if make is followed by make Install, FFmpeg will be installed on the MAC system. This is not necessary for debugging, just make the binary library.

After a successful compilation, you can look at these files:

(2) debug FFmpeg with GDB

If you prefer the command line, using GDB or LLDB for debugging is a good option.

Before you can use GDB, you need to install and sign GDB. If you are not ready for GDB and are interested in using GDB, you can read the section below, which also contains GDB common commands.


Install the GDB:

brew install homebrew/dupes/gdb

Reference: blog.csdn.net/cairo123/ar in GDB authorization…

Write an example test:

touch gdbtest.c:
int main() {
	int a = 10;
	printf("%d\n");
}
Copy the code

Compile:

gcc -o gdbtest gdbtest.c -g

Note that -g is required to generate the symbol dSYM file.

Then you can debug. Common commands include:

GDB gdbtest -- load executable r --run, run, can take parameters I b --info break, breakpoint information b 3 --break 3, Line 3 breakpoint b main - the main function under the first line of the breakpoint other_c b: fun1 - file other_c fun1 function under the first line breakpoint b 120 - under 120 lines of breakpoint clear - delete all breakpoints 3 -- delete 3 d, Delete breakpoint 3 disable 1 -- disable breakpoint 1 enable 1 -- enable breakpoint 1 S --step, jump in F --finish, jump out N --next, execute line N 3 --next 3, execute line 3 C --continue, Continue until the next breakpoint or end p a --print a, print the value of variable A list/ L -- view the code q/kill -- exit the debug bt -- view the call stack return -- return the current functionCopy the code

For convenience, I directly use FFmpeg program to debug (of course, you can also write your own code to call FFmpeg), for example, you can choose ffplay_g this program to debug it depends on the basis of FFmpeg.

R “XXX /file.mp3” after GDB ffplay_g, indicates that ffplay_g is used to play the file.

Here’s a demo video of me using GDB for simple debugging:

Note that programs with _g are programs with debugging information, which is needed for debugging.

(3) use Xcode to debug FFmpeg

Obviously, debugging with GDB is not intuitive for reading code or manipulating it, so consider debugging FFmpeg with Xcode.

Don’t introduce each configuration link here, because, you can refer to the following this article illustrated, according to the step operation can: www.jianshu.com/p/226c19aa6…

So far, I’ve covered how to debug FFmpeg.

In summary, this article describes how to debug FFmpeg source code on MacOS, including using GDB or Xcode to debug methods, and also describes how to compile FFmpeg with debugging information. See you later.