欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ffmpeg使用C语言sdk实现抽取视频中的视频数据

程序员文章站 2022-06-05 17:22:27
主要使用函数 特征码:Start code 解码的一些视频参数,分辨率和帧率:SPS/PPS ffmpeg获取SPS/PPS:codec extradata 实例 ......

主要使用函数

  • 特征码:start code

  • 解码的一些视频参数,分辨率和帧率:sps/pps

  • ffmpeg获取sps/pps:codec->extradata

实例

#include <stdio.h>
#include <libavutil/log.h>
#include <libavformat/avformat.h>


#ifndef av_wb32
#   define av_wb32(p, val) do {                 \
        uint32_t d = (val);                     \
        ((uint8_t*)(p))[3] = (d);               \
        ((uint8_t*)(p))[2] = (d)>>8;            \
        ((uint8_t*)(p))[1] = (d)>>16;           \
        ((uint8_t*)(p))[0] = (d)>>24;           \
    } while(0)
#endif

#ifndef av_rb16
#   define av_rb16(x)                           \
    ((((const uint8_t*)(x))[0] << 8) |          \
      ((const uint8_t*)(x))[1])
#endif

static int alloc_and_copy(avpacket *out,
                          const uint8_t *sps_pps, uint32_t sps_pps_size,
                          const uint8_t *in, uint32_t in_size)
{
    uint32_t offset         = out->size;
    uint8_t nal_header_size = offset ? 3 : 4;
    int err;

    err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);
    if (err < 0)
        return err;

    if (sps_pps)
        memcpy(out->data + offset, sps_pps, sps_pps_size);
    memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);
    if (!offset) {
        av_wb32(out->data + sps_pps_size, 1);
    } else {
        (out->data + offset + sps_pps_size)[0] =
        (out->data + offset + sps_pps_size)[1] = 0;
        (out->data + offset + sps_pps_size)[2] = 1;
    }

    return 0;
}

int h264_extradata_to_annexb(const uint8_t *codec_extradata, const int codec_extradata_size, avpacket *out_extradata, int padding)
{
    uint16_t unit_size;
    uint64_t total_size                 = 0;
    uint8_t *out                        = null, unit_nb, sps_done = 0,
             sps_seen                   = 0, pps_seen = 0, sps_offset = 0, pps_offset = 0;
    const uint8_t *extradata            = codec_extradata + 4;
    static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
    int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size, 用于指示表示编码数据长度所需字节数

    sps_offset = pps_offset = -1;

    /* retrieve sps and pps unit(s) */
    unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
    if (!unit_nb) {
        goto pps;
    }else {
        sps_offset = 0;
        sps_seen = 1;
    }

    while (unit_nb--) {
        int err;

        unit_size   = av_rb16(extradata);
        total_size += unit_size + 4;
        if (total_size > int_max - padding) {
            av_log(null, av_log_error,
                   "too big extradata size, corrupted stream or invalid mp4/avcc bitstream\n");
            av_free(out);
            return averror(einval);
        }
        if (extradata + 2 + unit_size > codec_extradata + codec_extradata_size) {
            av_log(null, av_log_error, "packet header is not contained in global extradata, "
                   "corrupted stream or invalid mp4/avcc bitstream\n");
            av_free(out);
            return averror(einval);
        }
        if ((err = av_reallocp(&out, total_size + padding)) < 0)
            return err;
        memcpy(out + total_size - unit_size - 4, nalu_header, 4);
        memcpy(out + total_size - unit_size, extradata + 2, unit_size);
        extradata += 2 + unit_size;
pps:
        if (!unit_nb && !sps_done++) {
            unit_nb = *extradata++; /* number of pps unit(s) */
            if (unit_nb) {
                pps_offset = total_size;
                pps_seen = 1;
            }
        }
    }

    if (out)
        memset(out + total_size, 0, padding);

    if (!sps_seen)
        av_log(null, av_log_warning,
               "warning: sps nalu missing or invalid. "
               "the resulting stream may not play.\n");

    if (!pps_seen)
        av_log(null, av_log_warning,
               "warning: pps nalu missing or invalid. "
               "the resulting stream may not play.\n");

    out_extradata->data      = out;
    out_extradata->size      = total_size;

    return length_size;
}

int h264_mp4toannexb(avformatcontext *fmt_ctx, avpacket *in, file *dst_fd)
{

    avpacket *out = null;
    avpacket spspps_pkt;

    int len;
    uint8_t unit_type;
    int32_t nal_size;
    uint32_t cumul_size    = 0;
    const uint8_t *buf;
    const uint8_t *buf_end;
    int            buf_size;
    int ret = 0, i;

    out = av_packet_alloc();

    buf      = in->data;
    buf_size = in->size;
    buf_end  = in->data + in->size;

    do {
        ret= averror(einval);
        if (buf + 4 /*s->length_size*/ > buf_end)
            goto fail;

        for (nal_size = 0, i = 0; i<4/*s->length_size*/; i++)
            nal_size = (nal_size << 8) | buf[i];

        buf += 4; /*s->length_size;*/
        unit_type = *buf & 0x1f;
        unit_type = *buf & 0x1f

        if (nal_size > buf_end - buf || nal_size < 0)
            goto fail;

        /*
        if (unit_type == 7)
            s->idr_sps_seen = s->new_idr = 1;
        else if (unit_type == 8) {
            s->idr_pps_seen = s->new_idr = 1;
            */
            /* if sps has not been seen yet, prepend the avcc one to pps */
            /*
            if (!s->idr_sps_seen) {
                if (s->sps_offset == -1)
                    av_log(ctx, av_log_warning, "sps not present in the stream, nor in avcc, stream may be unreadable\n");
                else {
                    if ((ret = alloc_and_copy(out,
                                         ctx->par_out->extradata + s->sps_offset,
                                         s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset,
                                         buf, nal_size)) < 0)
                        goto fail;
                    s->idr_sps_seen = 1;
                    goto next_nal;
                }
            }
        }
        */

        /* if this is a new idr picture following an idr picture, reset the idr flag.
         * just check first_mb_in_slice to be 0 as this is the simplest solution.
         * this could be checking idr_pic_id instead, but would complexify the parsing. */
        /*
        if (!s->new_idr && unit_type == 5 && (buf[1] & 0x80))
            s->new_idr = 1;

        */
        /* prepend only to the first type 5 nal unit of an idr picture, if no sps/pps are already present */
        if (/*s->new_idr && */unit_type == 5 /*&& !s->idr_sps_seen && !s->idr_pps_seen*/) {

            h264_extradata_to_annexb( fmt_ctx->streams[in->stream_index]->codec->extradata,
                                      fmt_ctx->streams[in->stream_index]->codec->extradata_size,
                                      &spspps_pkt,
                                      av_input_buffer_padding_size);

            if ((ret=alloc_and_copy(out,
                               spspps_pkt.data, spspps_pkt.size,
                               buf, nal_size)) < 0)
                goto fail;
            /*s->new_idr = 0;*/
        /* if only sps has been seen, also insert pps */
        }
        /*else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen) {
            if (s->pps_offset == -1) {
                av_log(ctx, av_log_warning, "pps not present in the stream, nor in avcc, stream may be unreadable\n");
                if ((ret = alloc_and_copy(out, null, 0, buf, nal_size)) < 0)
                    goto fail;
            } else if ((ret = alloc_and_copy(out,
                                        ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,
                                        buf, nal_size)) < 0)
                goto fail;
        }*/ else {
            if ((ret=alloc_and_copy(out, null, 0, buf, nal_size)) < 0)
                goto fail;
            /*
            if (!s->new_idr && unit_type == 1) {
                s->new_idr = 1;
                s->idr_sps_seen = 0;
                s->idr_pps_seen = 0;
            }
            */
        }


        len = fwrite( out->data, 1, out->size, dst_fd);
        if(len != out->size){
            av_log(null, av_log_debug, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
                    len,
                    out->size);
        }
        fflush(dst_fd);

next_nal:
        buf        += nal_size;
        cumul_size += nal_size + 4;//s->length_size;
    } while (cumul_size < buf_size);

    /*
    ret = av_packet_copy_props(out, in);
    if (ret < 0)
        goto fail;

    */
fail:
    av_packet_free(&out);

    return ret;
}

int main(int argc, char argv[]){
  int err_code;
  char errors[1024];
  
  char *src_filename = null;
  char *dst_filename = null;
  
  file *dst_fd = null;
  int video_stream_index = -1;
  
  // 创建文件上下文,和包管理
  avformatcontext *fmt_ctx = null;
  avpacket pkt;
  
  // 设置日志等级
  av_log_set_level(av_log_debug);
  
  // 判断参数个数
  if (argc < 3){
    av_log(null, av_log_debug, "参数不足");
    return -1
  }
  // 输出输出文件
  src_filename = argv[1];
  dst_filename = argv[2];
  
  //  判断参数不可以为空
  if (src_filename == null || dst_filename == null){
    av_log(null, av_log_error, "传入的参数不可以为空");
    return -1;
  }
  
  // 全局注册函数
  av_register_all();
  
  // 打开目标文件
  dst_fd = fopen(dst_filename, "wb");
  if (!dst_fd){
    av_log(null, av_log_debug, "不能打开文件%s", dst_filename);
    return -1;
  }
  
  // 读取流文件
  if ((err_code=avformat_open_input(&fmt_ctx, src_filename, null, null))<0){
    // 设置错误信息
    av_strerror(err_code, errors, 1024);
    // 打印日志输出
    av_log(null, av_log_debug, "流文件读取失败: %s, %d(%s)\n",
    src_filename,
    err_code,
    errors;);
    return -1;
  }
  av_dump_format(fmt_ctx, 0 , src_filename, 0);
  
  // 数据包处理
  av_init_packet(&pkt);
  pkt.data=null;
  pkt.size = 0;
  
  // 找到最好的流
  video_stream_index = av_find_best_stream(fmt_ctx, avmedia_type_video, -1, -1, null, 0);
  if (video_stream_index<0){
    av_log(null, av_log_debug, "不能找到流输出", av_get_media_type_string(avmedia_type_video),
          src_filename);
    return averror(einval);
  }
  
  // 拿到视频的index,去视频流中去取一个一个包文件
  while (av_read_frame(fmt_ctx, &pkt) >= 0){
    if(pkt.stream_index == video_stream_index){
      
      // 处理h264处理函数
      h264_mp4toannexb(fmt_ctx, &pkt, dst_fd);
      
    }
    av_packet_unref(&pkt);
  }
  
  // 关闭流文件
  avformat_close_input(&fmt_ctx);
  if(dst_fd){
    fclose(dst_fd);
  }
  
  return 0;
  
  
}