【FFmpeg】自定义回调函数处理AVIOContext中的数据

1、简述

AVIOContext是FFmpeg管理输入输出数据的结构体,它的成员变量有指向数据的指针、大小以及处理数据的回调函数指针等等。如果使用avio_open或avio_open2来创建,它会根据指定的url协议,将协议处理数据的回调函数指针赋值给AVIOContext的相应成员变量。
我们也可以自己定义回调函数,来处理AVIOContext中数据。这就需要使用avio_alloc_context来创建AVIOContext,创建时将回调函数指针作为参数,传递给avio_alloc_context。
FFmpeg源码中有示例演示如何自定义回调函数,

2、示例中使用到的关键函数
2.1 av_file_map、av_file_unmap
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx);
void av_file_unmap(uint8_t *bufptr, size_t size);

av_file_map能够根据文件名filename打开文件,并将文件内容映射到内存bufptr中;
使用完bufptr需要用av_file_unmap来释放内存。

2.1 avformat_alloc_context、
AVFormatContext *avformat_alloc_context(void);

创建封装上下文:AVFormatContext

2.2 av_malloc、av_freep
void *av_malloc(size_t size);
void av_free(void *ptr);

创建、释放内存块,这个内存块在avio_alloc_context创建AVIOContext时,指定给AVIOContext,在以后处理数据时,用来缓存数据。
av_malloc在avio_alloc_context之前调用;
av_freep在avio_context_free之前调用。

2.3 avio_alloc_context、avio_context_free
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));
void avio_context_free(AVIOContext **s);

创建、释放AVIOContext,在调用avio_alloc_context前要用av_malloc来分配内部缓存数据的空间。可以将自定义的read_packet、write_packet、seek函数指针在这里传递给AVIOContext。

2.4 avformat_open_input、avformat_close_input
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);
void avformat_close_input(AVFormatContext **s);

avformat_open_input:打开AVFormatContext中的流,并读取头
avformat_close_input:关闭流,释放AVFormatContext。

2.5 avformat_find_stream_info
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

从媒体文件中读取几包数据,从这几包数据中,分析出流的信息。
注意,这里读的动作,不会改变文件中游标的位置。

2.6 av_dump_format
void av_dump_format(AVFormatContext *ic,
                    int index,
                    const char *url,
                    int is_output);

打印输入或输出的格式,如时长、比特率、元数据、编码类型、基础时间等。

3、FFmpeg示例执行结果
$ ./avio_reading ~/Videos/640-480p.mp4 
ptr:0x7f9baa23d000 size:37507102
...
ptr:0x7f9baa27c232 size:37248492
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/laoer/Videos/640-480p.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1iso6
    creation_time   : 2016-02-03T02:12:11.000000Z
    album           : Yinyuetai
    artist          : yinyuetai.com
    comment         : Yinyuetai-1TR1166
    date            : 02/03/16 10:12:11
  Duration: 00:04:31.53, start: 0.000000, bitrate: N/A
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 640x480, 1004 kb/s, 29 fps, 29 tbr, 29k tbn, 58 tbc (default)
    Metadata:
      creation_time   : 2016-02-03T02:12:11.000000Z
      handler_name    : [email protected]
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default)
    Metadata:
      creation_time   : 2016-02-03T02:10:54.000000Z
      handler_name    : Sound Media Handler
4、doc/examples/avio_reading.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);

    if (!buf_size)
        return AVERROR_EOF;
    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 = NULL;
    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;
    }
    input_filename = argv[1];

    /* 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 = 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);
    avio_context_free(&avio_ctx);

    av_file_unmap(buffer, buffer_size);

    if (ret < 0) {
        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
        return 1;
    }

    return 0;
}
发布了324 篇原创文章 · 获赞 266 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/u010168781/article/details/105344702