【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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章