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.

195 lines
6.7KB

  1. /*
  2. * Bitstream filter for unpacking DivX-style packed B-frames in MPEG-4 (divx_packed)
  3. * Copyright (c) 2015 Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "mpeg4video.h"
  23. typedef struct UnpackBFramesBSFContext {
  24. uint8_t *b_frame_buf;
  25. int b_frame_buf_size;
  26. int updated_extradata;
  27. } UnpackBFramesBSFContext;
  28. /* search next start code */
  29. static unsigned int find_startcode(const uint8_t *buf, int buf_size, int *pos)
  30. {
  31. unsigned int startcode = 0xFF;
  32. for (; *pos < buf_size;) {
  33. startcode = ((startcode << 8) | buf[*pos]) & 0xFFFFFFFF;
  34. *pos +=1;
  35. if ((startcode & 0xFFFFFF00) != 0x100)
  36. continue; /* no startcode */
  37. return startcode;
  38. }
  39. return 0;
  40. }
  41. /* determine the position of the packed marker in the userdata,
  42. * the number of VOPs and the position of the second VOP */
  43. static void scan_buffer(const uint8_t *buf, int buf_size,
  44. int *pos_p, int *nb_vop, int *pos_vop2) {
  45. unsigned int startcode;
  46. int pos, i;
  47. for (pos = 0; pos < buf_size;) {
  48. startcode = find_startcode(buf, buf_size, &pos);
  49. if (startcode == USER_DATA_STARTCODE && pos_p) {
  50. /* check if the (DivX) userdata string ends with 'p' (packed) */
  51. for (i = 0; i < 255 && pos + i + 1 < buf_size; i++) {
  52. if (buf[pos + i] == 'p' && buf[pos + i + 1] == '\0') {
  53. *pos_p = pos + i;
  54. break;
  55. }
  56. }
  57. } else if (startcode == VOP_STARTCODE && nb_vop) {
  58. *nb_vop += 1;
  59. if (*nb_vop == 2 && pos_vop2) {
  60. *pos_vop2 = pos - 4; /* subtract 4 bytes startcode */
  61. }
  62. }
  63. }
  64. }
  65. /* allocate new buffer and copy size bytes from src */
  66. static uint8_t *create_new_buffer(const uint8_t *src, int size) {
  67. uint8_t *dst = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  68. if (dst) {
  69. memcpy(dst, src, size);
  70. memset(dst + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  71. }
  72. return dst;
  73. }
  74. static int mpeg4_unpack_bframes_filter(AVBitStreamFilterContext *bsfc,
  75. AVCodecContext *avctx, const char *args,
  76. uint8_t **poutbuf, int *poutbuf_size,
  77. const uint8_t *buf, int buf_size,
  78. int keyframe)
  79. {
  80. UnpackBFramesBSFContext *ctx = bsfc->priv_data;
  81. int pos_p = -1, nb_vop = 0, pos_vop2 = -1, ret = 0;
  82. if (avctx->codec_id != AV_CODEC_ID_MPEG4) {
  83. av_log(avctx, AV_LOG_ERROR,
  84. "The mpeg4_unpack_bframes bitstream filter is only useful for mpeg4.\n");
  85. return AVERROR(EINVAL);
  86. }
  87. if (!ctx->updated_extradata && avctx->extradata) {
  88. int pos_p_ext = -1;
  89. scan_buffer(avctx->extradata, avctx->extradata_size, &pos_p_ext, NULL, NULL);
  90. if (pos_p_ext >= 0) {
  91. av_log(avctx, AV_LOG_DEBUG,
  92. "Updating DivX userdata (remove trailing 'p') in extradata.\n");
  93. avctx->extradata[pos_p_ext] = '\0';
  94. }
  95. ctx->updated_extradata = 1;
  96. }
  97. scan_buffer(buf, buf_size, &pos_p, &nb_vop, &pos_vop2);
  98. av_log(avctx, AV_LOG_DEBUG, "Found %d VOP startcode(s) in this packet.\n", nb_vop);
  99. if (pos_vop2 >= 0) {
  100. if (ctx->b_frame_buf) {
  101. av_log(avctx, AV_LOG_WARNING,
  102. "Missing one N-VOP packet, discarding one B-frame.\n");
  103. av_freep(&ctx->b_frame_buf);
  104. ctx->b_frame_buf_size = 0;
  105. }
  106. /* store the packed B-frame in the BSFContext */
  107. ctx->b_frame_buf_size = buf_size - pos_vop2;
  108. ctx->b_frame_buf = create_new_buffer(buf + pos_vop2, ctx->b_frame_buf_size);
  109. if (!ctx->b_frame_buf) {
  110. ctx->b_frame_buf_size = 0;
  111. return AVERROR(ENOMEM);
  112. }
  113. }
  114. if (nb_vop > 2) {
  115. av_log(avctx, AV_LOG_WARNING,
  116. "Found %d VOP headers in one packet, only unpacking one.\n", nb_vop);
  117. }
  118. if (nb_vop == 1 && ctx->b_frame_buf) {
  119. /* use frame from BSFContext */
  120. *poutbuf = ctx->b_frame_buf;
  121. *poutbuf_size = ctx->b_frame_buf_size;
  122. /* the output buffer is distinct from the input buffer */
  123. ret = 1;
  124. if (buf_size <= MAX_NVOP_SIZE) {
  125. /* N-VOP */
  126. av_log(avctx, AV_LOG_DEBUG, "Skipping N-VOP.\n");
  127. ctx->b_frame_buf = NULL;
  128. ctx->b_frame_buf_size = 0;
  129. } else {
  130. /* copy packet into BSFContext */
  131. ctx->b_frame_buf_size = buf_size;
  132. ctx->b_frame_buf = create_new_buffer(buf , buf_size);
  133. if (!ctx->b_frame_buf) {
  134. ctx->b_frame_buf_size = 0;
  135. av_freep(poutbuf);
  136. *poutbuf_size = 0;
  137. return AVERROR(ENOMEM);
  138. }
  139. }
  140. } else if (nb_vop >= 2) {
  141. /* use first frame of the packet */
  142. *poutbuf = (uint8_t *) buf;
  143. *poutbuf_size = pos_vop2;
  144. } else if (pos_p >= 0) {
  145. av_log(avctx, AV_LOG_DEBUG, "Updating DivX userdata (remove trailing 'p').\n");
  146. *poutbuf_size = buf_size;
  147. *poutbuf = create_new_buffer(buf, buf_size);
  148. if (!*poutbuf) {
  149. *poutbuf_size = 0;
  150. return AVERROR(ENOMEM);
  151. }
  152. /* remove 'p' (packed) from the end of the (DivX) userdata string */
  153. (*poutbuf)[pos_p] = '\0';
  154. /* the output buffer is distinct from the input buffer */
  155. ret = 1;
  156. } else {
  157. /* copy packet */
  158. *poutbuf = (uint8_t *) buf;
  159. *poutbuf_size = buf_size;
  160. }
  161. return ret;
  162. }
  163. static void mpeg4_unpack_bframes_close(AVBitStreamFilterContext *bsfc)
  164. {
  165. UnpackBFramesBSFContext *ctx = bsfc->priv_data;
  166. av_freep(&ctx->b_frame_buf);
  167. }
  168. AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf = {
  169. .name = "mpeg4_unpack_bframes",
  170. .priv_data_size = sizeof(UnpackBFramesBSFContext),
  171. .filter = mpeg4_unpack_bframes_filter,
  172. .close = mpeg4_unpack_bframes_close
  173. };