You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.6KB

  1. /*
  2. * H.264 MP4 to Annex B byte stream format filter
  3. * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/mem.h"
  24. #include "avcodec.h"
  25. typedef struct H264BSFContext {
  26. uint8_t length_size;
  27. uint8_t first_idr;
  28. int extradata_parsed;
  29. } H264BSFContext;
  30. static int alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size,
  31. const uint8_t *sps_pps, uint32_t sps_pps_size,
  32. const uint8_t *in, uint32_t in_size)
  33. {
  34. uint32_t offset = *poutbuf_size;
  35. uint8_t nal_header_size = offset ? 3 : 4;
  36. void *tmp;
  37. *poutbuf_size += sps_pps_size + in_size + nal_header_size;
  38. tmp = av_realloc(*poutbuf, *poutbuf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  39. if (!tmp)
  40. return AVERROR(ENOMEM);
  41. *poutbuf = tmp;
  42. if (sps_pps)
  43. memcpy(*poutbuf + offset, sps_pps, sps_pps_size);
  44. memcpy(*poutbuf + sps_pps_size + nal_header_size + offset, in, in_size);
  45. if (!offset) {
  46. AV_WB32(*poutbuf + sps_pps_size, 1);
  47. } else {
  48. (*poutbuf + offset + sps_pps_size)[0] =
  49. (*poutbuf + offset + sps_pps_size)[1] = 0;
  50. (*poutbuf + offset + sps_pps_size)[2] = 1;
  51. }
  52. return 0;
  53. }
  54. static int h264_extradata_to_annexb(AVCodecContext *avctx, const int padding)
  55. {
  56. uint16_t unit_size;
  57. uint64_t total_size = 0;
  58. uint8_t *out = NULL, unit_nb, sps_done = 0,
  59. sps_seen = 0, pps_seen = 0;
  60. const uint8_t *extradata = avctx->extradata + 4;
  61. static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
  62. int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size
  63. if (length_size == 3)
  64. return AVERROR(EINVAL);
  65. /* retrieve sps and pps unit(s) */
  66. unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
  67. if (!unit_nb) {
  68. unit_nb = *extradata++; /* number of pps unit(s) */
  69. sps_done++;
  70. if (unit_nb)
  71. pps_seen = 1;
  72. } else {
  73. sps_seen = 1;
  74. }
  75. while (unit_nb--) {
  76. void *tmp;
  77. unit_size = AV_RB16(extradata);
  78. total_size += unit_size + 4;
  79. if (total_size > INT_MAX - padding ||
  80. extradata + 2 + unit_size > avctx->extradata +
  81. avctx->extradata_size) {
  82. av_free(out);
  83. return AVERROR(EINVAL);
  84. }
  85. tmp = av_realloc(out, total_size + padding);
  86. if (!tmp) {
  87. av_free(out);
  88. return AVERROR(ENOMEM);
  89. }
  90. out = tmp;
  91. memcpy(out + total_size - unit_size - 4, nalu_header, 4);
  92. memcpy(out + total_size - unit_size, extradata + 2, unit_size);
  93. extradata += 2 + unit_size;
  94. if (!unit_nb && !sps_done++) {
  95. unit_nb = *extradata++; /* number of pps unit(s) */
  96. if (unit_nb)
  97. pps_seen = 1;
  98. }
  99. }
  100. if (out)
  101. memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  102. if (!sps_seen)
  103. av_log(avctx, AV_LOG_WARNING,
  104. "Warning: SPS NALU missing or invalid. "
  105. "The resulting stream may not play.\n");
  106. if (!pps_seen)
  107. av_log(avctx, AV_LOG_WARNING,
  108. "Warning: PPS NALU missing or invalid. "
  109. "The resulting stream may not play.\n");
  110. av_free(avctx->extradata);
  111. avctx->extradata = out;
  112. avctx->extradata_size = total_size;
  113. return length_size;
  114. }
  115. static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
  116. AVCodecContext *avctx, const char *args,
  117. uint8_t **poutbuf, int *poutbuf_size,
  118. const uint8_t *buf, int buf_size,
  119. int keyframe)
  120. {
  121. H264BSFContext *ctx = bsfc->priv_data;
  122. uint8_t unit_type;
  123. int32_t nal_size;
  124. uint32_t cumul_size = 0;
  125. const uint8_t *buf_end = buf + buf_size;
  126. int ret = 0;
  127. /* nothing to filter */
  128. if (!avctx->extradata || avctx->extradata_size < 6) {
  129. *poutbuf = (uint8_t *)buf;
  130. *poutbuf_size = buf_size;
  131. return 0;
  132. }
  133. /* retrieve sps and pps NAL units from extradata */
  134. if (!ctx->extradata_parsed) {
  135. ret = h264_extradata_to_annexb(avctx, FF_INPUT_BUFFER_PADDING_SIZE);
  136. if (ret < 0)
  137. return ret;
  138. ctx->length_size = ret;
  139. ctx->first_idr = 1;
  140. ctx->extradata_parsed = 1;
  141. }
  142. *poutbuf_size = 0;
  143. *poutbuf = NULL;
  144. do {
  145. if (buf + ctx->length_size > buf_end)
  146. goto fail;
  147. if (ctx->length_size == 1) {
  148. nal_size = buf[0];
  149. } else if (ctx->length_size == 2) {
  150. nal_size = AV_RB16(buf);
  151. } else
  152. nal_size = AV_RB32(buf);
  153. buf += ctx->length_size;
  154. unit_type = *buf & 0x1f;
  155. if (buf + nal_size > buf_end || nal_size < 0)
  156. goto fail;
  157. /* prepend only to the first type 5 NAL unit of an IDR picture */
  158. if (ctx->first_idr && unit_type == 5) {
  159. if (alloc_and_copy(poutbuf, poutbuf_size,
  160. avctx->extradata, avctx->extradata_size,
  161. buf, nal_size) < 0)
  162. goto fail;
  163. ctx->first_idr = 0;
  164. } else {
  165. if (alloc_and_copy(poutbuf, poutbuf_size,
  166. NULL, 0, buf, nal_size) < 0)
  167. goto fail;
  168. if (!ctx->first_idr && unit_type == 1)
  169. ctx->first_idr = 1;
  170. }
  171. buf += nal_size;
  172. cumul_size += nal_size + ctx->length_size;
  173. } while (cumul_size < buf_size);
  174. return 1;
  175. fail:
  176. av_freep(poutbuf);
  177. *poutbuf_size = 0;
  178. return AVERROR(EINVAL);
  179. }
  180. AVBitStreamFilter ff_h264_mp4toannexb_bsf = {
  181. "h264_mp4toannexb",
  182. sizeof(H264BSFContext),
  183. h264_mp4toannexb_filter,
  184. };