Different users have different purposes and different levels of detail. expand gradually. [A blog is only a record with poor technical skills. It is best to help visitors. I still hope that you will not give an axe to your mistakes or ambiguity. -- Preface]
1. About how to transcode [convert f4v (the same container as MP4/3GP) to FLV]
First of all, install the ffmpeg-0.11.1 will not talk about, the third-party library required for compilation also don't talk about, a search a lot, but I write these, may also be a lot, try to write different from others, and briefly. Transcoding code analysis: FFMPEG. in C, the main function is the framework, skipping the initialization work. (Note: Use GDB ffmpeg_g to debug FFMPEG.) Check the function parse_options first. See the/* parse options */section, note that this option array, static const optiondef options [], is used to parse the input command. For example, if you set the command-I **. Avi of the input file, it will enter the (FFMPEG. c) opt_input_file function. It is easy to see that this will analyze the media file, multiple input files are called multiple times, and the grow_array function is used to increase the array static inputfile ** input_files.
In Demux and other processes, FFMPEG uses av_dict_set to dictionary metadata information and print it out. This is useful for us to follow the code and pay attention to some parameters.
Then, let's look at the main transcoding function transcode. At first, we will go to the transcode_init function and skip the annotation. When calculating the encoding parameters for each output stream, the loop for (I = 0; in I <nb_output_streams; I ++), the parameter Ost-> stream_copy is divided into two branches, which are related to the parameter-acodec copy/-vcodec copy. After confirming the encoding parameters, enable the encoder, initialize the input stream, and discard the useless avprogram (in the structure, there are metadata members, and the conjecture is that they correspond to different streams and script information ), open the output file and write the file header. According to the notes, the functional modules in the file are clearly divided. Next, the dump of the format parameter. The dump_format mark will print a lot of information, the source and target. Here, the get_input_stream function is. If the input and output files correspond to each other, the index of the output stream can be used to directly obtain the corresponding input stream (Audio/Video/subtitle and so on) from input_stream ), in addition, multiple single-channel stream MUX is also used as a file, which should correspond to the filter (guess, not yet read ). Look at the avformat_write_header function. Many metadata information is written here, which is printed in av_dump_format before. Here we can check it. In this example, we will adjust the write_header of avoutputformat to enter the header information structure of the specific container. Then, let's look at the output_packet function in transcode, which skips the previous special timestamp calculation and EOF processing, and the two branches are introduced by ist-> decoding_needed. First, let's take a look at the second record to process the direct stream copy. It first repairs the timing Stamp (corresponding to a single packet in the media file), followed by the For Loop: check_output_constraints checks the time sequence (audio and video are sorted by timestamp ). Let's take a look at the do_streamcopy function: Data validity detection, number of packages of the stream, and pts dts duration_per_packet flags_of_key_frame ,. Check the write_frame function. video_sync_method has several modes (vsync_auto scaling using vsync_vfr vsync_drop), such as current (video_sync_method = vsync_auto, audio_sync_method = equals ). Skip. Look at the av_interleaved_write_frame function, which contains interleave_packet and write_packet. 2. There is a requirement that requires f4v (h264) --> FLV (VP6). It needs to be intercepted from the server and transcoded, and FFmpeg is very convenient. However, after reading the configure file, I found that FFMPEG does not support encoder of VP6. Only the libvpx option is supported, and Vp8 is basically supported, which cannot solve the problem. Decided to find the codec of open-source VP6 and integrate it. Below we will make some step-by-step records. 3. How to add a codec in FFMPEG? For example, encoder :( in progress. Summarize the notes .)
First, this is to expand the VP6 encoder. If no official source code is found, refer to the mencoder/mplayer, which contains an encapsulated dynamic library vp6vfw. DLL, which is easy to find. Note that the input stream encoded by it is RGB and there is a config. the H header file defines all the required macros and so on. You can copy them in a successfully compiled and installed mplayer. Pay attention to the corresponding version.
During the demo test, the simple makefile is as follows (including the files extracted from mplayer ):
# Region #---------------------------------------------------------------------------------------------------------#
# Headers
Vpath %. h libmp codecs \
Osdep \
Loader \
Loader/DMO \
Loader/dshow \
Loader/qtx \
Loader/qtx/qtxsdk \
Loader/wine
Vpath %. c \
Osdep \
Libavutil \
Libmp codecs \
Loader \
Loader/DMO \
Loader/dshow \
Loader/qtx
# Compilingoptions
Defs + =-d1_fallenink __
#-W ignore all warnings-display all warnings-werror warning as error handling
Flags + =-w-lpthread-lm-G
# Tools
Cc = gcc
# Defines
Objects = demo_vp6enc.o \
Vp6enc. O \
Ve_vfw.o \
VFL. O \
DRV. O \
Win32.o \
Module. O \
Ext. O \
Ldt_keeper.o \
Cpudetect. O \
Mmap_anon.o \
Resource. O \
Registry. O \
Avstring. O \
Path. O \
Pe_image.o \
Pe_resource.o \
Mem. o
Main: $ (objects)
$ (CC) $ (flags)-O main $ (objects)
$ (Objects): %. O: %. c
$ (CC) $ (defs) $ (flags)-C $ <-o $ @
. Phony: clean
Clean:
-Rm main $ (objects)
# Region #---------------------------------------------------------------------------------------------------------#
Then let's see what needs to be done in FFMPEG.
You can refer to the following:
Http://wiki.multimedia.cx/index.php? Title = on2_vp6
Http://blog.csdn.net/phenixzhong/article/details/7375838
Code:
In libavcodec/avcodec. H, add the corresponding enumerated value to the enum codecid type (the name should be consistent with the FFMPEG style as much as possible), for example, codec_id_vp6vfw = 0x0ffff.
Add the vp6vfw decoder registration in the avcodec_register_all () function of libavcodec/allcodecs. C, for example:
Register_encoder (vp6vfw, vp6vfw );
In libavformat/xxx. in the c file, for example, identify vp6vfw in the file format that can contain VP6 data, such as static const avcodectag flv_video_codec_ids []; also need to be added in flv_write_packet; (changes vary here)
Create the source file vp6vfwenc. C and implement the necessary functions, and define a global avcodec variable, for example:
Avcodecff_vp6vfw_encoder = {
. Name = "vp6vfw ",
. Type = avmedia_type_video,
. ID = codec_id_vp6vfw,
. Priv_data_size = 0, // sizeof (vp56context ),
. Init = _ born,
. Close = _ die,
. Decode = _ working,
. Capabilities = 0,
. Pix_fmts = (const Enum pixelformat []) {pix_fmt_rgb24, pix_fmt_none },
. Long_name = null_if_config_small ("on2 VP6 (fallenink version, implemented by vp6vfw. dll )"),
};
In FFMPEG. C, the static const optiondef options [] array "vcodec" corresponds to the opt_video_codec function.
Compilation:
Configure
Test_deps _ encoder _ decoder ????
Added: vp6vfw \
Encoder_list = $ (find_things encoder ENC libavcodec/allcodecs. c )?????
Echo "creating config. Mak and config. H ..."????
In makefile under libavcodec:
Add a row, for example, objs-$ (config_vp6vfw_encoder) + = vp6vfwenc. o
If there are subdirectories, refer to the makefile method of other subdirectories.
4.Structured record file/file protocol, Io, URL level [file format] [iO] [Protocol] file organization such as: flvenc. c | aviobuf. c | avio. c | rtmproto. c. This is just to help you see the relationship clearly. In addition, the multi-layer protocol stack is reused in avio. C, for example, typedef struct urlcontext {
Const avclass * av_class;/** <information for av_log (). Set by url_open ().*/
Struct urlprotocol * prot ;...... Int ffurl_read (urlcontext * H, unsigned char * Buf, int size)
{
If (! (H-> flags & avio_flag_read ))
Return averror (EIO );
Return retry_transfer_wrapper (H, Buf, size, 1, h-> prot-> url_read );
}...... 5. [minor problem] [today, I was not careful about libavutil/log. in C, I customized my own log output. In order to transmit parameters outside, I defined two global functions, such as create_log_file (char * URL). The result link failed. Due to the lack of some knowledge about Linux, I have been searching for a long time. The process is as follows: I started to waste a lot of effort to guess: Didn't I compile it? This is a nonsense guess. Is the function name modified? This is also a mess. Is it set to static? A little reliable. Let's take a look. First, we found the libavutil. V file. Content: [equivalent to the Linux export file] libavutil _ $ major {
Global: Av _ *; FF _ *; avutil _*;
Local :*;
}; Obviously, this means that, indeed, only the above three prefixes are set as global functions, and the others are local ones (they can be linked to each other in the current library, not the current file). Instead of looking at the LD linker, You can see when these files were used. After configure. mak file, you can see: shflags =-shared-wl,-soname, $ (@ f)-wl,-bsymbolic-wl, -- version-script, $ (subdir) lib $ (name ). ver. Next find the following section: http://blog.csdn.net/hh2000/article/details/3201716
GCC compiles the dynamic library. By default, all functions are exported.
Use version-script to export only the symbol table to be used
$ Gcc-O mylib. So -- version-script a. Map A. o B. O
{
Global: name of the exported function;
Local :*;
};
Use the -- version-script option to load your file during lD. After all, use readelf to observe the static and dynamic sections and find that only the specified function name is exported as a symbol.
1, only meaningful for elf platforms that support shared libraries
2, cocould improve reduce the IMG size.
3, cocould improve the speed of Load Library, if library havn't been prelinked.
Also can refer to: http://login.sina.com.cn/sso/login.php? Useticket = 0 & returntype = meta & service = Blog & gateway = 1 & url = http://blog.sina.com.cn/s/blog_493667730100csde.html
...... To be supplemented