FFmpeg抽取视频数据

SPS/PPS在ffmpeg的codec->extradata中获取

  1. 加载ffmpeg的日志模块

    1
    2
    3
    #include <libavutil/log.h>
    ...
    av_log_set_level(AV_LOG_DEBUG);
  2. 注册所有编解码器

    1
    2
    /*register all formats and codec*/
    av_register_all();
  3. 打开视频文件并获取视频文件上下文

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    AVFormatContext *fmt_ctx = NULL;

    /*open input media file, and allocate format context*/
    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, "Could not open source file: %s, %d(%s)\n",
    src_filename,
    err_code,
    errors);
    return -1;
    }
  4. 打印视频详细信息

    1
    2
    /*dump input information*/
    av_dump_format(fmt_ctx, 0, src_filename, 0);
  5. 生成一个空的数据包,用于接收视频包

    1
    2
    3
    4
    /*initialize packet*/
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;
  6. 获取视频流

    1
    2
    3
    4
    5
    6
    7
    /*find best video stream*/
    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_ERROR, "Could not find %s stream in input file %s\n", av_get_media_type_string(AVMEDIA_TYPE_VIDEO),src_filename);
    return AVERROR(EINVAL);
    }
  7. 循环读取视频流中的包,进行处理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    /*read frames from media file*/
    while (av_read_frame(fmt_ctx, &pkt) >= 0)
    {
    if (pkt.stream_index == video_stream_index)
    {
    h264_mp4toannexb(fmt_ctx, &pkt, dst_fd);
    }
    //release pkt->data
    av_packet_unref(&pkt);
    }
    1. 对包进行处理,提取帧数据进行写文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      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;
      //一个AVPacket中存的可能是一帧也可能是多帧
      // nal_size是一个帧的具体大小,在帧的头部的前4个字节
      for (nal_size = 0, i = 0; i < 4 /*s->length_size*/; i++)
      nal_size = (nal_size << 8) | buf[i];

      buf += 4; /*s->length_size;*/
      //帧数据的第一个字节的后5位是这个帧的nal单元
      /* nal: 7 -> sps
      8 -> pps
      key frame -> 5
      normal frame -> 1 */
      unit_type = *buf & 0x1f;

      if (nal_size > buf_end - buf || nal_size < 0)
      goto fail;
      /* 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*/)
      {
      // 关键帧,需要获取sps/pps数据,并重新修改视频宽高比等参数
      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 ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
      goto fail;
      }

      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);
      fail:
      av_packet_free(&out);

      return ret;
      }
  1. 获取SPS/PPS数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    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;
    }
  1. 为每一帧数据增加特征码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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;
    }