• startcode
  • sps/pps
  • codec->extradata ```cpp

    include

    include

    include

ifndef AV_WB32

define AV_WB32(p, val) do { \

  1. uint32_t d = (val); \
  2. ((uint8_t*)(p))[3] = (d); \
  3. ((uint8_t*)(p))[2] = (d)>>8; \
  4. ((uint8_t*)(p))[1] = (d)>>16; \
  5. ((uint8_t*)(p))[0] = (d)>>24; \
  6. } while(0)

endif

ifndef AV_RB16

define AV_RB16(x) \

  1. ((((const uint8_t*)(x))[0] << 8) | \
  2. ((const uint8_t*)(x))[1])

endif

int h264_exradata_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, pps_len = 0, sps_offset = 0, pps_offset = 0; const uint8_t extradata = codec_extradata + 4; //扩展数据的前4个字节无用,直接跳过 static const uint8_t nalu_header[4] = {0, 0, 0, 1}; int length_size = (*extradata++ & 0x3) + 1; //表示sps和pps的长度所占的字节数,一般是2个

  1. sps_offset = pps_offset = -1;
  2. unit_nb = *extradata++ & 0x1f; //sps和pps的个数
  3. if (!unit_nb) {
  4. goto pps;
  5. } else {
  6. sps_offset = 0;
  7. sps_seen = 1;
  8. }
  9. //遍历每个sps和pps
  10. while (unit_nb--) {
  11. int err;
  12. unit_size = AV_RB16(extradata); //获取sps和pps的长度
  13. total_size += unit_size + 4; //+startcode
  14. if (total_size > INT_MAX - padding) {
  15. av_log(NULL, AV_LOG_ERROR, "Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
  16. av_free(out);
  17. return AVERROR(EINVAL);
  18. }
  19. if (extradata+2+unit_size > codec_extradata+codec_extradata_size) {
  20. av_log(NULL, AV_LOG_ERROR, "Packet header is not contained in global extradata"
  21. "corrupted stream or invalid MP4/AVCC bitstream\n");
  22. av_free(out);
  23. return AVERROR(EINVAL);
  24. }
  25. //重新分配长度
  26. if ((err = av_reallocp(&out, total_size+padding)) < 0)
  27. return err;
  28. memcpy(out+total_size-unit_size-4, nalu_header, 4);
  29. memcpy(out+total_size-unit_size, extradata+2, unit_size);
  30. extradata += 2 + unit_size;

pps: if (!unit_nb && !sps_done++) { unit_nb = *extradata++; if (unit_nb) { pps_offset = total_size; sps_seen = 1; } } } if (out) { memset(out+total_size, 0, padding); }

  1. if (!sps_seen) {
  2. av_log(NULL, AV_LOG_WARNING, "Warning:SPS NALU missing or invalid."
  3. "The resulting stream may not play.\n");
  4. }
  5. if (!pps_seen) {
  6. av_log(NULL, AV_LOG_WARNING, "Warning:PPS NALU missing or invalid."
  7. "The resulting stream may not play.\n");
  8. }
  9. out_extradata->data = out;
  10. out_extradata->size = total_size;
  11. return length_size;

}

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;

  1. //对avpacket进行扩容
  2. err = av_grow_packet(out, sps_pps_size+in_size+nal_header_size);
  3. if (err) {
  4. return err;
  5. }
  6. if (sps_pps)
  7. memcpy(out->data+offset, sps_pps, sps_pps_size);
  8. memcpy(out->data+sps_pps_size+nal_header_size+offset, in, in_size);
  9. if (!offset) {
  10. AV_WB32(out->data+sps_pps_size, 1);
  11. } else {
  12. (out->data + offset + sps_pps_size)[0] = 0;
  13. (out->data + offset + sps_pps_size)[0] = 0;
  14. (out->data + offset + sps_pps_size)[0] = 1;
  15. }
  16. return 0;

} int h264_mp4toannexb(AVFormatContext fmt_ctx, AVPacket in, FILE dst_fd) { AVPacket out = NULL; AVPacket spspps_pkt;

  1. int len = 0;
  2. uint8_t unit_type;
  3. uint32_t nal_size;
  4. uint32_t cumul_size = 0;
  5. const uint8_t *buf = NULL;
  6. const uint8_t *buf_end = NULL;
  7. int buf_size;
  8. int ret = 0, i = 0;
  9. out = av_packet_alloc(); //此时out->size == 0
  10. buf = in->data;
  11. buf_size = in->size;
  12. buf_end = in->data + in->size;
  13. do {
  14. ret = AVERROR(EINVAL);
  15. if (buf + 4 > buf_end) {
  16. goto fail;
  17. }
  18. //pkt.data的前四个字节是一帧数据的大小; 如果是存放多帧的话,先取4个字节的帧大小取帧数据,再取4个字节帧大小,取帧数据,以此类推
  19. for (nal_size = 0, i = 0; i<4; i++) {
  20. nal_size = (nal_size << 8) | buf[i];
  21. }
  22. buf += 4;
  23. unit_type = *buf & 0x1f;
  24. if (nal_size > buf_end - buf || nal_size < 0)
  25. goto fail;
  26. //如果是关键帧,需要添加sps和pps在关键帧数据前
  27. if (unit_type == 5) {
  28. h264_exradata_to_annexb(fmt_ctx->streams[in->stream_index]->codec->extradata,
  29. fmt_ctx->streams[in->stream_index]->codec->extradata_size,
  30. &spspps_pkt,
  31. AV_INPUT_BUFFER_PADDING_SIZE);
  32. if ((ret = alloc_and_copy(out,
  33. spspps_pkt.data, spspps_pkt.size,
  34. buf, nal_size)) < 0) {
  35. goto fail;
  36. }
  37. } else {
  38. if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
  39. goto fail;
  40. }
  41. len = fwrite(out->data, 1, out->size, dst_fd);
  42. if (len != out->size) {
  43. av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
  44. len,
  45. out->size);
  46. }
  47. fflush(dst_fd);

next_nal: buf += nal_size; cumul_size += nal_size + 4; } while (cumul_size < buf_size);

fail: av_packet_free(&out);

  1. return ret;

} int main(int argc, char *argv[]) { int err_code; char errors[1024];

  1. char *src_filename = NULL;
  2. char *dst_filename = NULL;
  3. FILE *dst_fd = NULL;
  4. int video_stream_index = -1;
  5. AVFormatContext *fmt_ctx = NULL;
  6. AVPacket pkt;
  7. av_log_set_level(AV_LOG_DEBUG);
  8. if (argc < 3) {
  9. av_log(NULL, AV_LOG_DEBUG, "the count of paramers should be more than three!\n");
  10. return -1;
  11. }
  12. src_filename = argv[1];
  13. dst_filename = argv[2];
  14. if (src_filename == NULL || dst_filename == NULL) {
  15. av_log(NULL, AV_LOG_DEBUG, "src or dst file is null, plz check them!\n");
  16. return -1;
  17. }
  18. //register all formats and codec
  19. av_register_all();
  20. dst_fd = fopen(dst_filename, "wb");
  21. if (!dst_fd) {
  22. av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", dst_filename);
  23. return -1;
  24. }
  25. if ((err_code = avformat_open_input(&fmt_ctx, src_filename, NULL, NULL)) < 0) {
  26. av_strerror(err_code, errors, 1024);
  27. av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",
  28. src_filename,
  29. err_code,
  30. errors);
  31. return -1;
  32. }
  33. av_dump_format(fmt_ctx, 0, src_filename, 0);
  34. av_init_packet(&pkt);
  35. pkt.data = NULL;
  36. pkt.size = 0;
  37. video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  38. if (video_stream_index < 0) {
  39. av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",
  40. av_get_media_type_string(AVMEDIA_TYPE_VIDEO),
  41. src_filename);
  42. return AVERROR(EINVAL);
  43. }
  44. while (av_read_frame(fmt_ctx, &pkt) >=0) {
  45. if (pkt.stream_index == video_stream_index) {
  46. h264_mp4toannexb(fmt_ctx, &pkt, dst_fd);
  47. }
  48. av_packet_unref(&pkt);
  49. }
  50. av_packet_unref(&pkt);
  51. if (fmt_ctx) {
  52. avformat_close_input(&fmt_ctx);
  53. fmt_ctx = NULL;
  54. }
  55. if (dst_fd) {
  56. fclose(dst_fd);
  57. dst_fd = NULL;
  58. }
  59. return 0;

}

```