1
0

obs-nal.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /******************************************************************************
  2. Copyright (C) 2022 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "obs-nal.h"
  15. /* NOTE: I noticed that FFmpeg does some unusual special handling of certain
  16. * scenarios that I was unaware of, so instead of just searching for {0, 0, 1}
  17. * we'll just use the code from FFmpeg - http://www.ffmpeg.org/ */
  18. static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p,
  19. const uint8_t *end)
  20. {
  21. const uint8_t *a = p + 4 - ((intptr_t)p & 3);
  22. for (end -= 3; p < a && p < end; p++) {
  23. if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  24. return p;
  25. }
  26. for (end -= 3; p < end; p += 4) {
  27. uint32_t x = *(const uint32_t *)p;
  28. if ((x - 0x01010101) & (~x) & 0x80808080) {
  29. if (p[1] == 0) {
  30. if (p[0] == 0 && p[2] == 1)
  31. return p;
  32. if (p[2] == 0 && p[3] == 1)
  33. return p + 1;
  34. }
  35. if (p[3] == 0) {
  36. if (p[2] == 0 && p[4] == 1)
  37. return p + 2;
  38. if (p[4] == 0 && p[5] == 1)
  39. return p + 3;
  40. }
  41. }
  42. }
  43. for (end += 3; p < end; p++) {
  44. if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  45. return p;
  46. }
  47. return end + 3;
  48. }
  49. const uint8_t *obs_nal_find_startcode(const uint8_t *p, const uint8_t *end)
  50. {
  51. const uint8_t *out = ff_avc_find_startcode_internal(p, end);
  52. if (p < out && out < end && !out[-1])
  53. out--;
  54. return out;
  55. }