ffmpeg_custom_context.cpp : Defines the entry point for the console application.//#include "stdafx.h" extern "C" {#include <libavformat/avformat.h >}; #pragma comment (lib, "Avformat.lib") Int readfunc (Void* ptr, uint8_t* buf, int buf_size) {file* fp = (file*) ptr;size_t size = fread (buf, 1, &NBSP;BUF_SIZE,&NBSP;FP); Int ret = size;return ret;} //This function you are free to play. Must be free to play. I did not have the network stream to read from the file, generally speaking here can not return -1 or 0, otherwise ffmpeg thought ended, or failed. You don't want to read the file, just copy a piece of data from the memory to BUF. Accept the network flow, the bottom of the capture card on the stream can be here,,,//must pay attention to this function, ffmpeg implementation is a pit, accidentally on the recruit. Int64_t seek_func (void *opaque, int64_t offset, int whence) {int64_t ret; file *fp = (file*) opaque;if (whence == avseek_size) {return -1;} Fseek (fp, offset, whence); Return ftell (FP);} Int _tmain (Int aRgc, _tchar* argv[]) {Av_register_all ();int ret = 0; File* fp = fopen ("./boygirls.mp4", "RB"); int nbuffersize = 1024 * 1024;unsigned char* pBuffer = new unsigned char[nBufferSize]; avformatcontext* pformatctx = null; avinputformat *pifmt = null; avcodeccontext *vcodecctx = null, *acodecctx = null; avcodec *vdec = null, *adec = null;int ai = -1, vi = -1;// allocate the aviocontext:aviocontext* pioctx = avio_alloc_context ( PBUFFER,&NBSP;NBUFFERSIZE,0,FP,READFUNC,0,SEEK_FUNC);//STEP2: Probe stream format ret = av_probe_input_ Buffer (pioctx, &pifmt, "", null, 0, 0);if (ret < 0) { fprintf (stderr, "probe failed!\n"); goto quit;} else {fprintf (stdout, "probe success!\n");fprintf (stdout, "format: %s[%s]\n", pifmt->name, pifmt->long_name);} allocate the avformatcontext:pformatctx = avformat_alloc_context ();// Set the iocontext:pformatctx->pb = pioctx;pformatctx->flags = avfmt_flag_ CUSTOM_IO;//STEP4: Open stream if (avformat_open_input (&pFormatCtx, "", pifmt, null) < 0) {fprintf (stderr, "avformat open failed.\n"); goto quit;} else {fprintf (stdout, "open stream success!\n");} if (Avformat_find_stream_info (pformatctx, null) <0) {printf ("av_find_stream_info error \n "); goto quit;} What should be done next??? Hey, what are you going to do? Quit:avformat_close_input (&PFORMATCTX);d elete[]pbuffer;return 0;}
There was already an article: http://hi.baidu.com/mingyuejingque/item/34db89a7d16fc9706cd4559d
But the new beggar Prince said that the layout is not easy to read, especially in the above URL of the reply I have posted the code, the wretched Baidu default did not open replies,,, so new post this article convenient for everyone. Thank the Motherland!
FFmpeg custom data sources, can be files, can be memory, can be network, love