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.

237 lines
6.7KB

  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. #include "bsf.h"
  26. typedef struct H264BSFContext {
  27. uint8_t length_size;
  28. uint8_t first_idr;
  29. int extradata_parsed;
  30. } H264BSFContext;
  31. static int alloc_and_copy(AVPacket *out,
  32. const uint8_t *sps_pps, uint32_t sps_pps_size,
  33. const uint8_t *in, uint32_t in_size)
  34. {
  35. uint32_t offset = out->size;
  36. uint8_t nal_header_size = offset ? 3 : 4;
  37. int err;
  38. err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);
  39. if (err < 0)
  40. return err;
  41. if (sps_pps)
  42. memcpy(out->data + offset, sps_pps, sps_pps_size);
  43. memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);
  44. if (!offset) {
  45. AV_WB32(out->data + sps_pps_size, 1);
  46. } else {
  47. (out->data + offset + sps_pps_size)[0] =
  48. (out->data + offset + sps_pps_size)[1] = 0;
  49. (out->data + offset + sps_pps_size)[2] = 1;
  50. }
  51. return 0;
  52. }
  53. static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding)
  54. {
  55. uint16_t unit_size;
  56. uint64_t total_size = 0;
  57. uint8_t *out = NULL, unit_nb, sps_done = 0,
  58. sps_seen = 0, pps_seen = 0;
  59. const uint8_t *extradata = ctx->par_in->extradata + 4;
  60. static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
  61. int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size
  62. if (length_size == 3)
  63. return AVERROR(EINVAL);
  64. /* retrieve sps and pps unit(s) */
  65. unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
  66. if (!unit_nb) {
  67. unit_nb = *extradata++; /* number of pps unit(s) */
  68. sps_done++;
  69. if (unit_nb)
  70. pps_seen = 1;
  71. } else {
  72. sps_seen = 1;
  73. }
  74. while (unit_nb--) {
  75. int err;
  76. unit_size = AV_RB16(extradata);
  77. total_size += unit_size + 4;
  78. if (total_size > INT_MAX - padding ||
  79. extradata + 2 + unit_size > ctx->par_in->extradata +
  80. ctx->par_in->extradata_size) {
  81. av_free(out);
  82. return AVERROR(EINVAL);
  83. }
  84. if ((err = av_reallocp(&out, total_size + padding)) < 0)
  85. return err;
  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. if (unit_nb)
  92. pps_seen = 1;
  93. }
  94. }
  95. if (out)
  96. memset(out + total_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  97. if (!sps_seen)
  98. av_log(ctx, AV_LOG_WARNING,
  99. "Warning: SPS NALU missing or invalid. "
  100. "The resulting stream may not play.\n");
  101. if (!pps_seen)
  102. av_log(ctx, AV_LOG_WARNING,
  103. "Warning: PPS NALU missing or invalid. "
  104. "The resulting stream may not play.\n");
  105. av_freep(&ctx->par_out->extradata);
  106. ctx->par_out->extradata = out;
  107. ctx->par_out->extradata_size = total_size;
  108. return length_size;
  109. }
  110. static int h264_mp4toannexb_init(AVBSFContext *ctx)
  111. {
  112. H264BSFContext *s = ctx->priv_data;
  113. int ret;
  114. /* retrieve sps and pps NAL units from extradata */
  115. if (ctx->par_in->extradata_size >= 6) {
  116. ret = h264_extradata_to_annexb(ctx, AV_INPUT_BUFFER_PADDING_SIZE);
  117. if (ret < 0)
  118. return ret;
  119. s->length_size = ret;
  120. s->first_idr = 1;
  121. s->extradata_parsed = 1;
  122. }
  123. return 0;
  124. }
  125. static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
  126. {
  127. H264BSFContext *s = ctx->priv_data;
  128. AVPacket *in;
  129. uint8_t unit_type;
  130. int32_t nal_size;
  131. uint32_t cumul_size = 0;
  132. const uint8_t *buf;
  133. const uint8_t *buf_end;
  134. int buf_size;
  135. int ret = 0;
  136. ret = ff_bsf_get_packet(ctx, &in);
  137. if (ret < 0)
  138. return ret;
  139. /* nothing to filter */
  140. if (!s->extradata_parsed) {
  141. av_packet_move_ref(out, in);
  142. av_packet_free(&in);
  143. return 0;
  144. }
  145. buf = in->data;
  146. buf_size = in->size;
  147. buf_end = in->data + in->size;
  148. do {
  149. if (buf + s->length_size > buf_end)
  150. goto fail;
  151. if (s->length_size == 1) {
  152. nal_size = buf[0];
  153. } else if (s->length_size == 2) {
  154. nal_size = AV_RB16(buf);
  155. } else
  156. nal_size = AV_RB32(buf);
  157. buf += s->length_size;
  158. unit_type = *buf & 0x1f;
  159. if (buf + nal_size > buf_end || nal_size < 0)
  160. goto fail;
  161. /* prepend only to the first type 5 NAL unit of an IDR picture */
  162. if (s->first_idr && unit_type == 5) {
  163. if (alloc_and_copy(out,
  164. ctx->par_out->extradata, ctx->par_out->extradata_size,
  165. buf, nal_size) < 0)
  166. goto fail;
  167. s->first_idr = 0;
  168. } else {
  169. if (alloc_and_copy(out,
  170. NULL, 0, buf, nal_size) < 0)
  171. goto fail;
  172. if (!s->first_idr && unit_type == 1)
  173. s->first_idr = 1;
  174. }
  175. buf += nal_size;
  176. cumul_size += nal_size + s->length_size;
  177. } while (cumul_size < buf_size);
  178. ret = av_packet_copy_props(out, in);
  179. if (ret < 0)
  180. goto fail;
  181. fail:
  182. if (ret < 0)
  183. av_packet_unref(out);
  184. av_packet_free(&in);
  185. return ret;
  186. }
  187. static const enum AVCodecID codec_ids[] = {
  188. AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
  189. };
  190. const AVBitStreamFilter ff_h264_mp4toannexb_bsf = {
  191. .name = "h264_mp4toannexb",
  192. .priv_data_size = sizeof(H264BSFContext),
  193. .init = h264_mp4toannexb_init,
  194. .filter = h264_mp4toannexb_filter,
  195. .codec_ids = codec_ids,
  196. };