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.

108 lines
3.0KB

  1. /*
  2. * H.264 encoder
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "common.h"
  19. #include "bitstream.h"
  20. #include "mpegvideo.h"
  21. #include "h264data.h"
  22. /**
  23. * Write out the provided data into a NAL unit.
  24. * @param nal_ref_idc NAL reference IDC
  25. * @param nal_unit_type NAL unit payload type
  26. * @param dest the target buffer, dst+1 == src is allowed as a special case
  27. * @param destsize the length of the dst array
  28. * @param b2 the data which should be escaped
  29. * @returns pointer to current position in the output buffer or NULL if an error occured
  30. */
  31. static uint8_t *h264_write_nal_unit(int nal_ref_idc, int nal_unit_type, uint8_t *dest, int *destsize,
  32. PutBitContext *b2)
  33. {
  34. PutBitContext b;
  35. int i, destpos, rbsplen, escape_count;
  36. uint8_t *rbsp;
  37. if (nal_unit_type != NAL_END_STREAM)
  38. put_bits(b2,1,1); // rbsp_stop_bit
  39. // Align b2 on a byte boundary
  40. align_put_bits(b2);
  41. rbsplen = put_bits_count(b2)/8;
  42. flush_put_bits(b2);
  43. rbsp = b2->buf;
  44. init_put_bits(&b,dest,*destsize);
  45. put_bits(&b,16,0);
  46. put_bits(&b,16,0x01);
  47. put_bits(&b,1,0); // forbidden zero bit
  48. put_bits(&b,2,nal_ref_idc); // nal_ref_idc
  49. put_bits(&b,5,nal_unit_type); // nal_unit_type
  50. flush_put_bits(&b);
  51. destpos = 5;
  52. escape_count= 0;
  53. for (i=0; i<rbsplen; i+=2)
  54. {
  55. if (rbsp[i]) continue;
  56. if (i>0 && rbsp[i-1]==0)
  57. i--;
  58. if (i+2<rbsplen && rbsp[i+1]==0 && rbsp[i+2]<=3)
  59. {
  60. escape_count++;
  61. i+=2;
  62. }
  63. }
  64. if(escape_count==0)
  65. {
  66. if(dest+destpos != rbsp)
  67. {
  68. memcpy(dest+destpos, rbsp, rbsplen);
  69. *destsize -= (rbsplen+destpos);
  70. }
  71. return dest+rbsplen+destpos;
  72. }
  73. if(rbsplen + escape_count + 1> *destsize)
  74. {
  75. av_log(NULL, AV_LOG_ERROR, "Destination buffer too small!\n");
  76. return NULL;
  77. }
  78. // this should be damn rare (hopefully)
  79. for (i = 0 ; i < rbsplen ; i++)
  80. {
  81. if (i + 2 < rbsplen && (rbsp[i] == 0 && rbsp[i+1] == 0 && rbsp[i+2] < 4))
  82. {
  83. dest[destpos++] = rbsp[i++];
  84. dest[destpos++] = rbsp[i];
  85. dest[destpos++] = 0x03; // emulation prevention byte
  86. }
  87. else
  88. dest[destpos++] = rbsp[i];
  89. }
  90. *destsize -= destpos;
  91. return dest+destpos;
  92. }