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.

69 lines
1.9KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include "vaapi_encode_h26x.h"
  20. int ff_vaapi_encode_h26x_nal_unit_to_byte_stream(uint8_t *dst, size_t *dst_bit_len,
  21. uint8_t *src, size_t src_bit_len)
  22. {
  23. size_t dp, sp;
  24. int zero_run = 0;
  25. size_t dst_len = *dst_bit_len / 8;
  26. size_t src_len = (src_bit_len + 7) / 8;
  27. int trailing_zeroes = src_len * 8 - src_bit_len;
  28. if (dst_len < src_len + 4) {
  29. // Definitely doesn't fit.
  30. goto fail;
  31. }
  32. // Start code.
  33. dst[0] = dst[1] = dst[2] = 0;
  34. dst[3] = 1;
  35. dp = 4;
  36. for (sp = 0; sp < src_len; sp++) {
  37. if (dp >= dst_len)
  38. goto fail;
  39. if (zero_run < 2) {
  40. if (src[sp] == 0)
  41. ++zero_run;
  42. else
  43. zero_run = 0;
  44. } else {
  45. if ((src[sp] & ~3) == 0) {
  46. // emulation_prevention_three_byte
  47. dst[dp++] = 3;
  48. if (dp >= dst_len)
  49. goto fail;
  50. }
  51. zero_run = src[sp] == 0;
  52. }
  53. dst[dp++] = src[sp];
  54. }
  55. *dst_bit_len = 8 * dp - trailing_zeroes;
  56. return 0;
  57. fail:
  58. *dst_bit_len = 0;
  59. return AVERROR(ENOSPC);
  60. }