PageRenderTime 77ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 2ms

/src/media_tools/av_parsers.c

https://github.com/svettom/gpac
C | 4542 lines | 3823 code | 483 blank | 236 comment | 965 complexity | 4d74b05e1a383deca7966dc5f407e1fd MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * GPAC - Multimedia Framework C SDK
  3. *
  4. * Authors: Jean Le Feuvre, Romain Bouqueau, Cyril Concolato
  5. * Copyright (c) Telecom ParisTech 2000-2012
  6. * All rights reserved
  7. *
  8. * This file is part of GPAC / Media Tools sub-project
  9. *
  10. * GPAC is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. *
  15. * GPAC is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. */
  25. #include "../../include/gpac/internal/media_dev.h"
  26. #include "../../include/gpac/constants.h"
  27. #include "../../include/gpac/mpeg4_odf.h"
  28. #ifndef GPAC_DISABLE_OGG
  29. #include "../../include/gpac/internal/ogg.h"
  30. #include "../../include/gpac/math.h"
  31. #endif
  32. static const struct { u32 w, h; } std_par[ ] =
  33. {
  34. { 4, 3}, {3, 2}, {16, 9}, {5, 3}, {5, 4}, {8, 5},
  35. {0, 0},
  36. };
  37. GF_EXPORT
  38. void gf_media_reduce_aspect_ratio(u32 *width, u32 *height)
  39. {
  40. u32 i=0;
  41. u32 w = *width;
  42. u32 h = *height;
  43. while (std_par[i].w) {
  44. if (std_par[i].w * h == std_par[i].h * w) {
  45. *width = std_par[i].w;
  46. *height = std_par[i].h;
  47. return;
  48. }
  49. i++;
  50. }
  51. }
  52. GF_EXPORT
  53. void gf_media_get_reduced_frame_rate(u32 *timescale, u32 *sample_dur)
  54. {
  55. u32 res;
  56. if (! *sample_dur) return;
  57. res = *timescale / *sample_dur;
  58. if (res * *sample_dur == *timescale) {
  59. *timescale = res;
  60. *sample_dur = 1;
  61. }
  62. }
  63. GF_EXPORT
  64. const char *gf_m4v_get_profile_name(u8 video_pl)
  65. {
  66. switch (video_pl) {
  67. case 0x00: return "Reserved (0x00) Profile";
  68. case 0x01: return "Simple Profile @ Level 1";
  69. case 0x02: return "Simple Profile @ Level 2";
  70. case 0x03: return "Simple Profile @ Level 3";
  71. case 0x08: return "Simple Profile @ Level 0";
  72. case 0x10: return "Simple Scalable Profile @ Level 0";
  73. case 0x11: return "Simple Scalable Profile @ Level 1";
  74. case 0x12: return "Simple Scalable Profile @ Level 2";
  75. case 0x15: return "AVC/H264 Profile";
  76. case 0x21: return "Core Profile @ Level 1";
  77. case 0x22: return "Core Profile @ Level 2";
  78. case 0x32: return "Main Profile @ Level 2";
  79. case 0x33: return "Main Profile @ Level 3";
  80. case 0x34: return "Main Profile @ Level 4";
  81. case 0x42: return "N-bit Profile @ Level 2";
  82. case 0x51: return "Scalable Texture Profile @ Level 1";
  83. case 0x61: return "Simple Face Animation Profile @ Level 1";
  84. case 0x62: return "Simple Face Animation Profile @ Level 2";
  85. case 0x63: return "Simple FBA Profile @ Level 1";
  86. case 0x64: return "Simple FBA Profile @ Level 2";
  87. case 0x71: return "Basic Animated Texture Profile @ Level 1";
  88. case 0x72: return "Basic Animated Texture Profile @ Level 2";
  89. case 0x81: return "Hybrid Profile @ Level 1";
  90. case 0x82: return "Hybrid Profile @ Level 2";
  91. case 0x91: return "Advanced Real Time Simple Profile @ Level 1";
  92. case 0x92: return "Advanced Real Time Simple Profile @ Level 2";
  93. case 0x93: return "Advanced Real Time Simple Profile @ Level 3";
  94. case 0x94: return "Advanced Real Time Simple Profile @ Level 4";
  95. case 0xA1: return "Core Scalable Profile @ Level1";
  96. case 0xA2: return "Core Scalable Profile @ Level2";
  97. case 0xA3: return "Core Scalable Profile @ Level3";
  98. case 0xB1: return "Advanced Coding Efficiency Profile @ Level 1";
  99. case 0xB2: return "Advanced Coding Efficiency Profile @ Level 2";
  100. case 0xB3: return "Advanced Coding Efficiency Profile @ Level 3";
  101. case 0xB4: return "Advanced Coding Efficiency Profile @ Level 4";
  102. case 0xC1: return "Advanced Core Profile @ Level 1";
  103. case 0xC2: return "Advanced Core Profile @ Level 2";
  104. case 0xD1: return "Advanced Scalable Texture @ Level1";
  105. case 0xD2: return "Advanced Scalable Texture @ Level2";
  106. case 0xE1: return "Simple Studio Profile @ Level 1";
  107. case 0xE2: return "Simple Studio Profile @ Level 2";
  108. case 0xE3: return "Simple Studio Profile @ Level 3";
  109. case 0xE4: return "Simple Studio Profile @ Level 4";
  110. case 0xE5: return "Core Studio Profile @ Level 1";
  111. case 0xE6: return "Core Studio Profile @ Level 2";
  112. case 0xE7: return "Core Studio Profile @ Level 3";
  113. case 0xE8: return "Core Studio Profile @ Level 4";
  114. case 0xF0: return "Advanced Simple Profile @ Level 0";
  115. case 0xF1: return "Advanced Simple Profile @ Level 1";
  116. case 0xF2: return "Advanced Simple Profile @ Level 2";
  117. case 0xF3: return "Advanced Simple Profile @ Level 3";
  118. case 0xF4: return "Advanced Simple Profile @ Level 4";
  119. case 0xF5: return "Advanced Simple Profile @ Level 5";
  120. case 0xF7: return "Advanced Simple Profile @ Level 3b";
  121. case 0xF8: return "Fine Granularity Scalable Profile @ Level 0";
  122. case 0xF9: return "Fine Granularity Scalable Profile @ Level 1";
  123. case 0xFA: return "Fine Granularity Scalable Profile @ Level 2";
  124. case 0xFB: return "Fine Granularity Scalable Profile @ Level 3";
  125. case 0xFC: return "Fine Granularity Scalable Profile @ Level 4";
  126. case 0xFD: return "Fine Granularity Scalable Profile @ Level 5";
  127. case 0xFE: return "Not part of MPEG-4 Visual profiles";
  128. case 0xFF: return "No visual capability required";
  129. default: return "ISO Reserved Profile";
  130. }
  131. }
  132. #ifndef GPAC_DISABLE_AV_PARSERS
  133. #define MPEG12_START_CODE_PREFIX 0x000001
  134. #define MPEG12_PICTURE_START_CODE 0x00000100
  135. #define MPEG12_SLICE_MIN_START 0x00000101
  136. #define MPEG12_SLICE_MAX_START 0x000001af
  137. #define MPEG12_USER_DATA_START_CODE 0x000001b2
  138. #define MPEG12_SEQUENCE_START_CODE 0x000001b3
  139. #define MPEG12_SEQUENCE_ERR_START_CODE 0x000001b4
  140. #define MPEG12_EXT_START_CODE 0x000001b5
  141. #define MPEG12_SEQUENCE_END_START_CODE 0x000001b7
  142. #define MPEG12_GOP_START_CODE 0x000001b8
  143. s32 gf_mv12_next_start_code(unsigned char *pbuffer, u32 buflen, u32 *optr, u32 *scode)
  144. {
  145. u32 value;
  146. u32 offset;
  147. if (buflen < 4) return -1;
  148. for (offset = 0; offset < buflen - 3; offset++, pbuffer++) {
  149. #ifdef GPAC_BIG_ENDIAN
  150. value = *(u32 *)pbuffer >> 8;
  151. #else
  152. value = (pbuffer[0] << 16) | (pbuffer[1] << 8) | (pbuffer[2] << 0);
  153. #endif
  154. if (value == MPEG12_START_CODE_PREFIX) {
  155. *optr = offset;
  156. *scode = (value << 8) | pbuffer[3];
  157. return 0;
  158. }
  159. }
  160. return -1;
  161. }
  162. s32 gf_mv12_next_slice_start(unsigned char *pbuffer, u32 startoffset, u32 buflen, u32 *slice_offset)
  163. {
  164. u32 slicestart, code;
  165. while (gf_mv12_next_start_code(pbuffer + startoffset, buflen - startoffset, &slicestart, &code) >= 0) {
  166. if ((code >= MPEG12_SLICE_MIN_START) && (code <= MPEG12_SLICE_MAX_START)) {
  167. *slice_offset = slicestart + startoffset;
  168. return 0;
  169. }
  170. startoffset += slicestart + 4;
  171. }
  172. return -1;
  173. }
  174. /*
  175. MPEG-4 video (14496-2)
  176. */
  177. #define M4V_VO_START_CODE 0x00
  178. #define M4V_VOL_START_CODE 0x20
  179. #define M4V_VOP_START_CODE 0xB6
  180. #define M4V_VISOBJ_START_CODE 0xB5
  181. #define M4V_VOS_START_CODE 0xB0
  182. #define M4V_GOV_START_CODE 0xB3
  183. #define M4V_UDTA_START_CODE 0xB2
  184. #define M2V_PIC_START_CODE 0x00
  185. #define M2V_SEQ_START_CODE 0xB3
  186. #define M2V_EXT_START_CODE 0xB5
  187. #define M2V_GOP_START_CODE 0xB8
  188. struct __tag_m4v_parser
  189. {
  190. GF_BitStream *bs;
  191. Bool mpeg12;
  192. u32 current_object_type;
  193. u64 current_object_start;
  194. u32 tc_dec, prev_tc_dec, tc_disp, prev_tc_disp;
  195. };
  196. GF_EXPORT
  197. GF_M4VParser *gf_m4v_parser_new(char *data, u64 data_size, Bool mpeg12video)
  198. {
  199. GF_M4VParser *tmp;
  200. if (!data || !data_size) return NULL;
  201. GF_SAFEALLOC(tmp, GF_M4VParser);
  202. tmp->bs = gf_bs_new(data, data_size, GF_BITSTREAM_READ);
  203. tmp->mpeg12 = mpeg12video;
  204. return tmp;
  205. }
  206. GF_M4VParser *gf_m4v_parser_bs_new(GF_BitStream *bs, Bool mpeg12video)
  207. {
  208. GF_M4VParser *tmp;
  209. GF_SAFEALLOC(tmp, GF_M4VParser);
  210. tmp->bs = bs;
  211. tmp->mpeg12 = mpeg12video;
  212. return tmp;
  213. }
  214. GF_EXPORT
  215. void gf_m4v_parser_del(GF_M4VParser *m4v)
  216. {
  217. gf_bs_del(m4v->bs);
  218. gf_free(m4v);
  219. }
  220. #define M4V_CACHE_SIZE 4096
  221. s32 M4V_LoadObject(GF_M4VParser *m4v)
  222. {
  223. u32 v, bpos, found;
  224. char m4v_cache[M4V_CACHE_SIZE];
  225. u64 end, cache_start, load_size;
  226. if (!m4v) return 0;
  227. bpos = 0;
  228. found = 0;
  229. load_size = 0;
  230. end = 0;
  231. cache_start = 0;
  232. v = 0xffffffff;
  233. while (!end) {
  234. /*refill cache*/
  235. if (bpos == (u32) load_size) {
  236. if (!gf_bs_available(m4v->bs)) break;
  237. load_size = gf_bs_available(m4v->bs);
  238. if (load_size>M4V_CACHE_SIZE) load_size=M4V_CACHE_SIZE;
  239. bpos = 0;
  240. cache_start = gf_bs_get_position(m4v->bs);
  241. gf_bs_read_data(m4v->bs, m4v_cache, (u32) load_size);
  242. }
  243. v = ( (v<<8) & 0xFFFFFF00) | ((u8) m4v_cache[bpos]);
  244. bpos++;
  245. if ((v & 0xFFFFFF00) == 0x00000100) {
  246. end = cache_start+bpos-4;
  247. found = 1;
  248. break;
  249. }
  250. }
  251. if (!found) return -1;
  252. m4v->current_object_start = end;
  253. gf_bs_seek(m4v->bs, end+3);
  254. m4v->current_object_type = gf_bs_read_u8(m4v->bs);
  255. return (s32) m4v->current_object_type;
  256. }
  257. GF_EXPORT
  258. void gf_m4v_rewrite_pl(char **o_data, u32 *o_dataLen, u8 PL)
  259. {
  260. u32 pos = 0;
  261. unsigned char *data = (unsigned char *)*o_data;
  262. u32 dataLen = *o_dataLen;
  263. while (pos+4<dataLen) {
  264. if (!data[pos] && !data[pos+1] && (data[pos+2]==0x01) && (data[pos+3]==M4V_VOS_START_CODE)) {
  265. data[pos+4] = PL;
  266. return;
  267. }
  268. pos ++;
  269. }
  270. /*emulate VOS at beggining*/
  271. (*o_data) = (char *)gf_malloc(sizeof(char)*(dataLen+5));
  272. (*o_data)[0] = 0;
  273. (*o_data)[1] = 0;
  274. (*o_data)[2] = 1;
  275. (*o_data)[3] = (char) M4V_VOS_START_CODE;
  276. (*o_data)[4] = PL;
  277. memcpy( (*o_data + 5), data, sizeof(char)*dataLen);
  278. gf_free(data);
  279. (*o_dataLen) = dataLen + 5;
  280. }
  281. static GF_Err M4V_Reset(GF_M4VParser *m4v, u64 start)
  282. {
  283. gf_bs_seek(m4v->bs, start);
  284. assert(start < 1<<31);
  285. m4v->current_object_start = (u32) start;
  286. m4v->current_object_type = 0;
  287. return GF_OK;
  288. }
  289. static GF_Err gf_m4v_parse_config_mpeg12(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi)
  290. {
  291. unsigned char p[4];
  292. u32 ext_type;
  293. s32 o_type;
  294. u8 go, par;
  295. if (!m4v || !dsi) return GF_BAD_PARAM;
  296. memset(dsi, 0, sizeof(GF_M4VDecSpecInfo));
  297. dsi->VideoPL = 0;
  298. go = 1;
  299. while (go) {
  300. o_type = M4V_LoadObject(m4v);
  301. switch (o_type) {
  302. case M2V_SEQ_START_CODE:
  303. dsi->RAP_stream = 1;
  304. gf_bs_read_data(m4v->bs, (char *) p, 4);
  305. dsi->width = (p[0] << 4) | ((p[1] >> 4) & 0xf);
  306. dsi->height = ((p[1] & 0xf) << 8) | p[2];
  307. dsi->VideoPL = GPAC_OTI_VIDEO_MPEG1;
  308. par = (p[3] >> 4) & 0xf;
  309. switch (par) {
  310. case 2: dsi->par_num = dsi->height/3; dsi->par_den = dsi->width/4; break;
  311. case 3: dsi->par_num = dsi->height/9; dsi->par_den = dsi->width/16; break;
  312. case 4: dsi->par_num = dsi->height/2; dsi->par_den = dsi->width/21; break;
  313. default: dsi->par_den = dsi->par_num = 0; break;
  314. }
  315. switch (p[3] & 0xf) {
  316. case 0: break;
  317. case 1: dsi->fps = 24000.0/1001.0; break;
  318. case 2: dsi->fps = 24.0; break;
  319. case 3: dsi->fps = 25.0; break;
  320. case 4: dsi->fps = 30000.0/1001.0; break;
  321. case 5: dsi->fps = 30.0; break;
  322. case 6: dsi->fps = 50.0; break;
  323. case 7: dsi->fps = ((60.0*1000.0)/1001.0); break;
  324. case 8: dsi->fps = 60.0; break;
  325. case 9: dsi->fps = 1; break;
  326. case 10: dsi->fps = 5; break;
  327. case 11: dsi->fps = 10; break;
  328. case 12: dsi->fps = 12; break;
  329. case 13: dsi->fps = 15; break;
  330. }
  331. break;
  332. case M2V_EXT_START_CODE:
  333. gf_bs_read_data(m4v->bs, (char *) p, 4);
  334. ext_type = ((p[0] >> 4) & 0xf);
  335. if (ext_type == 1) {
  336. dsi->VideoPL = 0x65;
  337. dsi->height = ((p[1] & 0x1) << 13) | ((p[2] & 0x80) << 5) | (dsi->height & 0x0fff);
  338. dsi->width = (((p[2] >> 5) & 0x3) << 12) | (dsi->width & 0x0fff);
  339. }
  340. break;
  341. case M2V_PIC_START_CODE:
  342. if (dsi->width) go = 0;
  343. break;
  344. default:
  345. break;
  346. /*EOS*/
  347. case -1:
  348. go = 0;
  349. m4v->current_object_start = gf_bs_get_position(m4v->bs);
  350. break;
  351. }
  352. }
  353. M4V_Reset(m4v, 0);
  354. return GF_OK;
  355. }
  356. static const struct { u32 w, h; } m4v_sar[6] = { { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 }, { 40, 33 } };
  357. static u8 m4v_get_sar_idx(u32 w, u32 h)
  358. {
  359. u32 i;
  360. for (i=0; i<6; i++) {
  361. if ((m4v_sar[i].w==w) && (m4v_sar[i].h==h)) return i;
  362. }
  363. return 0xF;
  364. }
  365. static GF_Err gf_m4v_parse_config_mpeg4(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi)
  366. {
  367. s32 o_type;
  368. u8 go, verid, par;
  369. s32 clock_rate;
  370. if (!m4v || !dsi) return GF_BAD_PARAM;
  371. memset(dsi, 0, sizeof(GF_M4VDecSpecInfo));
  372. go = 1;
  373. while (go) {
  374. o_type = M4V_LoadObject(m4v);
  375. switch (o_type) {
  376. /*vosh*/
  377. case M4V_VOS_START_CODE:
  378. dsi->VideoPL = (u8) gf_bs_read_u8(m4v->bs);
  379. break;
  380. case M4V_VOL_START_CODE:
  381. verid = 0;
  382. dsi->RAP_stream = gf_bs_read_int(m4v->bs, 1);
  383. dsi->objectType = gf_bs_read_int(m4v->bs, 8);
  384. if (gf_bs_read_int(m4v->bs, 1)) {
  385. verid = gf_bs_read_int(m4v->bs, 4);
  386. gf_bs_read_int(m4v->bs, 3);
  387. }
  388. par = gf_bs_read_int(m4v->bs, 4);
  389. if (par == 0xF) {
  390. dsi->par_num = gf_bs_read_int(m4v->bs, 8);
  391. dsi->par_den = gf_bs_read_int(m4v->bs, 8);
  392. } else if (par<6) {
  393. dsi->par_num = m4v_sar[par].w;
  394. dsi->par_den = m4v_sar[par].h;
  395. }
  396. if (gf_bs_read_int(m4v->bs, 1)) {
  397. gf_bs_read_int(m4v->bs, 3);
  398. if (gf_bs_read_int(m4v->bs, 1)) gf_bs_read_int(m4v->bs, 79);
  399. }
  400. dsi->has_shape = gf_bs_read_int(m4v->bs, 2);
  401. if (dsi->has_shape && (verid!=1) ) gf_bs_read_int(m4v->bs, 4);
  402. gf_bs_read_int(m4v->bs, 1);
  403. /*clock rate*/
  404. dsi->clock_rate = gf_bs_read_int(m4v->bs, 16);
  405. /*marker*/
  406. gf_bs_read_int(m4v->bs, 1);
  407. clock_rate = dsi->clock_rate-1;
  408. if (clock_rate >= 65536) clock_rate = 65535;
  409. if (clock_rate > 0) {
  410. for (dsi->NumBitsTimeIncrement = 1; dsi->NumBitsTimeIncrement < 16; dsi->NumBitsTimeIncrement++) {
  411. if (clock_rate == 1) break;
  412. clock_rate = (clock_rate >> 1);
  413. }
  414. } else {
  415. /*fix from vivien for divX*/
  416. dsi->NumBitsTimeIncrement = 1;
  417. }
  418. /*fixed FPS stream*/
  419. dsi->time_increment = 0;
  420. if (gf_bs_read_int(m4v->bs, 1)) {
  421. dsi->time_increment = gf_bs_read_int(m4v->bs, dsi->NumBitsTimeIncrement);
  422. }
  423. if (!dsi->has_shape) {
  424. gf_bs_read_int(m4v->bs, 1);
  425. dsi->width = gf_bs_read_int(m4v->bs, 13);
  426. gf_bs_read_int(m4v->bs, 1);
  427. dsi->height = gf_bs_read_int(m4v->bs, 13);
  428. } else {
  429. dsi->width = dsi->height = 0;
  430. }
  431. /*shape will be done later*/
  432. gf_bs_align(m4v->bs);
  433. break;
  434. case M4V_VOP_START_CODE:
  435. case M4V_GOV_START_CODE:
  436. go = 0;
  437. break;
  438. /*EOS*/
  439. case -1:
  440. go = 0;
  441. m4v->current_object_start = gf_bs_get_position(m4v->bs);
  442. break;
  443. /*don't interest us*/
  444. case M4V_UDTA_START_CODE:
  445. default:
  446. break;
  447. }
  448. }
  449. return GF_OK;
  450. }
  451. GF_EXPORT
  452. GF_Err gf_m4v_parse_config(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi)
  453. {
  454. if (m4v->mpeg12) {
  455. return gf_m4v_parse_config_mpeg12(m4v, dsi);
  456. } else {
  457. return gf_m4v_parse_config_mpeg4(m4v, dsi);
  458. }
  459. }
  460. static GF_Err gf_m4v_parse_frame_mpeg12(GF_M4VParser *m4v, GF_M4VDecSpecInfo dsi, u8 *frame_type, u32 *time_inc, u64 *size, u64 *start, Bool *is_coded)
  461. {
  462. u8 go, hasVOP, firstObj, val;
  463. s32 o_type;
  464. if (!m4v || !size || !start || !frame_type) return GF_BAD_PARAM;
  465. *size = 0;
  466. firstObj = 1;
  467. hasVOP = 0;
  468. *is_coded = 0;
  469. m4v->current_object_type = (u32) -1;
  470. *frame_type = 0;
  471. M4V_Reset(m4v, m4v->current_object_start);
  472. go = 1;
  473. while (go) {
  474. o_type = M4V_LoadObject(m4v);
  475. switch (o_type) {
  476. case M2V_PIC_START_CODE:
  477. /*done*/
  478. if (hasVOP) {
  479. go = 0;
  480. break;
  481. }
  482. if (firstObj) {
  483. *start = m4v->current_object_start;
  484. firstObj = 0;
  485. }
  486. hasVOP = 1;
  487. *is_coded = 1;
  488. val = gf_bs_read_u8(m4v->bs);
  489. val = gf_bs_read_u8(m4v->bs);
  490. *frame_type = ( (val >> 3) & 0x7 ) - 1;
  491. break;
  492. case M2V_GOP_START_CODE:
  493. if (firstObj) {
  494. *start = m4v->current_object_start;
  495. firstObj = 0;
  496. }
  497. if (hasVOP) go = 0;
  498. break;
  499. case M2V_SEQ_START_CODE:
  500. if (firstObj) {
  501. *start = m4v->current_object_start;
  502. firstObj = 0;
  503. }
  504. if (hasVOP) {
  505. go = 0;
  506. break;
  507. }
  508. /**/
  509. break;
  510. default:
  511. break;
  512. case -1:
  513. *size = gf_bs_get_position(m4v->bs) - *start;
  514. return GF_EOS;
  515. }
  516. }
  517. *size = m4v->current_object_start - *start;
  518. return GF_OK;
  519. }
  520. static GF_Err gf_m4v_parse_frame_mpeg4(GF_M4VParser *m4v, GF_M4VDecSpecInfo dsi, u8 *frame_type, u32 *time_inc, u64 *size, u64 *start, Bool *is_coded)
  521. {
  522. u8 go, hasVOP, firstObj, secs;
  523. s32 o_type;
  524. u32 vop_inc = 0;
  525. if (!m4v || !size || !start || !frame_type) return GF_BAD_PARAM;
  526. *size = 0;
  527. firstObj = 1;
  528. hasVOP = 0;
  529. *is_coded = 0;
  530. m4v->current_object_type = (u32) -1;
  531. *frame_type = 0;
  532. M4V_Reset(m4v, m4v->current_object_start);
  533. go = 1;
  534. while (go) {
  535. o_type = M4V_LoadObject(m4v);
  536. switch (o_type) {
  537. case M4V_VOP_START_CODE:
  538. /*done*/
  539. if (hasVOP) {
  540. go = 0;
  541. break;
  542. }
  543. if (firstObj) {
  544. *start = m4v->current_object_start;
  545. firstObj = 0;
  546. }
  547. hasVOP = 1;
  548. /*coding type*/
  549. *frame_type = gf_bs_read_int(m4v->bs, 2);
  550. /*modulo time base*/
  551. secs = 0;
  552. while (gf_bs_read_int(m4v->bs, 1) != 0)
  553. secs ++;
  554. /*no support for B frames in parsing*/
  555. secs += (dsi.enh_layer || *frame_type!=2) ? m4v->tc_dec : m4v->tc_disp;
  556. /*marker*/
  557. gf_bs_read_int(m4v->bs, 1);
  558. /*vop_time_inc*/
  559. if (dsi.NumBitsTimeIncrement)
  560. vop_inc = gf_bs_read_int(m4v->bs, dsi.NumBitsTimeIncrement);
  561. m4v->prev_tc_dec = m4v->tc_dec;
  562. m4v->prev_tc_disp = m4v->tc_disp;
  563. if (dsi.enh_layer || *frame_type!=2) {
  564. m4v->tc_disp = m4v->tc_dec;
  565. m4v->tc_dec = secs;
  566. }
  567. *time_inc = secs * dsi.clock_rate + vop_inc;
  568. /*marker*/
  569. gf_bs_read_int(m4v->bs, 1);
  570. /*coded*/
  571. *is_coded = gf_bs_read_int(m4v->bs, 1);
  572. gf_bs_align(m4v->bs);
  573. break;
  574. case M4V_GOV_START_CODE:
  575. if (firstObj) {
  576. *start = m4v->current_object_start;
  577. firstObj = 0;
  578. }
  579. if (hasVOP) go = 0;
  580. break;
  581. case M4V_VOS_START_CODE:
  582. case M4V_VOL_START_CODE:
  583. if (hasVOP) {
  584. go = 0;
  585. } else if (firstObj) {
  586. *start = m4v->current_object_start;
  587. firstObj = 0;
  588. }
  589. break;
  590. case M4V_VO_START_CODE:
  591. default:
  592. break;
  593. case -1:
  594. *size = gf_bs_get_position(m4v->bs) - *start;
  595. return GF_EOS;
  596. }
  597. }
  598. *size = m4v->current_object_start - *start;
  599. return GF_OK;
  600. }
  601. GF_EXPORT
  602. GF_Err gf_m4v_parse_frame(GF_M4VParser *m4v, GF_M4VDecSpecInfo dsi, u8 *frame_type, u32 *time_inc, u64 *size, u64 *start, Bool *is_coded)
  603. {
  604. if (m4v->mpeg12) {
  605. return gf_m4v_parse_frame_mpeg12(m4v, dsi, frame_type, time_inc, size, start, is_coded);
  606. } else {
  607. return gf_m4v_parse_frame_mpeg4(m4v, dsi, frame_type, time_inc, size, start, is_coded);
  608. }
  609. }
  610. GF_Err gf_m4v_rewrite_par(char **o_data, u32 *o_dataLen, s32 par_n, s32 par_d)
  611. {
  612. u64 start, end, size;
  613. GF_BitStream *mod;
  614. GF_M4VParser *m4v;
  615. Bool go = 1;
  616. m4v = gf_m4v_parser_new(*o_data, *o_dataLen, 0);
  617. mod = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
  618. end = start = 0;
  619. while (go) {
  620. u32 type = M4V_LoadObject(m4v);
  621. end = gf_bs_get_position(m4v->bs) - 4;
  622. size = end - start;
  623. /*store previous object*/
  624. if (size) {
  625. assert (size < 1<<31);
  626. if (size) gf_bs_write_data(mod, *o_data + start, (u32) size);
  627. start = end;
  628. }
  629. switch (type) {
  630. case M4V_VOL_START_CODE:
  631. gf_bs_write_int(mod, 0, 8);
  632. gf_bs_write_int(mod, 0, 8);
  633. gf_bs_write_int(mod, 1, 8);
  634. gf_bs_write_int(mod, M4V_VOL_START_CODE, 8);
  635. gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 1), 1);
  636. gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 8), 8);
  637. start = gf_bs_read_int(m4v->bs, 1);
  638. gf_bs_write_int(mod, (u32) start, 1);
  639. if (start) {
  640. gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 7), 7);
  641. }
  642. start = gf_bs_read_int(m4v->bs, 4);
  643. if (start == 0xF) {
  644. gf_bs_read_int(m4v->bs, 8);
  645. gf_bs_read_int(m4v->bs, 8);
  646. }
  647. if ((par_n>=0) && (par_d>=0)) {
  648. u8 par = m4v_get_sar_idx(par_n, par_d);
  649. gf_bs_write_int(mod, par, 4);
  650. if (par==0xF) {
  651. gf_bs_write_int(mod, par_n, 8);
  652. gf_bs_write_int(mod, par_d, 8);
  653. }
  654. } else {
  655. gf_bs_write_int(mod, 0x0, 4);
  656. }
  657. case -1:
  658. go = 0;
  659. break;
  660. default:
  661. break;
  662. }
  663. }
  664. while (gf_bs_bits_available(m4v->bs)) {
  665. u32 b = gf_bs_read_int(m4v->bs, 1);
  666. gf_bs_write_int(mod, b, 1);
  667. }
  668. gf_m4v_parser_del(m4v);
  669. gf_free(*o_data);
  670. gf_bs_get_content(mod, o_data, o_dataLen);
  671. gf_bs_del(mod);
  672. return GF_OK;
  673. }
  674. GF_EXPORT
  675. u64 gf_m4v_get_object_start(GF_M4VParser *m4v)
  676. {
  677. return m4v->current_object_start;
  678. }
  679. GF_EXPORT
  680. Bool gf_m4v_is_valid_object_type(GF_M4VParser *m4v)
  681. {
  682. return ((s32) m4v->current_object_type==-1) ? 0 : 1;
  683. }
  684. GF_EXPORT
  685. GF_Err gf_m4v_get_config(char *rawdsi, u32 rawdsi_size, GF_M4VDecSpecInfo *dsi)
  686. {
  687. GF_Err e;
  688. GF_M4VParser *vparse;
  689. if (!rawdsi || !rawdsi_size) return GF_NON_COMPLIANT_BITSTREAM;
  690. vparse = gf_m4v_parser_new(rawdsi, rawdsi_size, 0);
  691. e = gf_m4v_parse_config(vparse, dsi);
  692. gf_m4v_parser_del(vparse);
  693. return e;
  694. }
  695. #endif
  696. /*
  697. AAC parser
  698. */
  699. GF_EXPORT
  700. const char *gf_m4a_object_type_name(u32 objectType)
  701. {
  702. switch (objectType) {
  703. case 0: return "MPEG-4 Audio Reserved";
  704. case 1: return "MPEG-4 Audio AAC Main";
  705. case 2: return "MPEG-4 Audio AAC LC";
  706. case 3: return "MPEG-4 Audio AAC SSR";
  707. case 4: return "MPEG-4 Audio AAC LTP";
  708. case 5: return "MPEG-4 Audio SBR";
  709. case 6: return "MPEG-4 Audio AAC Scalable";
  710. case 7: return "MPEG-4 Audio TwinVQ";
  711. case 8: return "MPEG-4 Audio CELP";
  712. case 9: return "MPEG-4 Audio HVXC";
  713. case 10: return "MPEG-4 Audio Reserved";
  714. case 11: return "MPEG-4 Audio Reserved";
  715. case 12: return "MPEG-4 Audio TTSI";
  716. case 13: return "MPEG-4 Audio Main synthetic";
  717. case 14: return "MPEG-4 Audio Wavetable synthesis";
  718. case 15: return "MPEG-4 Audio General MIDI";
  719. case 16: return "MPEG-4 Audio Algorithmic Synthesis and Audio FX";
  720. case 17: return "MPEG-4 Audio ER AAC LC";
  721. case 18: return "MPEG-4 Audio Reserved";
  722. case 19: return "MPEG-4 Audio ER AAC LTP";
  723. case 20: return "MPEG-4 Audio ER AAC scalable";
  724. case 21: return "MPEG-4 Audio ER TwinVQ";
  725. case 22: return "MPEG-4 Audio ER BSAC";
  726. case 23: return "MPEG-4 Audio ER AAC LD";
  727. case 24: return "MPEG-4 Audio ER CELP";
  728. case 25: return "MPEG-4 Audio ER HVXC";
  729. case 26: return "MPEG-4 Audio ER HILN";
  730. case 27: return "MPEG-4 Audio ER Parametric";
  731. case 28: return "MPEG-4 Audio SSC";
  732. case 29: return "MPEG-4 Audio ParametricStereo";
  733. case 30: return "MPEG-4 Audio Reserved";
  734. case 31: return "MPEG-4 Audio Reserved";
  735. case 32: return "MPEG-1 Audio Layer-1";
  736. case 33: return "MPEG-1 Audio Layer-2";
  737. case 34: return "MPEG-1 Audio Layer-3";
  738. case 35: return "MPEG-4 Audio DST";
  739. case 36: return "MPEG-4 Audio ALS";
  740. case 37: return "MPEG-4 Audio SLS";
  741. default: return "MPEG-4 Audio Unknown";
  742. }
  743. }
  744. GF_EXPORT
  745. const char *gf_m4a_get_profile_name(u8 audio_pl)
  746. {
  747. switch (audio_pl) {
  748. case 0x00: return "ISO Reserved (0x00)";
  749. case 0x01: return "Main Audio Profile @ Level 1";
  750. case 0x02: return "Main Audio Profile @ Level 2";
  751. case 0x03: return "Main Audio Profile @ Level 3";
  752. case 0x04: return "Main Audio Profile @ Level 4";
  753. case 0x05: return "Scalable Audio Profile @ Level 1";
  754. case 0x06: return "Scalable Audio Profile @ Level 2";
  755. case 0x07: return "Scalable Audio Profile @ Level 3";
  756. case 0x08: return "Scalable Audio Profile @ Level 4";
  757. case 0x09: return "Speech Audio Profile @ Level 1";
  758. case 0x0A: return "Speech Audio Profile @ Level 2";
  759. case 0x0B: return "Synthetic Audio Profile @ Level 1";
  760. case 0x0C: return "Synthetic Audio Profile @ Level 2";
  761. case 0x0D: return "Synthetic Audio Profile @ Level 3";
  762. case 0x0E: return "High Quality Audio Profile @ Level 1";
  763. case 0x0F: return "High Quality Audio Profile @ Level 2";
  764. case 0x10: return "High Quality Audio Profile @ Level 3";
  765. case 0x11: return "High Quality Audio Profile @ Level 4";
  766. case 0x12: return "High Quality Audio Profile @ Level 5";
  767. case 0x13: return "High Quality Audio Profile @ Level 6";
  768. case 0x14: return "High Quality Audio Profile @ Level 7";
  769. case 0x15: return "High Quality Audio Profile @ Level 8";
  770. case 0x16: return "Low Delay Audio Profile @ Level 1";
  771. case 0x17: return "Low Delay Audio Profile @ Level 2";
  772. case 0x18: return "Low Delay Audio Profile @ Level 3";
  773. case 0x19: return "Low Delay Audio Profile @ Level 4";
  774. case 0x1A: return "Low Delay Audio Profile @ Level 5";
  775. case 0x1B: return "Low Delay Audio Profile @ Level 6";
  776. case 0x1C: return "Low Delay Audio Profile @ Level 7";
  777. case 0x1D: return "Low Delay Audio Profile @ Level 8";
  778. case 0x1E: return "Natural Audio Profile @ Level 1";
  779. case 0x1F: return "Natural Audio Profile @ Level 2";
  780. case 0x20: return "Natural Audio Profile @ Level 3";
  781. case 0x21: return "Natural Audio Profile @ Level 4";
  782. case 0x22: return "Mobile Audio Internetworking Profile @ Level 1";
  783. case 0x23: return "Mobile Audio Internetworking Profile @ Level 2";
  784. case 0x24: return "Mobile Audio Internetworking Profile @ Level 3";
  785. case 0x25: return "Mobile Audio Internetworking Profile @ Level 4";
  786. case 0x26: return "Mobile Audio Internetworking Profile @ Level 5";
  787. case 0x27: return "Mobile Audio Internetworking Profile @ Level 6";
  788. case 0x28: return "AAC Profile @ Level 1";
  789. case 0x29: return "AAC Profile @ Level 2";
  790. case 0x2A: return "AAC Profile @ Level 4";
  791. case 0x2B: return "AAC Profile @ Level 5";
  792. case 0x2C: return "High Efficiency AAC Profile @ Level 2";
  793. case 0x2D: return "High Efficiency AAC Profile @ Level 3";
  794. case 0x2E: return "High Efficiency AAC Profile @ Level 4";
  795. case 0x2F: return "High Efficiency AAC Profile @ Level 5";
  796. case 0x3B: return "High Definition AAC Profile @ Level 1";
  797. case 0x3C: return "ALS Simple Profile @ Level 1";
  798. case 0xFE: return "Not part of MPEG-4 audio profiles";
  799. case 0xFF: return "No audio capability required";
  800. default: return "ISO Reserved / User Private";
  801. }
  802. }
  803. #ifndef GPAC_DISABLE_AV_PARSERS
  804. GF_EXPORT
  805. u32 gf_m4a_get_profile(GF_M4ADecSpecInfo *cfg)
  806. {
  807. switch (cfg->base_object_type) {
  808. case 2: /*AAC LC*/
  809. if (cfg->nb_chan<=2) return (cfg->base_sr<=24000) ? 0x28 : 0x29; /*LC@L1 or LC@L2*/
  810. return (cfg->base_sr<=48000) ? 0x2A : 0x2B; /*LC@L4 or LC@L5*/
  811. case 5: /*HE-AAC - SBR*/
  812. case 29: /*HE-AAC - SBR+PS*/
  813. if (cfg->nb_chan<=2) return (cfg->base_sr<=24000) ? 0x2C : 0x2D; /*HE@L2 or HE@L3*/
  814. return (cfg->base_sr<=48000) ? 0x2E : 0x2F; /*HE@L4 or HE@L5*/
  815. /*default to HQ*/
  816. default:
  817. if (cfg->nb_chan<=2) return (cfg->base_sr<24000) ? 0x0E : 0x0F; /*HQ@L1 or HQ@L2*/
  818. return 0x10; /*HQ@L3*/
  819. }
  820. }
  821. GF_EXPORT
  822. GF_Err gf_m4a_parse_config(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg, Bool size_known)
  823. {
  824. memset(cfg, 0, sizeof(GF_M4ADecSpecInfo));
  825. cfg->base_object_type = gf_bs_read_int(bs, 5);
  826. /*extended object type*/
  827. if (cfg->base_object_type==31) {
  828. cfg->base_object_type = 32 + gf_bs_read_int(bs, 6);
  829. }
  830. cfg->base_sr_index = gf_bs_read_int(bs, 4);
  831. if (cfg->base_sr_index == 0x0F) {
  832. cfg->base_sr = gf_bs_read_int(bs, 24);
  833. } else {
  834. cfg->base_sr = GF_M4ASampleRates[cfg->base_sr_index];
  835. }
  836. cfg->nb_chan = gf_bs_read_int(bs, 4);
  837. /*this is 7+1 channels*/
  838. if (cfg->nb_chan==7) cfg->nb_chan=8;
  839. if (cfg->base_object_type==5 || cfg->base_object_type==29) {
  840. if (cfg->base_object_type==29) {
  841. cfg->has_ps = 1;
  842. cfg->nb_chan = 1;
  843. }
  844. cfg->has_sbr = 1;
  845. cfg->sbr_sr_index = gf_bs_read_int(bs, 4);
  846. if (cfg->sbr_sr_index == 0x0F) {
  847. cfg->sbr_sr = gf_bs_read_int(bs, 24);
  848. } else {
  849. cfg->sbr_sr = GF_M4ASampleRates[cfg->sbr_sr_index];
  850. }
  851. cfg->sbr_object_type = gf_bs_read_int(bs, 5);
  852. }
  853. /*object cfg*/
  854. switch (cfg->base_object_type) {
  855. case 1:
  856. case 2:
  857. case 3:
  858. case 4:
  859. case 6:
  860. case 7:
  861. case 17:
  862. case 19:
  863. case 20:
  864. case 21:
  865. case 22:
  866. case 23:
  867. {
  868. Bool ext_flag;
  869. /*frame length flag*/
  870. /*fl_flag = */gf_bs_read_int(bs, 1);
  871. /*depends on core coder*/
  872. if (gf_bs_read_int(bs, 1))
  873. /*delay = */gf_bs_read_int(bs, 14);
  874. ext_flag = gf_bs_read_int(bs, 1);
  875. if (!cfg->nb_chan) {
  876. }
  877. if ((cfg->base_object_type == 6) || (cfg->base_object_type == 20)) {
  878. gf_bs_read_int(bs, 3);
  879. }
  880. if (ext_flag) {
  881. if (cfg->base_object_type == 22) {
  882. gf_bs_read_int(bs, 5);
  883. gf_bs_read_int(bs, 11);
  884. }
  885. if ((cfg->base_object_type == 17)
  886. || (cfg->base_object_type == 19)
  887. || (cfg->base_object_type == 20)
  888. || (cfg->base_object_type == 23)
  889. ) {
  890. gf_bs_read_int(bs, 1);
  891. gf_bs_read_int(bs, 1);
  892. gf_bs_read_int(bs, 1);
  893. }
  894. ext_flag = gf_bs_read_int(bs, 1);
  895. }
  896. }
  897. break;
  898. }
  899. /*ER cfg*/
  900. switch (cfg->base_object_type) {
  901. case 17:
  902. case 19:
  903. case 20:
  904. case 21:
  905. case 22:
  906. case 23:
  907. case 24:
  908. case 25:
  909. case 26:
  910. case 27:
  911. {
  912. u32 epConfig = gf_bs_read_int(bs, 2);
  913. if ((epConfig == 2) || (epConfig == 3) ) {
  914. }
  915. if (epConfig == 3) {
  916. gf_bs_read_int(bs, 1);
  917. }
  918. }
  919. break;
  920. }
  921. if (size_known && (cfg->base_object_type != 5) && (cfg->base_object_type != 29) ) {
  922. while (gf_bs_available(bs)>=2) {
  923. u32 sync = gf_bs_peek_bits(bs, 11, 0);
  924. if (sync==0x2b7) {
  925. gf_bs_read_int(bs, 11);
  926. cfg->sbr_object_type = gf_bs_read_int(bs, 5);
  927. cfg->has_sbr = gf_bs_read_int(bs, 1);
  928. if (cfg->has_sbr) {
  929. cfg->sbr_sr_index = gf_bs_read_int(bs, 4);
  930. if (cfg->sbr_sr_index == 0x0F) {
  931. cfg->sbr_sr = gf_bs_read_int(bs, 24);
  932. } else {
  933. cfg->sbr_sr = GF_M4ASampleRates[cfg->sbr_sr_index];
  934. }
  935. }
  936. } else if (sync == 0x548) {
  937. gf_bs_read_int(bs, 11);
  938. cfg->has_ps = gf_bs_read_int(bs, 1);
  939. if (cfg->has_ps)
  940. cfg->nb_chan = 1;
  941. } else {
  942. break;
  943. }
  944. }
  945. }
  946. cfg->audioPL = gf_m4a_get_profile(cfg);
  947. return GF_OK;
  948. }
  949. GF_EXPORT
  950. GF_Err gf_m4a_get_config(char *dsi, u32 dsi_size, GF_M4ADecSpecInfo *cfg)
  951. {
  952. GF_BitStream *bs;
  953. if (!dsi || !dsi_size || (dsi_size<2) ) return GF_NON_COMPLIANT_BITSTREAM;
  954. bs = gf_bs_new(dsi, dsi_size, GF_BITSTREAM_READ);
  955. gf_m4a_parse_config(bs, cfg, 1);
  956. gf_bs_del(bs);
  957. return GF_OK;
  958. }
  959. u32 gf_latm_get_value(GF_BitStream *bs)
  960. {
  961. u32 i, tmp, value = 0;
  962. u32 bytesForValue = gf_bs_read_int(bs, 2);
  963. for (i=0; i <= bytesForValue; i++) {
  964. value <<= 8;
  965. tmp = gf_bs_read_int(bs, 8);
  966. value += tmp;
  967. }
  968. return value;
  969. }
  970. GF_EXPORT
  971. GF_Err gf_m4a_write_config_bs(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg)
  972. {
  973. if (!cfg->base_sr_index) {
  974. if (!cfg->base_sr) return GF_BAD_PARAM;
  975. while (GF_M4ASampleRates[cfg->base_sr_index]) {
  976. if (GF_M4ASampleRates[cfg->base_sr_index]==cfg->base_sr)
  977. break;
  978. cfg->base_sr_index++;
  979. }
  980. }
  981. if (cfg->sbr_sr && !cfg->sbr_sr_index) {
  982. while (GF_M4ASampleRates[cfg->sbr_sr_index]) {
  983. if (GF_M4ASampleRates[cfg->sbr_sr_index]==cfg->sbr_sr)
  984. break;
  985. cfg->sbr_sr_index++;
  986. }
  987. }
  988. /*extended object type*/
  989. if (cfg->base_object_type>=32) {
  990. gf_bs_write_int(bs, 31, 5);
  991. gf_bs_write_int(bs, cfg->base_object_type-32, 6);
  992. } else {
  993. gf_bs_write_int(bs, cfg->base_object_type, 5);
  994. }
  995. gf_bs_write_int(bs, cfg->base_sr_index, 4);
  996. if (cfg->base_sr_index == 0x0F) {
  997. gf_bs_write_int(bs, cfg->base_sr, 24);
  998. }
  999. if (cfg->nb_chan == 8) {
  1000. gf_bs_write_int(bs, 7, 4);
  1001. } else {
  1002. gf_bs_write_int(bs, cfg->nb_chan, 4);
  1003. }
  1004. if (cfg->base_object_type==5 || cfg->base_object_type==29) {
  1005. if (cfg->base_object_type == 29) {
  1006. cfg->has_ps = 1;
  1007. cfg->nb_chan = 1;
  1008. }
  1009. cfg->has_sbr = 1;
  1010. gf_bs_write_int(bs, cfg->sbr_sr_index, 4);
  1011. if (cfg->sbr_sr_index == 0x0F) {
  1012. gf_bs_write_int(bs, cfg->sbr_sr, 24);
  1013. }
  1014. gf_bs_write_int(bs, cfg->sbr_object_type, 5);
  1015. }
  1016. /*object cfg*/
  1017. switch (cfg->base_object_type) {
  1018. case 1:
  1019. case 2:
  1020. case 3:
  1021. case 4:
  1022. case 6:
  1023. case 7:
  1024. case 17:
  1025. case 19:
  1026. case 20:
  1027. case 21:
  1028. case 22:
  1029. case 23:
  1030. {
  1031. /*frame length flag*/
  1032. gf_bs_write_int(bs, 0, 1);
  1033. /*depends on core coder*/
  1034. gf_bs_write_int(bs, 0, 1);
  1035. /*ext flag*/
  1036. gf_bs_write_int(bs, 0, 1);
  1037. if ((cfg->base_object_type == 6) || (cfg->base_object_type == 20)) {
  1038. gf_bs_write_int(bs, 0, 3);
  1039. }
  1040. }
  1041. break;
  1042. }
  1043. /*ER cfg - not supported*/
  1044. /*implicit sbr - not used yet*/
  1045. if (0 && (cfg->base_object_type != 5) && (cfg->base_object_type != 29) ) {
  1046. gf_bs_write_int(bs, 0x2b7, 11);
  1047. cfg->sbr_object_type = gf_bs_read_int(bs, 5);
  1048. cfg->has_sbr = gf_bs_read_int(bs, 1);
  1049. if (cfg->has_sbr) {
  1050. cfg->sbr_sr_index = gf_bs_read_int(bs, 4);
  1051. if (cfg->sbr_sr_index == 0x0F) {
  1052. cfg->sbr_sr = gf_bs_read_int(bs, 24);
  1053. } else {
  1054. cfg->sbr_sr = GF_M4ASampleRates[cfg->sbr_sr_index];
  1055. }
  1056. }
  1057. }
  1058. return GF_OK;
  1059. }
  1060. GF_EXPORT
  1061. GF_Err gf_m4a_write_config(GF_M4ADecSpecInfo *cfg, char **dsi, u32 *dsi_size)
  1062. {
  1063. GF_BitStream *bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
  1064. gf_m4a_write_config_bs(bs, cfg);
  1065. gf_bs_get_content(bs, dsi, dsi_size);
  1066. gf_bs_del(bs);
  1067. return GF_OK;
  1068. }
  1069. #endif /*GPAC_DISABLE_AV_PARSERS*/
  1070. GF_EXPORT
  1071. u8 gf_mp3_version(u32 hdr)
  1072. {
  1073. return ((hdr >> 19) & 0x3);
  1074. }
  1075. GF_EXPORT
  1076. const char *gf_mp3_version_name(u32 hdr)
  1077. {
  1078. u32 v = gf_mp3_version(hdr);
  1079. switch (v) {
  1080. case 0: return "MPEG-2.5";
  1081. case 1: return "Reserved";
  1082. case 2: return "MPEG-2";
  1083. case 3: return "MPEG-1";
  1084. default: return "Unknown";
  1085. }
  1086. }
  1087. #ifndef GPAC_DISABLE_AV_PARSERS
  1088. GF_EXPORT
  1089. u8 gf_mp3_layer(u32 hdr)
  1090. {
  1091. return 4 - (((hdr >> 17) & 0x3));
  1092. }
  1093. GF_EXPORT
  1094. u8 gf_mp3_num_channels(u32 hdr)
  1095. {
  1096. if (((hdr >> 6) & 0x3) == 3) return 1;
  1097. return 2;
  1098. }
  1099. GF_EXPORT
  1100. u16 gf_mp3_sampling_rate(u32 hdr)
  1101. {
  1102. u16 res;
  1103. /* extract the necessary fields from the MP3 header */
  1104. u8 version = gf_mp3_version(hdr);
  1105. u8 sampleRateIndex = (hdr >> 10) & 0x3;
  1106. switch (sampleRateIndex) {
  1107. case 0:
  1108. res = 44100;
  1109. break;
  1110. case 1:
  1111. res = 48000;
  1112. break;
  1113. case 2:
  1114. res = 32000;
  1115. break;
  1116. default:
  1117. GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] Samplerate index not valid\n"));
  1118. return 0;
  1119. }
  1120. /*reserved or MPEG-1*/
  1121. if (version & 1) return res;
  1122. /*MPEG-2*/
  1123. res /= 2;
  1124. /*MPEG-2.5*/
  1125. if (version == 0) res /= 2;
  1126. return res;
  1127. }
  1128. GF_EXPORT
  1129. u16 gf_mp3_window_size(u32 hdr)
  1130. {
  1131. u8 version = gf_mp3_version(hdr);
  1132. u8 layer = gf_mp3_layer(hdr);
  1133. if (layer == 3) {
  1134. if (version == 3) return 1152;
  1135. return 576;
  1136. }
  1137. if (layer == 2) return 1152;
  1138. return 384;
  1139. }
  1140. GF_EXPORT
  1141. u8 gf_mp3_object_type_indication(u32 hdr)
  1142. {
  1143. switch (gf_mp3_version(hdr)) {
  1144. case 3:
  1145. return GPAC_OTI_AUDIO_MPEG1;
  1146. case 2:
  1147. case 0:
  1148. return GPAC_OTI_AUDIO_MPEG2_PART3;
  1149. default:
  1150. return 0x00;
  1151. }
  1152. }
  1153. /*aligned bitrate parsing with libMAD*/
  1154. static
  1155. u32 const bitrate_table[5][15] = {
  1156. /* MPEG-1 */
  1157. { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */
  1158. 256000, 288000, 320000, 352000, 384000, 416000, 448000 },
  1159. { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */
  1160. 128000, 160000, 192000, 224000, 256000, 320000, 384000 },
  1161. { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */
  1162. 112000, 128000, 160000, 192000, 224000, 256000, 320000 },
  1163. /* MPEG-2 LSF */
  1164. { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */
  1165. 128000, 144000, 160000, 176000, 192000, 224000, 256000 },
  1166. { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */
  1167. 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */
  1168. };
  1169. u32 gf_mp3_bit_rate(u32 hdr)
  1170. {
  1171. u8 version = gf_mp3_version(hdr);
  1172. u8 layer = gf_mp3_layer(hdr);
  1173. u8 bitRateIndex = (hdr >> 12) & 0xF;
  1174. if (bitRateIndex == 15) {
  1175. GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] Bitrate index not valid\n"));
  1176. return 0;
  1177. }
  1178. /*MPEG-1*/
  1179. if (version & 1)
  1180. return bitrate_table[layer - 1][bitRateIndex];
  1181. /*MPEG-2/2.5*/
  1182. else
  1183. return bitrate_table[3 + (layer >> 1)][bitRateIndex];
  1184. }
  1185. GF_EXPORT
  1186. u16 gf_mp3_frame_size(u32 hdr)
  1187. {
  1188. u8 version = gf_mp3_version(hdr);
  1189. u8 layer = gf_mp3_layer(hdr);
  1190. u32 pad = ( (hdr >> 9) & 0x1) ? 1 : 0;
  1191. u32 bitrate = gf_mp3_bit_rate(hdr);
  1192. u32 samplerate = gf_mp3_sampling_rate(hdr);
  1193. u32 frameSize = 0;
  1194. if (!samplerate || !bitrate) return 0;
  1195. if (layer==1) {
  1196. frameSize = (( 12 * bitrate / samplerate) + pad) * 4;
  1197. } else {
  1198. u32 slots_per_frame = 144;
  1199. if ((layer == 3) && !(version & 1)) slots_per_frame = 72;
  1200. frameSize = (slots_per_frame * bitrate / samplerate) + pad;
  1201. }
  1202. return (u16) frameSize;
  1203. }
  1204. GF_EXPORT
  1205. u32 gf_mp3_get_next_header(FILE* in)
  1206. {
  1207. u8 b, state = 0;
  1208. u32 dropped = 0;
  1209. unsigned char bytes[4];
  1210. bytes[0] = bytes[1] = bytes[2] = bytes[3] = 0;
  1211. while (1) {
  1212. if (fread(&b, 1, 1, in) == 0) return 0;
  1213. if (state==3) {
  1214. bytes[state] = b;
  1215. return GF_4CC(bytes[0], bytes[1], bytes[2], bytes[3]);
  1216. }
  1217. if (state==2) {
  1218. if (((b & 0xF0) == 0) || ((b & 0xF0) == 0xF0) || ((b & 0x0C) == 0x0C)) {
  1219. if (bytes[1] == 0xFF) state = 1;
  1220. else state = 0;
  1221. } else {
  1222. bytes[state] = b;
  1223. state = 3;
  1224. }
  1225. }
  1226. if (state==1) {
  1227. if (((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) {
  1228. bytes[state] = b;
  1229. state = 2;
  1230. } else {
  1231. state = 0;
  1232. }
  1233. }
  1234. if (state==0) {
  1235. if (b == 0xFF) {
  1236. bytes[state] = b;
  1237. state = 1;
  1238. } else {
  1239. if ((dropped == 0) && ((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) {
  1240. bytes[0] = (u8) 0xFF;
  1241. bytes[1] = b;
  1242. state = 2;
  1243. } else {
  1244. dropped++;
  1245. }
  1246. }
  1247. }
  1248. }
  1249. return 0;
  1250. }
  1251. GF_EXPORT
  1252. u32 gf_mp3_get_next_header_mem(const char *buffer, u32 size, u32 *pos)
  1253. {
  1254. u32 cur;
  1255. u8 b, state = 0;
  1256. u32 dropped = 0;
  1257. unsigned char bytes[4];
  1258. bytes[0] = bytes[1] = bytes[2] = bytes[3] = 0;
  1259. cur = 0;
  1260. *pos = 0;
  1261. while (cur<size) {
  1262. b = (u8) buffer[cur];
  1263. cur++;
  1264. if (state==3) {
  1265. u32 val;
  1266. bytes[state] = b;
  1267. val = GF_4CC(bytes[0], bytes[1], bytes[2], bytes[3]);
  1268. if (gf_mp3_frame_size(val)) {
  1269. *pos = dropped;
  1270. return val;
  1271. }
  1272. state = 0;
  1273. dropped = cur;
  1274. }
  1275. if (state==2) {
  1276. if (((b & 0xF0) == 0) || ((b & 0xF0) == 0xF0) || ((b & 0x0C) == 0x0C)) {
  1277. if (bytes[1] == 0xFF) {
  1278. state = 1;
  1279. dropped+=1;
  1280. } else {
  1281. state = 0;
  1282. dropped = cur;
  1283. }
  1284. } else {
  1285. bytes[state] = b;
  1286. state = 3;
  1287. }
  1288. }
  1289. if (state==1) {
  1290. if (((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) {
  1291. bytes[state] = b;
  1292. state = 2;
  1293. } else {
  1294. state = 0;
  1295. dropped = cur;
  1296. }
  1297. }
  1298. if (state==0) {
  1299. if (b == 0xFF) {
  1300. bytes[state] = b;
  1301. state = 1;
  1302. } else {
  1303. dropped++;
  1304. }
  1305. }
  1306. }
  1307. return 0;
  1308. }
  1309. #endif /*GPAC_DISABLE_AV_PARSERS*/
  1310. GF_EXPORT
  1311. const char *gf_avc_get_profile_name(u8 video_prof)
  1312. {
  1313. switch (video_prof) {
  1314. case 0x42: return "Baseline";
  1315. case 0x4D: return "Main";
  1316. case 0x53: return "Scalable Baseline";
  1317. case 0x56: return "Scalable High";
  1318. case 0x58: return "Extended";
  1319. case 0x64: return "High";
  1320. case 0x6E: return "High 10";
  1321. case 0x7A: return "High 4:2:2";
  1322. case 0x90: return "High 4:4:4";
  1323. default: return "Unknown";
  1324. }
  1325. }
  1326. #ifndef GPAC_DISABLE_AV_PARSERS
  1327. static u8 avc_golomb_bits[256] = {
  1328. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3,
  1329. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2,
  1330. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1331. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1,
  1332. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1333. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1334. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1335. 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  1336. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1337. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1338. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1339. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1340. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1341. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1342. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1343. 0
  1344. };
  1345. static u32 bs_get_ue(GF_BitStream *bs)
  1346. {
  1347. u8 coded;
  1348. u32 bits = 0, read = 0;
  1349. while (1) {
  1350. read = gf_bs_peek_bits(bs, 8, 0);
  1351. if (read) break;
  1352. //check whether we still have bits once the peek is done since we may have less than 8 bits available
  1353. if (!gf_bs_available(bs)) {
  1354. GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AVC/HEVC] Not enough bits in bitstream !!\n"));
  1355. return 0;
  1356. }
  1357. gf_bs_read_int(bs, 8);
  1358. bits += 8;
  1359. }
  1360. coded = avc_golomb_bits[read];
  1361. gf_bs_read_int(bs, coded);
  1362. bits += coded;
  1363. return gf_bs_read_int(bs, bits + 1) - 1;
  1364. }
  1365. static s32 bs_get_se(GF_BitStream *bs)
  1366. {
  1367. u32 v = bs_get_ue(bs);
  1368. if ((v & 0x1) == 0) return (s32) (0 - (v>>1));
  1369. return (v + 1) >> 1;
  1370. }
  1371. u32 gf_media_nalu_is_start_code(GF_BitStream *bs)
  1372. {
  1373. u8 s1, s2, s3, s4;
  1374. Bool is_sc = 0;
  1375. u64 pos = gf_bs_get_position(bs);
  1376. s1 = gf_bs_read_int(bs, 8);
  1377. s2 = gf_bs_read_int(bs, 8);
  1378. if (!s1 && !s2) {
  1379. s3 = gf_bs_read_int(bs, 8);
  1380. if (s3==0x01) is_sc = 3;
  1381. else if (!s3) {
  1382. s4 = gf_bs_read_int(bs, 8);
  1383. if (s4==0x01) is_sc = 4;
  1384. }
  1385. }
  1386. gf_bs_seek(bs, pos+is_sc);
  1387. return is_sc;
  1388. }
  1389. /*read that amount of data at each IO access rather than fetching byte by byte...*/
  1390. #define AVC_CACHE_SIZE 4096
  1391. GF_EXPORT
  1392. u32 gf_media_nalu_next_start_code_bs(GF_BitStream *bs)
  1393. {
  1394. u32 v, bpos;
  1395. char avc_cache[AVC_CACHE_SIZE];
  1396. u64 end, cache_start, load_size;
  1397. u64 start = gf_bs_get_position(bs);
  1398. if (start<3) return 0;
  1399. load_size = 0;
  1400. bpos = 0;
  1401. cache_start = 0;
  1402. end = 0;
  1403. v = 0xffffffff;
  1404. while (!end) {
  1405. /*refill cache*/
  1406. if (bpos == (u32) load_size) {
  1407. if (!gf_bs_available(bs)) break;
  1408. load_size = gf_bs_available(bs);
  1409. if (load_size>AVC_CACHE_SIZE) load_size=AVC_CACHE_SIZE;
  1410. bpos = 0;
  1411. cache_start = gf_bs_get_position(bs);
  1412. gf_bs_read_data(bs, avc_cache, (u32) load_size);
  1413. }
  1414. v = ( (v<<8) & 0xFFFFFF00) | ((u32) avc_cache[bpos]);
  1415. bpos++;
  1416. if (v == 0x00000001) end = cache_start+bpos-4;
  1417. else if ( (v & 0x00FFFFFF) == 0x00000001) end = cache_start+bpos-3;
  1418. }
  1419. gf_bs_seek(bs, start);
  1420. if (!end) end = gf_bs_get_size(bs);
  1421. return (u32) (end-start);
  1422. }
  1423. GF_EXPORT
  1424. u32 gf_media_nalu_next_start_code(u8 *data, u32 data_len, u32 *sc_size)
  1425. {
  1426. u32 v, bpos;
  1427. u32 end;
  1428. bpos = 0;
  1429. end = 0;
  1430. v = 0xffffffff;
  1431. while (!end) {
  1432. /*refill cache*/
  1433. if (bpos == (u32) data_len)
  1434. break;
  1435. v = ( (v<<8) & 0xFFFFFF00) | ((u32) data[bpos]);
  1436. bpos++;
  1437. if (v == 0x00000001) {
  1438. end = bpos-4;
  1439. *sc_size = 4;
  1440. return end;
  1441. }
  1442. else if ( (v & 0x00FFFFFF) == 0x00000001) {
  1443. end = bpos-3;
  1444. *sc_size = 3;
  1445. return end;
  1446. }
  1447. }
  1448. if (!end) end = data_len;
  1449. return (u32) (end);
  1450. }
  1451. Bool gf_media_avc_slice_is_intra(AVCState *avc)
  1452. {
  1453. switch (avc->s_info.slice_type) {
  1454. case GF_AVC_TYPE_I:
  1455. case GF_AVC_TYPE2_I:
  1456. case GF_AVC_TYPE_SI:
  1457. case GF_AVC_TYPE2_SI:
  1458. return 1;
  1459. default:
  1460. return 0;
  1461. }
  1462. }
  1463. Bool gf_media_avc_slice_is_IDR(AVCState *avc)
  1464. {
  1465. if (avc->sei.recovery_point.valid)
  1466. {
  1467. avc->sei.recovery_point.valid = 0;
  1468. return 1;
  1469. }
  1470. if (avc->s_info.nal_unit_type != GF_AVC_NALU_IDR_SLICE)
  1471. return 0;
  1472. return gf_media_avc_slice_is_intra(avc);
  1473. }
  1474. static const struct { u32 w, h; } avc_sar[14] =
  1475. {
  1476. { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
  1477. { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
  1478. { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
  1479. { 64, 33 }, { 160,99 },
  1480. };
  1481. /*ISO 14496-10 (N11084) E.1.2*/
  1482. static void avc_parse_hrd_parameters(GF_BitStream *bs, AVC_HRD *hrd)
  1483. {
  1484. int i, cpb_cnt_minus1;
  1485. cpb_cnt_minus1 = bs_get_ue(bs); /*cpb_cnt_minus1*/
  1486. if (cpb_cnt_minus1 > 31)
  1487. GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] invalid cpb_cnt_minus1 value: %d (expected in [0;31])\n", cpb_cnt_minus1));
  1488. gf_bs_read_int(bs, 4); /*bit_rate_scale*/
  1489. gf_bs_read_int(bs, 4); /*cpb_size_scale*/
  1490. /*for( SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ ) {*/
  1491. for (i=0; i<=cpb_cnt_minus1; i++) {
  1492. bs_get_ue(bs); /*bit_rate_value_minus1[ SchedSelIdx ]*/
  1493. bs_get_ue(bs); /*cpb_size_value_minus1[ SchedSelIdx ]*/
  1494. gf_bs_read_int(bs, 1); /*cbr_flag[ SchedSelIdx ]*/
  1495. }
  1496. gf_bs_read_int(bs, 5); /*initial_cpb_removal_delay_length_minus1*/
  1497. hrd->cpb_removal_delay_length_minus1 = gf_bs_read_int(bs, 5); /*cpb_removal_delay_length_minus1*/
  1498. hrd->dpb_output_delay_length_minus1 = gf_bs_read_int(bs, 5); /*dpb_output_delay_length_minus1*/
  1499. hrd->time_offset_length = gf_bs_read_int(bs, 5); /*time_offset_length*/
  1500. return;
  1501. }
  1502. /*returns the nal_size without emulation prevention bytes*/
  1503. static u32 avc_emulation_bytes_add_count(char *buffer, u32 nal_size)
  1504. {
  1505. u32 i = 0, emulation_bytes_count = 0;
  1506. u8 num_zero = 0;
  1507. while (i < nal_size) {
  1508. /*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003
  1509. other than the following sequences shall not occur at any byte-aligned position:
  1510. \96 0x00000300
  1511. \96 0x00000301
  1512. \96 0x00000302
  1513. \96 0x00000303"
  1514. */
  1515. if (num_zero == 2 && buffer[i] < 0x04) {
  1516. /*emulation code found*/
  1517. num_zero = 0;
  1518. emulation_bytes_count++;
  1519. } else {
  1520. if (!buffer[i])
  1521. num_zero++;
  1522. else
  1523. num_zero = 0;
  1524. }
  1525. i++;
  1526. }
  1527. return emulation_bytes_count;
  1528. }
  1529. static u32 avc_add_emulation_bytes(const char *buffer_src, char *buffer_dst, u32 nal_size)
  1530. {
  1531. u32 i = 0, emulation_bytes_count = 0;
  1532. u8 num_zero = 0;
  1533. while (i < nal_size) {
  1534. /*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003
  1535. other than the following sequences shall not occur at any byte-aligned position:
  1536. 0x00000300
  1537. 0x00000301
  1538. 0x00000302
  1539. 0x00000303"
  1540. */
  1541. if (num_zero == 2 && buffer_src[i] < 0x04) {
  1542. /*add emulation code*/
  1543. num_zero = 0;
  1544. buffer_dst[i+emulation_bytes_count] = 0x03;
  1545. emulation_bytes_count++;
  1546. if (!buffer_src[i])
  1547. num_zero = 1;
  1548. } else {
  1549. if (!buffer_src[i])
  1550. num_zero++;
  1551. else
  1552. num_zero = 0;
  1553. }
  1554. buffer_dst[i+emulation_bytes_count] = buffer_src[i];
  1555. i++;
  1556. }
  1557. return nal_size+emulation_bytes_count;
  1558. }
  1559. #ifdef GPAC_UNUSED_FUNC
  1560. /*returns the nal_size without emulation prevention bytes*/
  1561. static u32 avc_emulation_bytes_remove_count(unsigned char *buffer, u32 nal_size)
  1562. {
  1563. u32 i = 0, emulation_bytes_count = 0;
  1564. u8 num_zero = 0;
  1565. while (i < nal_size)
  1566. {
  1567. /*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003
  1568. other than the following sequences shall not occur at any byte-aligned position:
  1569. \96 0x00000300
  1570. \96 0x00000301
  1571. \96 0x00000302
  1572. \96 0x00000303"
  1573. */
  1574. if (num_zero == 2
  1575. && buffer[i] == 0x03
  1576. && i+1 < nal_size /*next byte is readable*/
  1577. && buffer[i+1] < 0x04)
  1578. {
  1579. /*emulation code found*/
  1580. num_zero = 0;
  1581. emulation_bytes_count++;
  1582. i++;
  1583. }
  1584. if (!buffer[i])
  1585. num_zero++;
  1586. else
  1587. num_zero = 0;
  1588. i++;
  1589. }
  1590. return emulation_bytes_count;
  1591. }
  1592. #endif /*GPAC_UNUSED_FUNC*/
  1593. /*nal_size is updated to allow better error detection*/
  1594. static u32 avc_remove_emulation_bytes(const char *buffer_src, char *buffer_dst, u32 nal_size)
  1595. {
  1596. u32 i = 0, emulation_bytes_count = 0;
  1597. u8 num_zero = 0;
  1598. while (i < nal_size)
  1599. {
  1600. /*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003
  1601. other than the following sequences shall not occur at any byte-aligned position:
  1602. 0x00000300
  1603. 0x00000301
  1604. 0x00000302
  1605. 0x00000303"
  1606. */
  1607. if (num_zero == 2
  1608. && buffer_src[i] == 0x03
  1609. && i+1 < nal_size /*next byte is readable*/
  1610. && buffer_src[i+1] < 0x04)
  1611. {
  1612. /*emulation code found*/
  1613. num_zero = 0;
  1614. emulation_bytes_count++;
  1615. i++;
  1616. }
  1617. buffer_dst[i-emulation_bytes_count] = buffer_src[i];
  1618. if (!buffer_src[i])
  1619. num_zero++;
  1620. else
  1621. num_zero = 0;
  1622. i++;
  1623. }
  1624. return nal_size-emulation_bytes_count;
  1625. }
  1626. GF_EXPORT
  1627. s32 gf_media_avc_read_sps(const char *sps_data, u32 sps_size, AVCState *avc, u32 subseq_sps, u32 *vui_flag_pos)
  1628. {
  1629. AVC_SPS *sps;
  1630. u32 ChromaArrayType = 0;
  1631. s32 mb_width, mb_height, sps_id = -1;
  1632. u32 profile_idc, level_idc, pcomp, i, chroma_format_idc, cl, cr, ct, cb, luma_bd, chroma_bd;
  1633. GF_BitStream *bs;
  1634. char *sps_data_without_emulation_bytes = NULL;
  1635. u32 sps_data_without_emulation_bytes_size = 0;
  1636. /*SPS still contains emulation bytes*/
  1637. sps_data_without_emulation_bytes = gf_malloc(sps_size*sizeof(char));
  1638. sps_data_without_emulation_bytes_size = avc_remove_emulation_bytes(sps_data, sps_data_without_emulation_bytes, sps_size);
  1639. bs = gf_bs_new(sps_data_without_emulation_bytes, sps_data_without_emulation_bytes_size, GF_BITSTREAM_READ);
  1640. if (!bs) {
  1641. sps_id = -1;
  1642. goto exit;
  1643. }
  1644. if (vui_flag_pos) *vui_flag_pos = 0;
  1645. /*nal hdr*/ gf_bs_read_int(bs, 8);
  1646. profile_idc = gf_bs_read_int(bs, 8);
  1647. pcomp = gf_bs_read_int(bs, 8);
  1648. /*sanity checks*/
  1649. if (pcomp & 0x3)
  1650. goto exit;
  1651. level_idc = gf_bs_read_int(bs, 8);
  1652. /*SubsetSps is used to be sure that AVC SPS are not going to be scratched
  1653. by subset SPS. According to the SVC standard, subset SPS can have the same sps_id
  1654. than its base layer, but it does not refer to the same SPS. */
  1655. sps_id = bs_get_ue(bs) + GF_SVC_SSPS_ID_SHIFT * subseq_sps;
  1656. if (sps_id >=32) {
  1657. sps_id = -1;
  1658. goto exit;
  1659. }
  1660. chroma_format_idc = luma_bd = chroma_bd = 0;
  1661. sps = &avc->sps[sps_id];
  1662. sps->state |= subseq_sps ? AVC_SUBSPS_PARSED : AVC_SPS_PARSED;
  1663. /*High Profile and SVC*/
  1664. switch (profile_idc) {
  1665. case 100:
  1666. case 110:
  1667. case 122:
  1668. case 244:
  1669. case 44:
  1670. /*sanity checks: note1 from 7.4.2.1.1 of iso/iec 14496-10-N11084*/
  1671. if (pcomp & 0xE0)
  1672. goto exit;
  1673. case 83:
  1674. case 86:
  1675. case 118:
  1676. case 128:
  1677. chroma_format_idc = bs_get_ue(bs);
  1678. ChromaArrayType = chroma_format_idc;
  1679. if (chroma_format_idc == 3) {
  1680. u8 separate_colour_plane_flag = gf_bs_read_int(bs, 1);
  1681. /*
  1682. Depending on the value of separate_colour_plane_flag, the value of the variable ChromaArrayType is assigned as follows.
  1683. \96 If separate_colour_plane_flag is equal to 0, ChromaArrayType is set equal to chroma_format_idc.
  1684. \96 Otherwise (separate_colour_plane_flag is equal to 1), ChromaArrayType is set equal to 0.
  1685. */
  1686. if (separate_colour_plane_flag) ChromaArrayType = 0;
  1687. }
  1688. luma_bd = bs_get_ue(bs);
  1689. chroma_bd = bs_get_ue(bs);
  1690. /*qpprime_y_zero_transform_bypass_flag = */ gf_bs_read_int(bs, 1);
  1691. /*seq_scaling_matrix_present_flag*/
  1692. if (gf_bs_read_int(bs, 1)) {
  1693. u32 k;
  1694. for (k=0; k<8; k++) {
  1695. if (gf_bs_read_int(bs, 1)) {
  1696. u32 z, last = 8, next = 8;
  1697. u32 sl = k<6 ? 16 : 64;
  1698. for (z=0; z<sl; z++) {
  1699. if (next) {
  1700. s32 delta = bs_get_se(bs);
  1701. next = (last + delta + 256) % 256;
  1702. }
  1703. last = next ? next : last;
  1704. }
  1705. }
  1706. }
  1707. }
  1708. break;
  1709. default :
  1710. ChromaArrayType = chroma_format_idc = 1;
  1711. break;
  1712. }
  1713. sps->profile_idc = profile_idc;
  1714. sps->level_idc = level_idc;
  1715. sps->prof_compat = pcomp;
  1716. sps->log2_max_frame_num = bs_get_ue(bs) + 4;
  1717. sps->poc_type = bs_get_ue(bs);
  1718. sps->chroma_format = chroma_format_idc;
  1719. sps->luma_bit_depth_m8 = luma_bd;
  1720. sps->chroma_bit_depth_m8 = chroma_bd;
  1721. if (sps->poc_type == 0) {
  1722. sps->log2_max_poc_lsb = bs_get_ue(bs) + 4;
  1723. } else if(sps->poc_type == 1) {
  1724. sps->delta_pic_order_always_zero_flag = gf_bs_read_int(bs, 1);
  1725. sps->offset_for_non_ref_pic = bs_get_se(bs);
  1726. sps->offset_for_top_to_bottom_field = bs_get_se(bs);
  1727. sps->poc_cycle_length = bs_get_ue(bs);
  1728. for(i=0; i<sps->poc_cycle_length; i++) sps->offset_f

Large files files are truncated, but you can click here to view the full file