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.

110 lines
3.0KB

  1. /*
  2. * H.264 encoder
  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/common.h"
  21. #include "bitstream.h"
  22. #include "mpegvideo.h"
  23. #include "h264data.h"
  24. /**
  25. * Write out the provided data into a NAL unit.
  26. * @param nal_ref_idc NAL reference IDC
  27. * @param nal_unit_type NAL unit payload type
  28. * @param dest the target buffer, dst+1 == src is allowed as a special case
  29. * @param destsize the length of the dst array
  30. * @param b2 the data which should be escaped
  31. * @returns pointer to current position in the output buffer or NULL if an error occurred
  32. */
  33. static uint8_t *h264_write_nal_unit(int nal_ref_idc, int nal_unit_type, uint8_t *dest, int *destsize,
  34. PutBitContext *b2)
  35. {
  36. PutBitContext b;
  37. int i, destpos, rbsplen, escape_count;
  38. uint8_t *rbsp;
  39. if (nal_unit_type != NAL_END_STREAM)
  40. put_bits(b2,1,1); // rbsp_stop_bit
  41. // Align b2 on a byte boundary
  42. align_put_bits(b2);
  43. rbsplen = put_bits_count(b2)/8;
  44. flush_put_bits(b2);
  45. rbsp = b2->buf;
  46. init_put_bits(&b,dest,*destsize);
  47. put_bits(&b,16,0);
  48. put_bits(&b,16,0x01);
  49. put_bits(&b,1,0); // forbidden zero bit
  50. put_bits(&b,2,nal_ref_idc); // nal_ref_idc
  51. put_bits(&b,5,nal_unit_type); // nal_unit_type
  52. flush_put_bits(&b);
  53. destpos = 5;
  54. escape_count= 0;
  55. for (i=0; i<rbsplen; i+=2)
  56. {
  57. if (rbsp[i]) continue;
  58. if (i>0 && rbsp[i-1]==0)
  59. i--;
  60. if (i+2<rbsplen && rbsp[i+1]==0 && rbsp[i+2]<=3)
  61. {
  62. escape_count++;
  63. i+=2;
  64. }
  65. }
  66. if(escape_count==0)
  67. {
  68. if(dest+destpos != rbsp)
  69. {
  70. memcpy(dest+destpos, rbsp, rbsplen);
  71. *destsize -= (rbsplen+destpos);
  72. }
  73. return dest+rbsplen+destpos;
  74. }
  75. if(rbsplen + escape_count + 1> *destsize)
  76. {
  77. av_log(NULL, AV_LOG_ERROR, "Destination buffer too small!\n");
  78. return NULL;
  79. }
  80. // this should be damn rare (hopefully)
  81. for (i = 0 ; i < rbsplen ; i++)
  82. {
  83. if (i + 2 < rbsplen && (rbsp[i] == 0 && rbsp[i+1] == 0 && rbsp[i+2] < 4))
  84. {
  85. dest[destpos++] = rbsp[i++];
  86. dest[destpos++] = rbsp[i];
  87. dest[destpos++] = 0x03; // emulation prevention byte
  88. }
  89. else
  90. dest[destpos++] = rbsp[i];
  91. }
  92. *destsize -= destpos;
  93. return dest+destpos;
  94. }