h264_mp4toannexb_bsf.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/intreadwrite.h"
  21. #include "avcodec.h"
  22. typedef struct H264BSFContext {
  23. uint8_t length_size;
  24. uint8_t first_idr;
  25. uint8_t *sps_pps_data;
  26. uint32_t size;
  27. } H264BSFContext;
  28. static void alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size,
  29. const uint8_t *sps_pps, uint32_t sps_pps_size,
  30. const uint8_t *in, uint32_t in_size) {
  31. uint32_t offset = *poutbuf_size;
  32. uint8_t nal_header_size = offset ? 3 : 4;
  33. *poutbuf_size += sps_pps_size+in_size+nal_header_size;
  34. *poutbuf = av_realloc(*poutbuf, *poutbuf_size);
  35. if (sps_pps)
  36. memcpy(*poutbuf+offset, sps_pps, sps_pps_size);
  37. memcpy(*poutbuf+sps_pps_size+nal_header_size+offset, in, in_size);
  38. if (!offset)
  39. AV_WB32(*poutbuf+sps_pps_size, 1);
  40. else {
  41. (*poutbuf+offset+sps_pps_size)[0] = (*poutbuf+offset+sps_pps_size)[1] = 0;
  42. (*poutbuf+offset+sps_pps_size)[2] = 1;
  43. }
  44. }
  45. static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
  46. AVCodecContext *avctx, const char *args,
  47. uint8_t **poutbuf, int *poutbuf_size,
  48. const uint8_t *buf, int buf_size,
  49. int keyframe) {
  50. H264BSFContext *ctx = bsfc->priv_data;
  51. uint8_t unit_type;
  52. uint32_t nal_size, cumul_size = 0;
  53. /* nothing to filter */
  54. if (!avctx->extradata || avctx->extradata_size < 6) {
  55. *poutbuf = (uint8_t*) buf;
  56. *poutbuf_size = buf_size;
  57. return 0;
  58. }
  59. /* retrieve sps and pps NAL units from extradata */
  60. if (!ctx->sps_pps_data) {
  61. uint16_t unit_size;
  62. uint32_t total_size = 0;
  63. uint8_t *out = NULL, unit_nb, sps_done = 0;
  64. const uint8_t *extradata = avctx->extradata+4;
  65. static const uint8_t nalu_header[4] = {0, 0, 0, 1};
  66. /* retrieve length coded size */
  67. ctx->length_size = (*extradata++ & 0x3) + 1;
  68. if (ctx->length_size == 3)
  69. return AVERROR(EINVAL);
  70. /* retrieve sps and pps unit(s) */
  71. unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
  72. if (!unit_nb) {
  73. unit_nb = *extradata++; /* number of pps unit(s) */
  74. sps_done++;
  75. }
  76. while (unit_nb--) {
  77. unit_size = AV_RB16(extradata);
  78. total_size += unit_size+4;
  79. if (extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
  80. av_free(out);
  81. return AVERROR(EINVAL);
  82. }
  83. out = av_realloc(out, total_size);
  84. if (!out)
  85. return AVERROR(ENOMEM);
  86. memcpy(out+total_size-unit_size-4, nalu_header, 4);
  87. memcpy(out+total_size-unit_size, extradata+2, unit_size);
  88. extradata += 2+unit_size;
  89. if (!unit_nb && !sps_done++)
  90. unit_nb = *extradata++; /* number of pps unit(s) */
  91. }
  92. ctx->sps_pps_data = out;
  93. ctx->size = total_size;
  94. ctx->first_idr = 1;
  95. }
  96. *poutbuf_size = 0;
  97. *poutbuf = NULL;
  98. do {
  99. if (ctx->length_size == 1)
  100. nal_size = buf[0];
  101. else if (ctx->length_size == 2)
  102. nal_size = AV_RB16(buf);
  103. else
  104. nal_size = AV_RB32(buf);
  105. buf += ctx->length_size;
  106. unit_type = *buf & 0x1f;
  107. /* prepend only to the first type 5 NAL unit of an IDR picture */
  108. if (ctx->first_idr && unit_type == 5) {
  109. alloc_and_copy(poutbuf, poutbuf_size,
  110. ctx->sps_pps_data, ctx->size,
  111. buf, nal_size);
  112. ctx->first_idr = 0;
  113. }
  114. else {
  115. alloc_and_copy(poutbuf, poutbuf_size,
  116. NULL, 0,
  117. buf, nal_size);
  118. if (!ctx->first_idr && unit_type == 1)
  119. ctx->first_idr = 1;
  120. }
  121. buf += nal_size;
  122. cumul_size += nal_size + ctx->length_size;
  123. } while (cumul_size < buf_size);
  124. return 1;
  125. }
  126. static void h264_mp4toannexb_close(AVBitStreamFilterContext *bsfc)
  127. {
  128. H264BSFContext *ctx = bsfc->priv_data;
  129. av_freep(&ctx->sps_pps_data);
  130. }
  131. AVBitStreamFilter h264_mp4toannexb_bsf = {
  132. "h264_mp4toannexb",
  133. sizeof(H264BSFContext),
  134. h264_mp4toannexb_filter,
  135. h264_mp4toannexb_close,
  136. };