AVIO内存输入模式

AVIO内存输入模式


目录

  1. 介绍
  2. 代码示例

1. 介绍

  1. AVIO内存输入模式是将内存区作输入输出,当启用内存IO模式后(即ifmt_ctx->pb有效时),将会忽略avformat_open_input()第二个参数url的值。
  2. 而对于其他一些场合,当有效音视频数据位于内存,而这片内存并无一个URL属性可用时,则只能使用内存IO模式来取得输入数据。

2. 代码示例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libavutil/frame.h>
#include <libavutil/mem.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

#define BUF_SIZE 20480

static char *av_get_err(int errnum) {
    static char err_buf[128] = {0};
    av_strerror(errnum, err_buf, 128);
    return err_buf;
}

static void decode(AVCodecContext *dec_ctx, AVPacket *packet, AVFrame *frame, FILE *outfile) {
    int ret = 0;
    ret = avcodec_send_packet(dec_ctx, packet);
    if (ret == AVERROR(EAGAIN)) {
        printf("Receive_frame and send_packet both returned EAGAIN, which is an API violation.\n");
    } else if (ret < 0) {
        printf("Error submitting the packet to the decoder, err:%s\n",
               av_get_err(ret));
        return;
    }

    while (ret >= 0) {
        ret = avcodec_receive_frame(dec_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
            return;
        } else if (ret < 0) {
            printf("Error during decoding\n");
            exit(1);
        }
        if (!packet) {
            printf("get flush frame\n");
        }
        int data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
        /**
           P表示Planar(平面),其数据格式排列方式为 :
           LLLLLLRRRRRRLLLLLLRRRRRRLLLLLLRRRRRRL...(每个LLLLLLRRRRRR为一个音频帧)
           而不带P的数据格式(即交错排列)排列方式为:
           LRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRL...(每个LR为一个音频样本)
        播放范例:   ffplay -ar 48000 -ac 2 -f f32le believe.pcm
            并不是每一种都是这样的格式
        */
        // 这里的写法不是通用,通用要调用重采样的函数去实现
        // 这里只是针对解码出来是planar格式的转换
        for (int i = 0; i < frame->nb_samples; i++) {
            for (int ch = 0; ch < dec_ctx->channels; ch++) {
                fwrite(frame->data[ch] + data_size * i, 1, data_size, outfile);
            }
        }
    }
}


static int read_packet(void *opaque, uint8_t *buf, int buf_size) {
    FILE *in_file = (FILE *) opaque;
    int read_size = fread(buf, 1, buf_size, in_file);
    printf("read_packet read_size:%d, buf_size:%d\n", read_size, buf_size);
    if (read_size <= 0) {
        return AVERROR_EOF;     // 数据读取完毕
    }
    return read_size;
}


int main(int argc, char **argv) {

    if(argc != 3) {
        printf("usage: %s <intput file> <out file>\n", argv[0]);
        return -1;
    }
    const char *in_file_name = argv[1];
    const char *out_file_name = argv[2];

//    const char *in_file_name = "/Users/lijinwang/Downloads/course/study/believe.aac";
//    const char *out_file_name = "/Users/lijinwang/Downloads/course/study/believe4.pcm";
    FILE *in_file = NULL;
    FILE *out_file = NULL;

    //1. 打开参数文件
    in_file = fopen(in_file_name, "rb");
    if (!in_file) {
        printf("open file %s failed\n", in_file_name);
        return -1;
    }
    out_file = fopen(out_file_name, "wb");
    if (!out_file) {
        printf("open file %s failed\n", out_file_name);
        return -1;
    }

    uint8_t *io_buffer = av_malloc(BUF_SIZE);
    AVIOContext *avio_ctx = (io_buffer, BUF_SIZE, 0, (void *) in_file, read_packet, NULL, NULL);
    AVFormatContext *format_ctx = avformat_alloc_context();
    format_ctx->pb = avio_ctx;
    int ret = avformat_open_input(&format_ctx, NULL, NULL, NULL);
    if (ret < 0) {
        printf("avformat_open_input failed:%s\n", av_err2str(ret));
        return -1;
    }

    //编码器查找
    AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
    if (!codec) {
        printf("avcodec_find_decoder failed\n");
        return -1;
    }

    AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
    if (!codec_ctx) {
        printf("avcodec_alloc_context3 failed\n");
        return -1;
    }

    ret = avcodec_open2(codec_ctx, codec, NULL);
    if (ret < 0) {
        printf("avcodec_open2 failed:%s\n", av_err2str(ret));
        return -1;
    }

    AVPacket *packet = av_packet_alloc();
    AVFrame *frame = av_frame_alloc();

    while (1) {
        ret = av_read_frame(format_ctx, packet);
        if (ret < 0) {
            printf("av_read_frame failed:%s\n", av_err2str(ret));
            break;
        }
        decode(codec_ctx, packet, frame, out_file);
    }
    printf("read file finish\n");
    decode(codec_ctx,packet,frame,out_file);

    fclose(in_file);
    fclose(out_file);

    av_free(io_buffer);
    av_frame_free(frame);
    av_packet_free(packet);

    avformat_close_input(&format_ctx);
    avcodec_free_context(&codec_ctx);

    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值