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.

155 lines
5.1KB

  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 "avcodec.h"
  21. typedef struct H264BSFContext {
  22. uint8_t length_size;
  23. uint8_t first_idr;
  24. uint8_t *sps_pps_data;
  25. uint32_t size;
  26. } H264BSFContext;
  27. static void alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size,
  28. const uint8_t *sps_pps, uint32_t sps_pps_size,
  29. const uint8_t *in, uint32_t in_size) {
  30. uint32_t offset = *poutbuf_size;
  31. uint8_t nal_header_size = offset ? 3 : 4;
  32. *poutbuf_size += sps_pps_size+in_size+nal_header_size;
  33. *poutbuf = av_realloc(*poutbuf, *poutbuf_size);
  34. if (sps_pps)
  35. memcpy(*poutbuf+offset, sps_pps, sps_pps_size);
  36. memcpy(*poutbuf+sps_pps_size+nal_header_size+offset, in, in_size);
  37. if (!offset)
  38. AV_WB32(*poutbuf+sps_pps_size, 1);
  39. else {
  40. (*poutbuf+offset+sps_pps_size)[0] = (*poutbuf+offset+sps_pps_size)[1] = 0;
  41. (*poutbuf+offset+sps_pps_size)[2] = 1;
  42. }
  43. }
  44. static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
  45. AVCodecContext *avctx, const char *args,
  46. uint8_t **poutbuf, int *poutbuf_size,
  47. const uint8_t *buf, int buf_size,
  48. int keyframe) {
  49. H264BSFContext *ctx = bsfc->priv_data;
  50. uint8_t unit_type;
  51. uint32_t nal_size, cumul_size = 0;
  52. /* nothing to filter */
  53. if (!avctx->extradata || avctx->extradata_size < 6) {
  54. *poutbuf = (uint8_t*) buf;
  55. *poutbuf_size = buf_size;
  56. return 0;
  57. }
  58. /* retrieve sps and pps NAL units from extradata */
  59. if (!ctx->sps_pps_data) {
  60. uint16_t unit_size;
  61. uint32_t total_size = 0;
  62. uint8_t *out = NULL, unit_nb, sps_done = 0;
  63. const uint8_t *extradata = avctx->extradata+4;
  64. static const uint8_t nalu_header[4] = {0, 0, 0, 1};
  65. /* retrieve length coded size */
  66. ctx->length_size = (*extradata++ & 0x3) + 1;
  67. if (ctx->length_size == 3)
  68. return AVERROR(EINVAL);
  69. /* retrieve sps and pps unit(s) */
  70. unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
  71. if (!unit_nb) {
  72. unit_nb = *extradata++; /* number of pps unit(s) */
  73. sps_done++;
  74. }
  75. while (unit_nb--) {
  76. unit_size = AV_RB16(extradata);
  77. total_size += unit_size+4;
  78. if (extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
  79. av_free(out);
  80. return AVERROR(EINVAL);
  81. }
  82. out = av_realloc(out, total_size);
  83. if (!out)
  84. return AVERROR(ENOMEM);
  85. memcpy(out+total_size-unit_size-4, nalu_header, 4);
  86. memcpy(out+total_size-unit_size, extradata+2, unit_size);
  87. extradata += 2+unit_size;
  88. if (!unit_nb && !sps_done++)
  89. unit_nb = *extradata++; /* number of pps unit(s) */
  90. }
  91. ctx->sps_pps_data = out;
  92. ctx->size = total_size;
  93. ctx->first_idr = 1;
  94. }
  95. *poutbuf_size = 0;
  96. *poutbuf = NULL;
  97. do {
  98. if (ctx->length_size == 1)
  99. nal_size = buf[0];
  100. else if (ctx->length_size == 2)
  101. nal_size = AV_RB16(buf);
  102. else
  103. nal_size = AV_RB32(buf);
  104. buf += ctx->length_size;
  105. unit_type = *buf & 0x1f;
  106. /* prepend only to the first type 5 NAL unit of an IDR picture */
  107. if (ctx->first_idr && unit_type == 5) {
  108. alloc_and_copy(poutbuf, poutbuf_size,
  109. ctx->sps_pps_data, ctx->size,
  110. buf, nal_size);
  111. ctx->first_idr = 0;
  112. }
  113. else {
  114. alloc_and_copy(poutbuf, poutbuf_size,
  115. NULL, 0,
  116. buf, nal_size);
  117. if (!ctx->first_idr && unit_type == 1)
  118. ctx->first_idr = 1;
  119. }
  120. buf += nal_size;
  121. cumul_size += nal_size + ctx->length_size;
  122. } while (cumul_size < buf_size);
  123. return 1;
  124. }
  125. static void h264_mp4toannexb_close(AVBitStreamFilterContext *bsfc)
  126. {
  127. H264BSFContext *ctx = bsfc->priv_data;
  128. av_freep(&ctx->sps_pps_data);
  129. }
  130. AVBitStreamFilter h264_mp4toannexb_bsf = {
  131. "h264_mp4toannexb",
  132. sizeof(H264BSFContext),
  133. h264_mp4toannexb_filter,
  134. h264_mp4toannexb_close,
  135. };