avc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "avc.h"
  25. static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
  26. {
  27. const uint8_t *a = p + 4 - ((intptr_t)p & 3);
  28. for (end -= 3; p < a && p < end; p++) {
  29. if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  30. return p;
  31. }
  32. for (end -= 3; p < end; p += 4) {
  33. uint32_t x = *(const uint32_t*)p;
  34. // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
  35. // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
  36. if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
  37. if (p[1] == 0) {
  38. if (p[0] == 0 && p[2] == 1)
  39. return p;
  40. if (p[2] == 0 && p[3] == 1)
  41. return p+1;
  42. }
  43. if (p[3] == 0) {
  44. if (p[2] == 0 && p[4] == 1)
  45. return p+2;
  46. if (p[4] == 0 && p[5] == 1)
  47. return p+3;
  48. }
  49. }
  50. }
  51. for (end += 3; p < end; p++) {
  52. if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  53. return p;
  54. }
  55. return end + 3;
  56. }
  57. const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
  58. const uint8_t *out= ff_avc_find_startcode_internal(p, end);
  59. if(p<out && out<end && !out[-1]) out--;
  60. return out;
  61. }
  62. int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
  63. {
  64. const uint8_t *p = buf_in;
  65. const uint8_t *end = p + size;
  66. const uint8_t *nal_start, *nal_end;
  67. size = 0;
  68. nal_start = ff_avc_find_startcode(p, end);
  69. for (;;) {
  70. while (nal_start < end && !*(nal_start++));
  71. if (nal_start == end)
  72. break;
  73. nal_end = ff_avc_find_startcode(nal_start, end);
  74. avio_wb32(pb, nal_end - nal_start);
  75. avio_write(pb, nal_start, nal_end - nal_start);
  76. size += 4 + nal_end - nal_start;
  77. nal_start = nal_end;
  78. }
  79. return size;
  80. }
  81. int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
  82. {
  83. AVIOContext *pb;
  84. int ret = avio_open_dyn_buf(&pb);
  85. if(ret < 0)
  86. return ret;
  87. ff_avc_parse_nal_units(pb, buf_in, *size);
  88. av_freep(buf);
  89. *size = avio_close_dyn_buf(pb, buf);
  90. return 0;
  91. }
  92. int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
  93. {
  94. if (len > 6) {
  95. /* check for h264 start code */
  96. if (AV_RB32(data) == 0x00000001 ||
  97. AV_RB24(data) == 0x000001) {
  98. uint8_t *buf=NULL, *end, *start;
  99. uint32_t sps_size=0, pps_size=0;
  100. uint8_t *sps=0, *pps=0;
  101. int ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
  102. if (ret < 0)
  103. return ret;
  104. start = buf;
  105. end = buf + len;
  106. /* look for sps and pps */
  107. while (end - buf > 4) {
  108. uint32_t size;
  109. uint8_t nal_type;
  110. size = FFMIN(AV_RB32(buf), end - buf - 4);
  111. buf += 4;
  112. nal_type = buf[0] & 0x1f;
  113. if (nal_type == 7) { /* SPS */
  114. sps = buf;
  115. sps_size = size;
  116. } else if (nal_type == 8) { /* PPS */
  117. pps = buf;
  118. pps_size = size;
  119. }
  120. buf += size;
  121. }
  122. if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
  123. return AVERROR_INVALIDDATA;
  124. avio_w8(pb, 1); /* version */
  125. avio_w8(pb, sps[1]); /* profile */
  126. avio_w8(pb, sps[2]); /* profile compat */
  127. avio_w8(pb, sps[3]); /* level */
  128. avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
  129. avio_w8(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
  130. avio_wb16(pb, sps_size);
  131. avio_write(pb, sps, sps_size);
  132. avio_w8(pb, 1); /* number of pps */
  133. avio_wb16(pb, pps_size);
  134. avio_write(pb, pps, pps_size);
  135. av_free(start);
  136. } else {
  137. avio_write(pb, data, len);
  138. }
  139. }
  140. return 0;
  141. }
  142. int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
  143. {
  144. uint16_t sps_size, pps_size;
  145. uint8_t *out;
  146. int out_size;
  147. *buf = NULL;
  148. if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
  149. return 0;
  150. if (*size < 11 || in[0] != 1)
  151. return AVERROR_INVALIDDATA;
  152. sps_size = AV_RB16(&in[6]);
  153. if (11 + sps_size > *size)
  154. return AVERROR_INVALIDDATA;
  155. pps_size = AV_RB16(&in[9 + sps_size]);
  156. if (11 + sps_size + pps_size > *size)
  157. return AVERROR_INVALIDDATA;
  158. out_size = 8 + sps_size + pps_size;
  159. out = av_mallocz(out_size);
  160. if (!out)
  161. return AVERROR(ENOMEM);
  162. AV_WB32(&out[0], 0x00000001);
  163. memcpy(out + 4, &in[8], sps_size);
  164. AV_WB32(&out[4 + sps_size], 0x00000001);
  165. memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
  166. *buf = out;
  167. *size = out_size;
  168. return 0;
  169. }