avc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * AVC helper functions for muxers
  3. * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.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 "libavutil/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "avio.h"
  24. const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end)
  25. {
  26. const uint8_t *a = p + 4 - ((long)p & 3);
  27. for( end -= 3; p < a && p < end; p++ ) {
  28. if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
  29. return p;
  30. }
  31. for( end -= 3; p < end; p += 4 ) {
  32. uint32_t x = *(const uint32_t*)p;
  33. // if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
  34. // if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
  35. if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
  36. if( p[1] == 0 ) {
  37. if( p[0] == 0 && p[2] == 1 )
  38. return p-1;
  39. if( p[2] == 0 && p[3] == 1 )
  40. return p;
  41. }
  42. if( p[3] == 0 ) {
  43. if( p[2] == 0 && p[4] == 1 )
  44. return p+1;
  45. if( p[4] == 0 && p[5] == 1 )
  46. return p+2;
  47. }
  48. }
  49. }
  50. for( end += 3; p < end; p++ ) {
  51. if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
  52. return p;
  53. }
  54. return end + 3;
  55. }
  56. int ff_avc_parse_nal_units(ByteIOContext *pb, const uint8_t *buf_in, int size)
  57. {
  58. const uint8_t *p = buf_in;
  59. const uint8_t *end = p + size;
  60. const uint8_t *nal_start, *nal_end;
  61. size = 0;
  62. nal_start = ff_avc_find_startcode(p, end);
  63. while (nal_start < end) {
  64. while(!*(nal_start++));
  65. nal_end = ff_avc_find_startcode(nal_start, end);
  66. put_be32(pb, nal_end - nal_start);
  67. put_buffer(pb, nal_start, nal_end - nal_start);
  68. size += 4 + nal_end - nal_start;
  69. nal_start = nal_end;
  70. }
  71. return size;
  72. }
  73. int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
  74. {
  75. ByteIOContext *pb;
  76. int ret = url_open_dyn_buf(&pb);
  77. if(ret < 0)
  78. return ret;
  79. ff_avc_parse_nal_units(pb, buf_in, *size);
  80. av_freep(buf);
  81. *size = url_close_dyn_buf(pb, buf);
  82. return 0;
  83. }
  84. int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t *data, int len)
  85. {
  86. if (len > 6) {
  87. /* check for h264 start code */
  88. if (AV_RB32(data) == 0x00000001 ||
  89. AV_RB24(data) == 0x000001) {
  90. uint8_t *buf=NULL, *end, *start;
  91. uint32_t sps_size=0, pps_size=0;
  92. uint8_t *sps=0, *pps=0;
  93. int ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
  94. if (ret < 0)
  95. return ret;
  96. start = buf;
  97. end = buf + len;
  98. /* look for sps and pps */
  99. while (buf < end) {
  100. unsigned int size;
  101. uint8_t nal_type;
  102. size = AV_RB32(buf);
  103. nal_type = buf[4] & 0x1f;
  104. if (nal_type == 7) { /* SPS */
  105. sps = buf + 4;
  106. sps_size = size;
  107. } else if (nal_type == 8) { /* PPS */
  108. pps = buf + 4;
  109. pps_size = size;
  110. }
  111. buf += size + 4;
  112. }
  113. assert(sps);
  114. assert(pps);
  115. put_byte(pb, 1); /* version */
  116. put_byte(pb, sps[1]); /* profile */
  117. put_byte(pb, sps[2]); /* profile compat */
  118. put_byte(pb, sps[3]); /* level */
  119. put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
  120. put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
  121. put_be16(pb, sps_size);
  122. put_buffer(pb, sps, sps_size);
  123. put_byte(pb, 1); /* number of pps */
  124. put_be16(pb, pps_size);
  125. put_buffer(pb, pps, pps_size);
  126. av_free(start);
  127. } else {
  128. put_buffer(pb, data, len);
  129. }
  130. }
  131. return 0;
  132. }