avio_reading简单使用

本文提供了一个使用FFmpeg库通过自定义AVIOContext读取媒体文件内容的示例程序。该程序展示了如何将文件内容加载到缓冲区,并通过回调函数供libavformat解复用器访问。
/*
* Copyright (c) 2011 Reinhard Tartler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")
/**
* @file
* libavformat AVIOContext API example.
*
* Make libavformat demuxer access media content through a custom
* AVIOContext read callback.
* @example avio_reading.c
*/
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/file.h>
}
struct buffer_data {
       uint8_t *ptr;
       size_t size; ///< size left in the buffer
};
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
       struct buffer_data *bd = (struct buffer_data *)opaque;
       buf_size = FFMIN(buf_size, bd->size);
       printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
       /* copy internal buffer data to buf */
       memcpy(buf, bd->ptr, buf_size);
       bd->ptr += buf_size;
       bd->size -= buf_size;
       return buf_size;
}
int main(int argc, char *argv[])
{
       AVFormatContext *fmt_ctx = NULL;
       AVIOContext *avio_ctx = NULL;
       uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
       size_t buffer_size, avio_ctx_buffer_size = 4096;
       char *input_filename = "E:/video_source/1.mp4";
       int ret = 0;
       struct buffer_data bd = { 0 };
       /*
       if (argc != 2) {
              fprintf(stderr, "usage: %s input_file\n"
                     "API example program to show how to read from a custom buffer "
                     "accessed through AVIOContext.\n", argv[0]);
              return 1;
       }*/
       
       /* register codecs and formats and other lavf/lavc components*/
       av_register_all();
       /* slurp file content into buffer */
       ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL);
       if (ret < 0)
              goto end;
       /* fill opaque structure used by the AVIOContext read callback */
       bd.ptr = buffer;
       bd.size = buffer_size;
       if (!(fmt_ctx = avformat_alloc_context())) {
              ret = AVERROR(ENOMEM);
              goto end;
       }
       avio_ctx_buffer = (uint8_t*)av_malloc(avio_ctx_buffer_size);
       if (!avio_ctx_buffer) {
              ret = AVERROR(ENOMEM);
              goto end;
       }
       avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size,
              0, &bd, &read_packet, NULL, NULL);
       if (!avio_ctx) {
              ret = AVERROR(ENOMEM);
              goto end;
       }
       fmt_ctx->pb = avio_ctx;
       ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL);
       if (ret < 0) {
              fprintf(stderr, "Could not open input\n");
              goto end;
       }
       ret = avformat_find_stream_info(fmt_ctx, NULL);
       if (ret < 0) {
              fprintf(stderr, "Could not find stream information\n");
              goto end;
       }
       av_dump_format(fmt_ctx, 0, input_filename, 0);
end:
       avformat_close_input(&fmt_ctx);
       /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
       if (avio_ctx) {
              av_freep(&avio_ctx->buffer);
              av_freep(&avio_ctx);
       }
       av_file_unmap(buffer, buffer_size);
       if (ret < 0) {
              fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
              return 1;
       }
       while (true)
       {
       }
       return 0;
}

运行结果:



`avio_open` 是 FFmpeg 中的一个函数,用于打开输入或输出的多媒体文件。 下面是 `avio_open` 的函数原型: ``` int avio_open(AVIOContext **s, const char *url, int flags); ``` 其中,`s` 是指向 `AVIOContext` 指针的指针,`url` 是输入/输出文件的 URL,`flags` 是打开文件的标志位。 使用 `avio_open` 函数打开文件的步骤如下: 1. 调用 `avio_alloc_context` 函数创建 `AVIOContext` 结构体对象。 ```c AVIOContext *avio_alloc_context( unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int (*read_packet)(void *opaque, uint8_t *buf, int buf_size), int (*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t (*seek)(void *opaque, int64_t offset, int whence) ); ``` 其中,`buffer_size` 是缓冲区大小,`read_packet` 是读取数据的回调函数,`write_packet` 是写入数据的回调函数,`seek` 是文件偏移量的回调函数,`opaque` 是用户自定义的参数,可以在回调函数中使用。 2. 调用 `avio_open` 函数打开文件。 ```c int ret = avio_open(&s, url, flags); if (ret < 0) { // 打开文件失败 return ret; } ``` 其中,`ret` 是返回值,小于 0 表示打开文件失败,大于等于 0 表示打开文件成功,返回的值是文件句柄。 3. 使用完毕后,调用 `avio_close` 函数关闭文件。 ```c avio_close(s); ``` 完整的使用示例代码如下: ```c #include <libavformat/avformat.h> int main(int argc, char *argv[]) { AVIOContext *s = NULL; const char *url = "input.mp4"; int flags = AVIO_FLAG_READ; // 创建 AVIOContext 对象 s = avio_alloc_context(NULL, 0, 0, NULL, NULL, NULL, NULL); if (!s) { printf("Failed to allocate AVIOContext\n"); return -1; } // 打开文件 int ret = avio_open(&s, url, flags); if (ret < 0) { printf("Failed to open file: %s\n", av_err2str(ret)); return -1; } // 使用 AVIOContext 对象读写文件 // 关闭文件 avio_close(s); return 0; } ``` 注意事项: 1. `AVIOContext` 结构体对象需要手动释放,使用 `avio_context_free` 函数。 2. 在使用完毕后,需要调用 `avio_close` 函数关闭文件。 3. 在使用 `avio_open` 函数打开文件前,需要先调用 `av_register_all` 函数注册 FFmpeg 中的所有组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱技术爱生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值