我正在尝试实现ffmpeg的自定义读取函数,该函数将从本地视频(将来从设备)检索缓冲区,然后解码此缓冲区等。
这是我的read函数
int IORead(void *opaque, uint8_t *buf, int buf_size)
{
FileReader* datrec = (FileReader*)opaque;
int ret = datrec->Read(buf, buf_size);
return ret;
}
对于FileReader:
class FileReader {
protected:
int fd;
public:
FileReader(const char *filename){ //, int buf_size){
fd = open(filename, O_RDONLY);
};
~FileReader() {
close(fd);
};
int Read(uint8_t *buf, int buf_size){
int len = read(fd, buf, buf_size);
return len;
};
};
和my执行:
FileReader *receiver = new FileReader("/sdcard/clip.ts");
AVFormatContext *avFormatContextPtr = NULL;
this->iobuffer = (unsigned char*) av_malloc(4096 + FF_INPUT_BUFFER_PADDING_SIZE);
avFormatContextPtr = avformat_alloc_context();
avFormatContextPtr->pb = avio_alloc_context(this->iobuffer, 4096, 0, receiver, IORead, NULL, NULL);
avFormatContextPtr->pb->seekable = 0;
int err = avformat_open_input(&avFormatContextPtr, "", NULL, NULL) ;
if( err != 0)
{...}
// Decoding process
{...}
然而,一旦avformat_open_input()
被调用,读取函数IORead
被调用,并继续读取文件clip.ts
,直到它到达它的结束,只有当它退出,解码过程到达没有数据解码(因为所有的数据都被消耗了)
我不知道是什么问题,特别是这段代码
AVFormatContext *avFormatContextPtr = NULL;
int err = avformat_open_input(&avFormatContextPtr, "/sdcard/clip.ts", NULL, NULL) ;
在到达文件末尾之前不会阻塞。
我错过了什么吗?谢谢你的帮助。
很可能avformat无法确定流的类型。你应该使用像
这样的东西avFormatContextPtr->iformat = av_find_input_format("mpegts");