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.

452 lines
14KB

  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 "libavcodec/h264.h"
  23. #include "libavcodec/get_bits.h"
  24. #include "avformat.h"
  25. #include "avio.h"
  26. #include "avc.h"
  27. #include "avio_internal.h"
  28. static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
  29. {
  30. const uint8_t *a = p + 4 - ((intptr_t)p & 3);
  31. for (end -= 3; p < a && p < end; p++) {
  32. if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  33. return p;
  34. }
  35. for (end -= 3; p < end; p += 4) {
  36. uint32_t x = *(const uint32_t*)p;
  37. // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
  38. // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
  39. if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
  40. if (p[1] == 0) {
  41. if (p[0] == 0 && p[2] == 1)
  42. return p;
  43. if (p[2] == 0 && p[3] == 1)
  44. return p+1;
  45. }
  46. if (p[3] == 0) {
  47. if (p[2] == 0 && p[4] == 1)
  48. return p+2;
  49. if (p[4] == 0 && p[5] == 1)
  50. return p+3;
  51. }
  52. }
  53. }
  54. for (end += 3; p < end; p++) {
  55. if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  56. return p;
  57. }
  58. return end + 3;
  59. }
  60. const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
  61. const uint8_t *out= ff_avc_find_startcode_internal(p, end);
  62. if(p<out && out<end && !out[-1]) out--;
  63. return out;
  64. }
  65. int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
  66. {
  67. const uint8_t *p = buf_in;
  68. const uint8_t *end = p + size;
  69. const uint8_t *nal_start, *nal_end;
  70. size = 0;
  71. nal_start = ff_avc_find_startcode(p, end);
  72. for (;;) {
  73. while (nal_start < end && !*(nal_start++));
  74. if (nal_start == end)
  75. break;
  76. nal_end = ff_avc_find_startcode(nal_start, end);
  77. avio_wb32(pb, nal_end - nal_start);
  78. avio_write(pb, nal_start, nal_end - nal_start);
  79. size += 4 + nal_end - nal_start;
  80. nal_start = nal_end;
  81. }
  82. return size;
  83. }
  84. int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
  85. {
  86. AVIOContext *pb;
  87. int ret = avio_open_dyn_buf(&pb);
  88. if(ret < 0)
  89. return ret;
  90. ff_avc_parse_nal_units(pb, buf_in, *size);
  91. *size = avio_close_dyn_buf(pb, buf);
  92. return 0;
  93. }
  94. int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
  95. {
  96. AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL;
  97. uint8_t *buf, *end, *start;
  98. uint8_t *sps, *pps, *sps_ext;
  99. uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0;
  100. int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0;
  101. if (len <= 6)
  102. return AVERROR_INVALIDDATA;
  103. /* check for H.264 start code */
  104. if (AV_RB32(data) != 0x00000001 &&
  105. AV_RB24(data) != 0x000001) {
  106. avio_write(pb, data, len);
  107. return 0;
  108. }
  109. ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
  110. if (ret < 0)
  111. return ret;
  112. start = buf;
  113. end = buf + len;
  114. ret = avio_open_dyn_buf(&sps_pb);
  115. if (ret < 0)
  116. goto fail;
  117. ret = avio_open_dyn_buf(&pps_pb);
  118. if (ret < 0)
  119. goto fail;
  120. ret = avio_open_dyn_buf(&sps_ext_pb);
  121. if (ret < 0)
  122. goto fail;
  123. /* look for sps and pps */
  124. while (end - buf > 4) {
  125. uint32_t size;
  126. uint8_t nal_type;
  127. size = FFMIN(AV_RB32(buf), end - buf - 4);
  128. buf += 4;
  129. nal_type = buf[0] & 0x1f;
  130. if (nal_type == 7) { /* SPS */
  131. nb_sps++;
  132. if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) {
  133. ret = AVERROR_INVALIDDATA;
  134. goto fail;
  135. }
  136. avio_wb16(sps_pb, size);
  137. avio_write(sps_pb, buf, size);
  138. } else if (nal_type == 8) { /* PPS */
  139. nb_pps++;
  140. if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) {
  141. ret = AVERROR_INVALIDDATA;
  142. goto fail;
  143. }
  144. avio_wb16(pps_pb, size);
  145. avio_write(pps_pb, buf, size);
  146. } else if (nal_type == 13) { /* SPS_EXT */
  147. nb_sps_ext++;
  148. if (size > UINT16_MAX || nb_sps_ext >= 256) {
  149. ret = AVERROR_INVALIDDATA;
  150. goto fail;
  151. }
  152. avio_wb16(sps_ext_pb, size);
  153. avio_write(sps_ext_pb, buf, size);
  154. }
  155. buf += size;
  156. }
  157. sps_size = avio_get_dyn_buf(sps_pb, &sps);
  158. pps_size = avio_get_dyn_buf(pps_pb, &pps);
  159. sps_ext_size = avio_get_dyn_buf(sps_ext_pb, &sps_ext);
  160. if (sps_size < 6 || !pps_size) {
  161. ret = AVERROR_INVALIDDATA;
  162. goto fail;
  163. }
  164. avio_w8(pb, 1); /* version */
  165. avio_w8(pb, sps[3]); /* profile */
  166. avio_w8(pb, sps[4]); /* profile compat */
  167. avio_w8(pb, sps[5]); /* level */
  168. avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
  169. avio_w8(pb, 0xe0 | nb_sps); /* 3 bits reserved (111) + 5 bits number of sps */
  170. avio_write(pb, sps, sps_size);
  171. avio_w8(pb, nb_pps); /* number of pps */
  172. avio_write(pb, pps, pps_size);
  173. if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) {
  174. H264SPS seq;
  175. ret = ff_avc_decode_sps(&seq, sps + 3, sps_size - 3);
  176. if (ret < 0)
  177. goto fail;
  178. avio_w8(pb, 0xfc | seq.chroma_format_idc); /* 6 bits reserved (111111) + chroma_format_idc */
  179. avio_w8(pb, 0xf8 | (seq.bit_depth_luma - 8)); /* 5 bits reserved (11111) + bit_depth_luma_minus8 */
  180. avio_w8(pb, 0xf8 | (seq.bit_depth_chroma - 8)); /* 5 bits reserved (11111) + bit_depth_chroma_minus8 */
  181. avio_w8(pb, nb_sps_ext); /* number of sps ext */
  182. if (nb_sps_ext)
  183. avio_write(pb, sps_ext, sps_ext_size);
  184. }
  185. fail:
  186. ffio_free_dyn_buf(&sps_pb);
  187. ffio_free_dyn_buf(&pps_pb);
  188. ffio_free_dyn_buf(&sps_ext_pb);
  189. av_free(start);
  190. return ret;
  191. }
  192. int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
  193. {
  194. uint16_t sps_size, pps_size;
  195. uint8_t *out;
  196. int out_size;
  197. *buf = NULL;
  198. if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
  199. return 0;
  200. if (*size < 11 || in[0] != 1)
  201. return AVERROR_INVALIDDATA;
  202. sps_size = AV_RB16(&in[6]);
  203. if (11 + sps_size > *size)
  204. return AVERROR_INVALIDDATA;
  205. pps_size = AV_RB16(&in[9 + sps_size]);
  206. if (11 + sps_size + pps_size > *size)
  207. return AVERROR_INVALIDDATA;
  208. out_size = 8 + sps_size + pps_size;
  209. out = av_mallocz(out_size + AV_INPUT_BUFFER_PADDING_SIZE);
  210. if (!out)
  211. return AVERROR(ENOMEM);
  212. AV_WB32(&out[0], 0x00000001);
  213. memcpy(out + 4, &in[8], sps_size);
  214. AV_WB32(&out[4 + sps_size], 0x00000001);
  215. memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
  216. *buf = out;
  217. *size = out_size;
  218. return 0;
  219. }
  220. const uint8_t *ff_avc_mp4_find_startcode(const uint8_t *start,
  221. const uint8_t *end,
  222. int nal_length_size)
  223. {
  224. unsigned int res = 0;
  225. if (end - start < nal_length_size)
  226. return NULL;
  227. while (nal_length_size--)
  228. res = (res << 8) | *start++;
  229. if (res > end - start)
  230. return NULL;
  231. return start + res;
  232. }
  233. uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
  234. uint32_t *dst_len, int header_len)
  235. {
  236. uint8_t *dst;
  237. uint32_t i, len;
  238. dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
  239. if (!dst)
  240. return NULL;
  241. /* NAL unit header */
  242. i = len = 0;
  243. while (i < header_len && i < src_len)
  244. dst[len++] = src[i++];
  245. while (i + 2 < src_len)
  246. if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
  247. dst[len++] = src[i++];
  248. dst[len++] = src[i++];
  249. i++; // remove emulation_prevention_three_byte
  250. } else
  251. dst[len++] = src[i++];
  252. while (i < src_len)
  253. dst[len++] = src[i++];
  254. memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  255. *dst_len = len;
  256. return dst;
  257. }
  258. static const AVRational avc_sample_aspect_ratio[17] = {
  259. { 0, 1 },
  260. { 1, 1 },
  261. { 12, 11 },
  262. { 10, 11 },
  263. { 16, 11 },
  264. { 40, 33 },
  265. { 24, 11 },
  266. { 20, 11 },
  267. { 32, 11 },
  268. { 80, 33 },
  269. { 18, 11 },
  270. { 15, 11 },
  271. { 64, 33 },
  272. { 160, 99 },
  273. { 4, 3 },
  274. { 3, 2 },
  275. { 2, 1 },
  276. };
  277. static inline int get_ue_golomb(GetBitContext *gb) {
  278. int i;
  279. for (i = 0; i < 32 && !get_bits1(gb); i++)
  280. ;
  281. return get_bitsz(gb, i) + (1 << i) - 1;
  282. }
  283. static inline int get_se_golomb(GetBitContext *gb) {
  284. int v = get_ue_golomb(gb) + 1;
  285. int sign = -(v & 1);
  286. return ((v >> 1) ^ sign) - sign;
  287. }
  288. int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
  289. {
  290. int i, j, ret, rbsp_size, aspect_ratio_idc, pic_order_cnt_type;
  291. int num_ref_frames_in_pic_order_cnt_cycle;
  292. int delta_scale, lastScale = 8, nextScale = 8;
  293. int sizeOfScalingList;
  294. GetBitContext gb;
  295. uint8_t *rbsp_buf;
  296. rbsp_buf = ff_nal_unit_extract_rbsp(buf, buf_size, &rbsp_size, 0);
  297. if (!rbsp_buf)
  298. return AVERROR(ENOMEM);
  299. ret = init_get_bits8(&gb, rbsp_buf, rbsp_size);
  300. if (ret < 0)
  301. goto end;
  302. memset(sps, 0, sizeof(*sps));
  303. sps->profile_idc = get_bits(&gb, 8);
  304. sps->constraint_set_flags |= get_bits1(&gb) << 0; // constraint_set0_flag
  305. sps->constraint_set_flags |= get_bits1(&gb) << 1; // constraint_set1_flag
  306. sps->constraint_set_flags |= get_bits1(&gb) << 2; // constraint_set2_flag
  307. sps->constraint_set_flags |= get_bits1(&gb) << 3; // constraint_set3_flag
  308. sps->constraint_set_flags |= get_bits1(&gb) << 4; // constraint_set4_flag
  309. sps->constraint_set_flags |= get_bits1(&gb) << 5; // constraint_set5_flag
  310. skip_bits(&gb, 2); // reserved_zero_2bits
  311. sps->level_idc = get_bits(&gb, 8);
  312. sps->id = get_ue_golomb(&gb);
  313. if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
  314. sps->profile_idc == 122 || sps->profile_idc == 244 || sps->profile_idc == 44 ||
  315. sps->profile_idc == 83 || sps->profile_idc == 86 || sps->profile_idc == 118 ||
  316. sps->profile_idc == 128 || sps->profile_idc == 138 || sps->profile_idc == 139 ||
  317. sps->profile_idc == 134) {
  318. sps->chroma_format_idc = get_ue_golomb(&gb); // chroma_format_idc
  319. if (sps->chroma_format_idc == 3) {
  320. skip_bits1(&gb); // separate_colour_plane_flag
  321. }
  322. sps->bit_depth_luma = get_ue_golomb(&gb) + 8;
  323. sps->bit_depth_chroma = get_ue_golomb(&gb) + 8;
  324. skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag
  325. if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag
  326. for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) {
  327. if (!get_bits1(&gb)) // seq_scaling_list_present_flag
  328. continue;
  329. lastScale = 8;
  330. nextScale = 8;
  331. sizeOfScalingList = i < 6 ? 16 : 64;
  332. for (j = 0; j < sizeOfScalingList; j++) {
  333. if (nextScale != 0) {
  334. delta_scale = get_se_golomb(&gb);
  335. nextScale = (lastScale + delta_scale) & 0xff;
  336. }
  337. lastScale = nextScale == 0 ? lastScale : nextScale;
  338. }
  339. }
  340. }
  341. } else {
  342. sps->chroma_format_idc = 1;
  343. sps->bit_depth_luma = 8;
  344. sps->bit_depth_chroma = 8;
  345. }
  346. get_ue_golomb(&gb); // log2_max_frame_num_minus4
  347. pic_order_cnt_type = get_ue_golomb(&gb);
  348. if (pic_order_cnt_type == 0) {
  349. get_ue_golomb(&gb); // log2_max_pic_order_cnt_lsb_minus4
  350. } else if (pic_order_cnt_type == 1) {
  351. skip_bits1(&gb); // delta_pic_order_always_zero
  352. get_se_golomb(&gb); // offset_for_non_ref_pic
  353. get_se_golomb(&gb); // offset_for_top_to_bottom_field
  354. num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb(&gb);
  355. for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++)
  356. get_se_golomb(&gb); // offset_for_ref_frame
  357. }
  358. get_ue_golomb(&gb); // max_num_ref_frames
  359. skip_bits1(&gb); // gaps_in_frame_num_value_allowed_flag
  360. get_ue_golomb(&gb); // pic_width_in_mbs_minus1
  361. get_ue_golomb(&gb); // pic_height_in_map_units_minus1
  362. sps->frame_mbs_only_flag = get_bits1(&gb);
  363. if (!sps->frame_mbs_only_flag)
  364. skip_bits1(&gb); // mb_adaptive_frame_field_flag
  365. skip_bits1(&gb); // direct_8x8_inference_flag
  366. if (get_bits1(&gb)) { // frame_cropping_flag
  367. get_ue_golomb(&gb); // frame_crop_left_offset
  368. get_ue_golomb(&gb); // frame_crop_right_offset
  369. get_ue_golomb(&gb); // frame_crop_top_offset
  370. get_ue_golomb(&gb); // frame_crop_bottom_offset
  371. }
  372. if (get_bits1(&gb)) { // vui_parameters_present_flag
  373. if (get_bits1(&gb)) { // aspect_ratio_info_present_flag
  374. aspect_ratio_idc = get_bits(&gb, 8);
  375. if (aspect_ratio_idc == 0xff) {
  376. sps->sar.num = get_bits(&gb, 16);
  377. sps->sar.den = get_bits(&gb, 16);
  378. } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(avc_sample_aspect_ratio)) {
  379. sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc];
  380. }
  381. }
  382. }
  383. if (!sps->sar.den) {
  384. sps->sar.num = 1;
  385. sps->sar.den = 1;
  386. }
  387. ret = 0;
  388. end:
  389. av_free(rbsp_buf);
  390. return ret;
  391. }