bsfeeder_framesize_impl.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. //--=========================================================================--
  3. // This file is a part of QC Tool project
  4. //-----------------------------------------------------------------------------
  5. //
  6. // This confidential and proprietary software may be used only
  7. // as authorized by a licensing agreement from Chips&Media Inc.
  8. // In the event of publication, the following notice is applicable:
  9. //
  10. // (C) COPYRIGHT 2004 - 2011 CHIPS&MEDIA INC.
  11. // ALL RIGHTS RESERVED
  12. //
  13. // The entire notice above must be reproduced on all authorized
  14. // copies.
  15. //
  16. //--=========================================================================--
  17. #include "main_helper.h"
  18. #ifdef SUPPORT_FFMPEG_DEMUX
  19. #include "theora_parser.h"
  20. #include <libavformat/avformat.h>
  21. #define RCV_V2
  22. #define IS_VP9_SUPERFRAME(__header) ((__header & 0xe0) == 0xc0)
  23. #define MAX_VP9_SUPERFRAME_INDEX_SIZE 34
  24. #define VP9_MAX_SUBFRAMES 8
  25. typedef struct VP9Superframe {
  26. Uint32 nframes;
  27. Uint32 frameSize[VP9_MAX_SUBFRAMES];
  28. void* frames[VP9_MAX_SUBFRAMES]; /* A superframe has multiple frames up to 8 frames. */
  29. Uint32 currentIndex;
  30. } VP9Superframe;
  31. typedef struct ReaderContext {
  32. AVFormatContext* avContext;
  33. BOOL isFirstPacket;
  34. Uint32 videoIndex;
  35. Uint32 standard;
  36. Uint32 mp4ClassId;
  37. tho_parser_t* theora;
  38. Uint8* tempBuffer;
  39. Uint32 tempRdPtr;
  40. Uint32 tempWrPtr;
  41. Uint32 seqWidth;
  42. Uint32 seqHeight;
  43. VP9Superframe superframe;
  44. } ReaderContext;
  45. static Uint32 u_bytes(
  46. Uint8* data,
  47. Uint32 len
  48. )
  49. {
  50. Uint32 i;
  51. Uint32 val=0;
  52. for (i=0; i<len; i++) {
  53. val |= data[i] << (i*8);
  54. }
  55. return val;
  56. }
  57. static BOOL VP9ParseSuperframe(
  58. void* chunk,
  59. Uint32 size,
  60. VP9Superframe* superframe
  61. )
  62. {
  63. Uint32 startPos;
  64. Uint32 frameSizeLength = 0;
  65. Uint32 numFrames, totalSubframeSize = 0;
  66. Uint32 i;
  67. Uint8* pData = NULL, superframeMarker;
  68. startPos = size - 1;
  69. pData = (Uint8*)chunk;
  70. pData = &pData[startPos];
  71. superframeMarker = *pData;
  72. frameSizeLength = ((*pData>>3) & 0x03) + 1;
  73. numFrames = (*pData&0x07) + 1;
  74. pData -= frameSizeLength * numFrames + 1;
  75. /* Check first SUPERFRAME_MARKER */
  76. if (*pData != superframeMarker) {
  77. VLOG(ERR, "INVALID POST SUPERFRAME_MARKER\n");
  78. return FALSE;
  79. }
  80. pData++;
  81. for (i=0; i<numFrames; i++) {
  82. superframe->frameSize[i] = u_bytes(pData, frameSizeLength);
  83. pData += frameSizeLength;
  84. }
  85. /* Check size */
  86. for (i=0; i<numFrames; i++) {
  87. totalSubframeSize += superframe->frameSize[i];
  88. }
  89. if (totalSubframeSize >= size) {
  90. VLOG(ERR, "TOTAL SIZE OF SUBFRAMES IS BIGGER THAN CHUNK SIZE\n");
  91. return FALSE;
  92. }
  93. pData = (Uint8*)chunk;
  94. for (i=0; i<numFrames; i++) {
  95. superframe->frames[i] = (void*)osal_malloc(superframe->frameSize[i]);
  96. osal_memcpy(superframe->frames[i], (void*)pData, superframe->frameSize[i]);
  97. pData += superframe->frameSize[i];
  98. }
  99. superframe->currentIndex = 0;
  100. superframe->nframes = numFrames;
  101. return TRUE;
  102. }
  103. static Int32 BuildSeqHeader(
  104. Uint8* pbHeader,
  105. const CodStd codStd,
  106. const AVStream* st,
  107. Int32* sizelength
  108. )
  109. {
  110. /*lint -save -e438 */
  111. AVCodecContext* avc = st->codec;
  112. Uint8* pbMetaData = avc->extradata;
  113. Int32 nMetaData = avc->extradata_size;
  114. Uint8* p = pbMetaData;
  115. Uint8 *a = p + 4 - ((long) p & 3);
  116. Uint8* t = pbHeader;
  117. Int32 size;
  118. Int32 fourcc;
  119. Int32 sps, pps, i, nal;
  120. Int32 frameRate = 0;
  121. fourcc = avc->codec_tag;
  122. if (!fourcc)
  123. fourcc = ConvCodecIdToFourcc(avc->codec_id);
  124. if (st->avg_frame_rate.den && st->avg_frame_rate.num)
  125. frameRate = (Int32)((double)st->avg_frame_rate.num/(double)st->avg_frame_rate.den);
  126. size = 0;
  127. *sizelength = 4; // default size length(in bytes) = 4
  128. if (codStd == STD_AVC || codStd == STD_AVS) {
  129. if (nMetaData > 1 && pbMetaData && pbMetaData[0] == 0x01) {
  130. // check mov/mo4 file format stream
  131. p += 4;
  132. *sizelength = (*p++ & 0x3) + 1;
  133. sps = (*p & 0x1f); // Number of sps
  134. p++;
  135. for (i = 0; i < sps; i++) {
  136. nal = (*p << 8) + *(p + 1) + 2;
  137. PUT_BYTE(t, 0x00);
  138. PUT_BYTE(t, 0x00);
  139. PUT_BYTE(t, 0x00);
  140. PUT_BYTE(t, 0x01);
  141. PUT_BUFFER(t, p+2, nal-2);
  142. p += nal;
  143. size += (nal - 2 + 4); // 4 => length of start code to be inserted
  144. }
  145. pps = *(p++); // number of pps
  146. for (i = 0; i < pps; i++)
  147. {
  148. nal = (*p << 8) + *(p + 1) + 2;
  149. PUT_BYTE(t, 0x00);
  150. PUT_BYTE(t, 0x00);
  151. PUT_BYTE(t, 0x00);
  152. PUT_BYTE(t, 0x01);
  153. PUT_BUFFER(t, p+2, nal-2);
  154. p += nal;
  155. size += (nal - 2 + 4); // 4 => length of start code to be inserted
  156. }
  157. }
  158. else if(nMetaData > 3) {
  159. size = -1;// return to meaning of invalid stream data;
  160. for (; p < a; p++) {
  161. if (p[0] == 0 && p[1] == 0 && p[2] == 1) {
  162. // find startcode
  163. size = avc->extradata_size;
  164. PUT_BUFFER(pbHeader, pbMetaData, size);
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. else if (codStd == STD_HEVC) {
  171. if (nMetaData > 1 && pbMetaData && pbMetaData[0] == 0x01) {
  172. static const Uint8 nalu_header[4] = { 0, 0, 0, 1 };
  173. Int32 numOfArrays = 0;
  174. Uint16 numNalus = 0;
  175. Uint16 nalUnitLength = 0;
  176. Uint32 offset = 0;
  177. p += 21;
  178. *sizelength = (*p++ & 0x3) + 1;
  179. numOfArrays = *p++;
  180. while(numOfArrays--) {
  181. p++; // NAL type
  182. numNalus = (*p << 8) + *(p + 1);
  183. p+=2;
  184. for(i = 0;i < numNalus;i++)
  185. {
  186. nalUnitLength = (*p << 8) + *(p + 1);
  187. p+=2;
  188. //if(i == 0)
  189. {
  190. osal_memcpy(pbHeader + offset, nalu_header, 4);
  191. offset += 4;
  192. osal_memcpy(pbHeader + offset, p, nalUnitLength);
  193. offset += nalUnitLength;
  194. }
  195. p += nalUnitLength;
  196. }
  197. }
  198. size = offset;
  199. }
  200. else if(nMetaData > 3)
  201. {
  202. size = -1;// return to meaning of invalid stream data;
  203. for (; p < a; p++)
  204. {
  205. if (p[0] == 0 && p[1] == 0 && p[2] == 1) // find startcode
  206. {
  207. size = avc->extradata_size;
  208. PUT_BUFFER(pbHeader, pbMetaData, size);
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. else if (codStd == STD_VC1)
  215. {
  216. if (!fourcc)
  217. return -1;
  218. if (fourcc == MKTAG('W', 'V', 'C', '1') || fourcc == MKTAG('W', 'M', 'V', 'A')) //VC AP
  219. {
  220. size = nMetaData;
  221. PUT_BUFFER(pbHeader, pbMetaData, size);
  222. //if there is no seq startcode in pbMetatData. VPU will be failed at seq_init stage.
  223. }
  224. else
  225. {
  226. #ifdef RCV_V2
  227. PUT_LE32(pbHeader, ((0xC5 << 24)|0));
  228. size += 4; //version
  229. PUT_LE32(pbHeader, nMetaData);
  230. size += 4;
  231. PUT_BUFFER(pbHeader, pbMetaData, nMetaData);
  232. size += nMetaData;
  233. PUT_LE32(pbHeader, avc->height);
  234. size += 4;
  235. PUT_LE32(pbHeader, avc->width);
  236. size += 4;
  237. PUT_LE32(pbHeader, 12);
  238. size += 4;
  239. PUT_LE32(pbHeader, 2 << 29 | 1 << 28 | 0x80 << 24 | 1 << 0);
  240. size += 4; // STRUCT_B_FRIST (LEVEL:3|CBR:1:RESERVE:4:HRD_BUFFER|24)
  241. PUT_LE32(pbHeader, avc->bit_rate);
  242. size += 4; // hrd_rate
  243. PUT_LE32(pbHeader, frameRate);
  244. size += 4; // frameRate
  245. #else //RCV_V1
  246. PUT_LE32(pbHeader, (0x85 << 24) | 0x00);
  247. size += 4; //frames count will be here
  248. PUT_LE32(pbHeader, nMetaData);
  249. size += 4;
  250. PUT_BUFFER(pbHeader, pbMetaData, nMetaData);
  251. size += nMetaData;
  252. PUT_LE32(pbHeader, avc->height);
  253. size += 4;
  254. PUT_LE32(pbHeader, avc->width);
  255. size += 4;
  256. #endif
  257. }
  258. }
  259. else if (codStd == STD_RV)
  260. {
  261. Int32 st_size =0;
  262. if (!fourcc)
  263. return -1;
  264. if (fourcc != MKTAG('R','V','3','0') && fourcc != MKTAG('R','V','4','0'))
  265. return -1;
  266. size = 26 + nMetaData;
  267. PUT_BE32(pbHeader, size); //Length
  268. PUT_LE32(pbHeader, MKTAG('V', 'I', 'D', 'O')); //MOFTag
  269. PUT_LE32(pbHeader, fourcc); //SubMOFTagl
  270. PUT_BE16(pbHeader, avc->width);
  271. PUT_BE16(pbHeader, avc->height);
  272. PUT_BE16(pbHeader, 0x0c); //BitCount;
  273. PUT_BE16(pbHeader, 0x00); //PadWidth;
  274. PUT_BE16(pbHeader, 0x00); //PadHeight;
  275. PUT_LE32(pbHeader, frameRate);
  276. PUT_BUFFER(pbHeader, pbMetaData, nMetaData); //OpaqueDatata
  277. size += st_size; //add for startcode pattern.
  278. }
  279. else if (codStd == STD_DIV3) {
  280. // not implemented yet
  281. if (!nMetaData) {
  282. PUT_LE32(pbHeader, MKTAG('C', 'N', 'M', 'V')); //signature 'CNMV'
  283. PUT_LE16(pbHeader, 0x00); //version
  284. PUT_LE16(pbHeader, 0x20); //length of header in bytes
  285. PUT_LE32(pbHeader, MKTAG('D', 'I', 'V', '3')); //codec FourCC
  286. PUT_LE16(pbHeader, avc->width); //width
  287. PUT_LE16(pbHeader, avc->height); //height
  288. PUT_LE32(pbHeader, st->avg_frame_rate.num); //frame rate
  289. PUT_LE32(pbHeader, st->avg_frame_rate.den); //time scale(?)
  290. PUT_LE32(pbHeader, st->nb_index_entries); //number of frames in file
  291. PUT_LE32(pbHeader, 0); //unused
  292. size += 32;
  293. return size;
  294. }
  295. PUT_BE32(pbHeader, nMetaData);
  296. size += 4;
  297. PUT_BUFFER(pbHeader, pbMetaData, nMetaData);
  298. size += nMetaData;
  299. }
  300. else if (codStd == STD_VP8) {
  301. PUT_LE32(pbHeader, MKTAG('D', 'K', 'I', 'F')); //signature 'DKIF'
  302. PUT_LE16(pbHeader, 0x00); //version
  303. PUT_LE16(pbHeader, 0x20); //length of header in bytes
  304. PUT_LE32(pbHeader, MKTAG('V', 'P', '8', '0')); //codec FourCC
  305. PUT_LE16(pbHeader, avc->width); //width
  306. PUT_LE16(pbHeader, avc->height); //height
  307. PUT_LE32(pbHeader, st->avg_frame_rate.num); //frame rate
  308. PUT_LE32(pbHeader, st->avg_frame_rate.den); //time scale(?)
  309. PUT_LE32(pbHeader, st->nb_index_entries); //number of frames in file
  310. PUT_LE32(pbHeader, 0); //unused
  311. size += 32;
  312. }
  313. else if (codStd == STD_VP9) {
  314. PUT_LE32(pbHeader, MKTAG('D', 'K', 'I', 'F')); //signature 'DKIF'
  315. PUT_LE16(pbHeader, 0x00); //version
  316. PUT_LE16(pbHeader, 0x20); //length of header in bytes
  317. PUT_LE32(pbHeader, MKTAG('V', 'P', '9', '0')); //codec FourCC
  318. PUT_LE16(pbHeader, avc->width); //width
  319. PUT_LE16(pbHeader, avc->height); //height
  320. PUT_LE32(pbHeader, st->avg_frame_rate.num); //frame rate
  321. PUT_LE32(pbHeader, st->avg_frame_rate.den); //time scale(?)
  322. PUT_LE32(pbHeader, st->nb_index_entries); //number of frames in file
  323. PUT_LE32(pbHeader, 0); //unused
  324. size += 32;
  325. }
  326. else {
  327. PUT_BUFFER(pbHeader, pbMetaData, nMetaData);
  328. size = nMetaData;
  329. }
  330. return size;
  331. /*lint -restore */
  332. }
  333. static Int32 BuildPicHeader(
  334. Uint8* pbHeader,
  335. const CodStd codStd,
  336. const AVStream* st,
  337. const AVPacket* pkt,
  338. Int32 sizelength
  339. )
  340. {
  341. AVCodecContext* avc = st->codec;
  342. Uint8* pbChunk = pkt->data;
  343. Int32 size = 0;
  344. Int32 fourcc;
  345. Int32 cSlice, nSlice;
  346. Int32 i, val, offset;
  347. BOOL hasStartCode = 0;
  348. size = 0;
  349. offset = 0;
  350. fourcc = avc->codec_tag;
  351. if (!fourcc)
  352. fourcc = ConvCodecIdToFourcc(avc->codec_id);
  353. if (codStd == STD_VC1) {
  354. if (!fourcc)
  355. return -1;
  356. if (fourcc == MKTAG('W', 'V', 'C', '1') || fourcc == MKTAG('W', 'M', 'V', 'A')) {
  357. if (pbChunk[0] != 0 || pbChunk[1] != 0 || pbChunk[2] != 1) {
  358. // check start code as prefix (0x00, 0x00, 0x01)
  359. pbHeader[0] = 0x00;
  360. pbHeader[1] = 0x00;
  361. pbHeader[2] = 0x01;
  362. pbHeader[3] = 0x0D; // replace to the correct picture header to indicate as frame
  363. size += 4;
  364. }
  365. }
  366. else {
  367. PUT_LE32(pbHeader, pkt->size | ((pkt->flags & AV_PKT_FLAG_KEY) ? 0x80000000 : 0));
  368. size += 4;
  369. #ifdef RCV_V2
  370. if (AV_NOPTS_VALUE == pkt->pts) {
  371. PUT_LE32(pbHeader, 0);
  372. }
  373. else {
  374. PUT_LE32(pbHeader, (int)((double)(pkt->pts/st->time_base.den))); // milli_sec
  375. }
  376. size += 4;
  377. #endif
  378. }
  379. }
  380. else if (codStd == STD_HEVC) {
  381. if(pkt->size < 5)
  382. return 0;
  383. if (!(avc->extradata_size > 1 && avc->extradata && avc->extradata[0] == 0x01))
  384. {
  385. const Uint8 *pbEnd = pbChunk + 4 - ((intptr_t)pbChunk & 3);
  386. for (; pbChunk < pbEnd ; pbChunk++) {
  387. if (pbChunk[0] == 0 && pbChunk[1] == 0 && pbChunk[2] == 1) {
  388. hasStartCode = TRUE;
  389. break;
  390. }
  391. }
  392. }
  393. if ((!hasStartCode && avc->extradata[0] == 0x01) ||
  394. (avc->extradata_size > 1 && avc->extradata && avc->extradata[0] == 0x01)) {
  395. // check sequence metadata if the stream is mov/mo4 file format.
  396. pbChunk = pkt->data;
  397. while (offset < pkt->size) {
  398. if(sizelength == 3) {
  399. nSlice = pbChunk[offset] << 16 | pbChunk[offset+1] << 8 | pbChunk[offset+2];
  400. pbChunk[offset] = 0x00;
  401. pbChunk[offset+1] = 0x00;
  402. pbChunk[offset+2] = 0x01;
  403. offset += 3;
  404. }
  405. else {// sizeLength = 4
  406. nSlice = pbChunk[offset] << 24 | pbChunk[offset+1] << 16 | pbChunk[offset+2] << 8 | pbChunk[offset+3];
  407. pbChunk[offset] = 0x00;
  408. pbChunk[offset+1] = 0x00;
  409. pbChunk[offset+2] = 0x00;
  410. pbChunk[offset+3] = 0x01; //replace size to startcode
  411. offset += 4;
  412. }
  413. switch ((pbChunk[offset]&0x7E)>>1) { /* NAL unit */
  414. case 39: /* PREFIX SEI */
  415. case 40: /* SUFFIX SEI */
  416. case 32: /* VPS */
  417. case 33: /* SPS */
  418. case 34: /* PPS */
  419. /* check next */
  420. break;
  421. }
  422. offset += nSlice;
  423. }
  424. }
  425. }
  426. else if (codStd == STD_RV) {
  427. int st_size = 0;
  428. if (!fourcc)
  429. return -1;
  430. if (fourcc != MKTAG('R','V','3','0') && fourcc != MKTAG('R','V','4','0')) // RV version 8, 9 , 10
  431. return -1;
  432. cSlice = pbChunk[0] + 1;
  433. nSlice = pkt->size - 1 - (cSlice * 8);
  434. size = 20 + (cSlice*8);
  435. PUT_BE32(pbHeader, nSlice);
  436. if (AV_NOPTS_VALUE == pkt->pts) {
  437. PUT_LE32(pbHeader, 0);
  438. }
  439. else {
  440. PUT_LE32(pbHeader, (int)((double)(pkt->pts/st->time_base.den))); // milli_sec
  441. }
  442. PUT_BE16(pbHeader, avc->frame_number);
  443. PUT_BE16(pbHeader, 0x02); //Flags
  444. PUT_BE32(pbHeader, 0x00); //LastPacket
  445. PUT_BE32(pbHeader, cSlice); //NumSegments
  446. offset = 1;
  447. for (i = 0; i < (int) cSlice; i++)
  448. {
  449. val = (pbChunk[offset+3] << 24) | (pbChunk[offset+2] << 16) | (pbChunk[offset+1] << 8) | pbChunk[offset];
  450. PUT_BE32(pbHeader, val); //isValid
  451. offset += 4;
  452. val = (pbChunk[offset+3] << 24) | (pbChunk[offset+2] << 16) | (pbChunk[offset+1] << 8) | pbChunk[offset];
  453. PUT_BE32(pbHeader, val); //Offset
  454. offset += 4;
  455. }
  456. size += st_size;
  457. }
  458. else if (codStd == STD_AVC) {
  459. if(pkt->size < 5)
  460. return 0;
  461. if (!(avc->extradata_size > 1 && avc->extradata && avc->extradata[0] == 0x01)) {
  462. const Uint8 *pbEnd = pbChunk + 4 - ((intptr_t)pbChunk & 3);
  463. for (; pbChunk < pbEnd ; pbChunk++) {
  464. if (pbChunk[0] == 0 && pbChunk[1] == 0 && pbChunk[2] == 1) {
  465. hasStartCode = 1;
  466. break;
  467. }
  468. }
  469. }
  470. if ((!hasStartCode && avc->extradata[0] == 0x01) ||
  471. (avc->extradata_size > 1 && avc->extradata && avc->extradata[0] == 0x01)) {
  472. // check sequence metadata if the stream is mov/mo4 file format.
  473. pbChunk = pkt->data;
  474. while (offset < pkt->size) {
  475. if(sizelength == 3) {
  476. nSlice = pbChunk[offset] << 16 | pbChunk[offset+1] << 8 | pbChunk[offset+2];
  477. pbChunk[offset] = 0x00;
  478. pbChunk[offset+1] = 0x00;
  479. pbChunk[offset+2] = 0x01;
  480. offset += 3;
  481. }
  482. else { // size length = 4
  483. nSlice = pbChunk[offset] << 24 | pbChunk[offset+1] << 16 | pbChunk[offset+2] << 8 | pbChunk[offset+3];
  484. pbChunk[offset] = 0x00;
  485. pbChunk[offset+1] = 0x00;
  486. pbChunk[offset+2] = 0x00;
  487. pbChunk[offset+3] = 0x01; //replace size to startcode
  488. offset += 4;
  489. }
  490. switch (pbChunk[offset]&0x1f) { /* NAL unit */
  491. case 6: /* SEI */
  492. case 7: /* SPS */
  493. case 8: /* PPS */
  494. case 9: /* AU */
  495. /* check next */
  496. break;
  497. }
  498. offset += nSlice;
  499. }
  500. }
  501. }
  502. else if(codStd == STD_AVS) {
  503. const Uint8* pbEnd;
  504. if(pkt->size < 5)
  505. return 0;
  506. pbEnd = pbChunk + 4 - ((intptr_t)pbChunk & 3);
  507. for (; pbChunk < pbEnd ; pbChunk++) {
  508. if (pbChunk[0] == 0 && pbChunk[1] == 0 && pbChunk[2] == 1) {
  509. hasStartCode = 1;
  510. break;
  511. }
  512. }
  513. if(hasStartCode == 0) {
  514. pbChunk = pkt->data;
  515. while (offset < pkt->size) {
  516. nSlice = pbChunk[offset] << 24 | pbChunk[offset+1] << 16 | pbChunk[offset+2] << 8 | pbChunk[offset+3];
  517. pbChunk[offset] = 0x00;
  518. pbChunk[offset+1] = 0x00;
  519. pbChunk[offset+2] = 0x00;
  520. pbChunk[offset+3] = 0x00; //replace size to startcode
  521. pbChunk[offset+4] = 0x01;
  522. offset += 4;
  523. switch (pbChunk[offset]&0x1f) /* NAL unit */
  524. {
  525. case 6: /* SEI */
  526. case 7: /* SPS */
  527. case 8: /* PPS */
  528. case 9: /* AU */
  529. /* check next */
  530. break;
  531. }
  532. offset += nSlice;
  533. }
  534. }
  535. }
  536. else if (codStd == STD_DIV3 || codStd == STD_VP8 || codStd == STD_VP9) {
  537. PUT_LE32(pbHeader,pkt->size);
  538. PUT_LE32(pbHeader,0);
  539. PUT_LE32(pbHeader,0);
  540. size += 12;
  541. }
  542. return size;
  543. }
  544. static Int32 MakeupTheoraPacket(
  545. tho_parser_t* theora,
  546. BSChunk* packet,
  547. AVPacket* avPacket,
  548. Uint32 seqSize
  549. )
  550. {
  551. size_t size;
  552. Int32 ret;
  553. Uint8* stream = (Uint8*)packet->data;
  554. ret = theora->read_frame(theora->handle, avPacket->data, avPacket->size);
  555. if (ret < 0) {
  556. VLOG(ERR, "%s:%d failed to read theora frame\n", __FUNCTION__, __LINE__);
  557. return -1;
  558. }
  559. size = theora_make_stream(theora->handle, stream+seqSize, PIC_RUN);
  560. return (size + seqSize);
  561. }
  562. void* BSFeederFrameSize_Create(
  563. const char* path,
  564. CodStd codecId,
  565. CodStd* retCodecId,
  566. Uint32* retMp4ClassId,
  567. Uint32* retSeqWidth,
  568. Uint32* retSeqHeight
  569. )
  570. {
  571. /*lint -esym(438, avContext) */
  572. ReaderContext* ffmpegReader = NULL;
  573. AVFormatContext* avContext = NULL;
  574. AVCodecContext* codec = NULL;
  575. AVInputFormat* fmt = NULL;
  576. Int32 error;
  577. Int32 videoIndex;
  578. Uint32 mp4ClassId;
  579. Int32 standard;
  580. av_register_all();
  581. if ((avContext=avformat_alloc_context()) == NULL) {
  582. return NULL;
  583. }
  584. avContext->flags |= CODEC_FLAG_TRUNCATED;
  585. if ((error=avformat_open_input(&avContext, path, fmt, NULL))) {
  586. VLOG(ERR, "%s:%d failed to av_open_input_file error(%d), %s\n",
  587. __FILE__, __LINE__, error, path);
  588. goto __failed_to_end;
  589. }
  590. if ((error=avformat_find_stream_info(avContext, NULL)) < 0) {
  591. VLOG(ERR, "%s:%d failed to avformat_find_stream_info. error(%d)\n",
  592. __FUNCTION__, __LINE__, error);
  593. goto __failed_to_end;
  594. }
  595. videoIndex = av_find_best_stream(avContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  596. if (videoIndex < 0) {
  597. VLOG(ERR, "%s:%d failed to av_find_best_stream.\n", __FUNCTION__, __LINE__);
  598. goto __failed_to_end;
  599. }
  600. codec = avContext->streams[videoIndex]->codec;
  601. standard = ConvFOURCCToCodStd(codec->codec_tag);
  602. if (standard == -1)
  603. standard = ConvCodecIdToCodStd(codec->codec_id);
  604. mp4ClassId = ConvFOURCCToMp4Class(codec->codec_tag);
  605. if (mp4ClassId == -1)
  606. mp4ClassId = ConvCodecIdToMp4Class(codec->codec_id);
  607. if (standard != STD_MPEG4) {
  608. mp4ClassId = 0;
  609. }
  610. if (retCodecId != NULL) {
  611. *retCodecId = (CodStd)standard;
  612. }
  613. if (retMp4ClassId != NULL) {
  614. *retMp4ClassId = mp4ClassId;
  615. }
  616. if (retSeqWidth != NULL && retSeqHeight != NULL) {
  617. *retSeqWidth = codec->width;
  618. *retSeqHeight = codec->height;
  619. }
  620. if ((ffmpegReader=(ReaderContext*)osal_malloc(sizeof(ReaderContext))) == NULL)
  621. goto __failed_to_end;
  622. ffmpegReader->standard = standard;
  623. ffmpegReader->mp4ClassId = mp4ClassId;
  624. ffmpegReader->avContext = avContext;
  625. ffmpegReader->videoIndex = videoIndex;
  626. ffmpegReader->isFirstPacket = TRUE;
  627. ffmpegReader->tempBuffer = NULL;
  628. ffmpegReader->tempRdPtr = 0;
  629. ffmpegReader->tempWrPtr = 0;
  630. ffmpegReader->theora = NULL;
  631. osal_memset((void*)&ffmpegReader->superframe, 0x00, sizeof(VP9Superframe));
  632. if (standard == STD_THO || standard == STD_VP3) {
  633. theora_parser_init((void**)&ffmpegReader->theora);
  634. fuck
  635. }
  636. return (void*)ffmpegReader;
  637. __failed_to_end:
  638. if (avContext) {
  639. avformat_free_context(avContext);
  640. avContext = NULL;
  641. }
  642. if (ffmpegReader) {
  643. if (ffmpegReader->theora)
  644. ffmpegReader->theora->close(ffmpegReader->theora);
  645. osal_free(ffmpegReader);
  646. }
  647. return NULL;
  648. /*lint +esym(438, avContext) */
  649. }
  650. BOOL BSFeederFrameSize_Destroy(
  651. void* feeder
  652. )
  653. {
  654. ReaderContext* ctx = (ReaderContext*)feeder;
  655. Uint32 i;
  656. if (ctx == NULL) {
  657. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  658. return FALSE;
  659. }
  660. if (ctx->avContext)
  661. avformat_close_input(&ctx->avContext);
  662. if (ctx->theora)
  663. ctx->theora->close(ctx->theora);
  664. for (i=0; i<ctx->superframe.nframes; i++) {
  665. if (ctx->superframe.frames[i] != NULL) {
  666. free(ctx->superframe.frames[i]);
  667. }
  668. }
  669. osal_free(ctx);
  670. return TRUE;
  671. }
  672. Int32 BSFeederFrameSize_Act(
  673. void* feeder,
  674. BSChunk* packet
  675. )
  676. {
  677. ReaderContext* ffmpegReader = (ReaderContext*)feeder;
  678. AVFormatContext* avFormatContext = ffmpegReader->avContext;
  679. AVPacket avpacket;
  680. Int32 error;
  681. Uint8* seqHeader = NULL;
  682. Uint8* picHeader = NULL;
  683. Uint8* ptr;
  684. Int32 seqHeaderSize;
  685. Int32 picHeaderSize;
  686. Uint32 vindex, size, thoSeqSize;
  687. Int32 retSize = -1;
  688. Int32 packetSize = -1;
  689. if (ffmpegReader->tempBuffer) {
  690. goto __consume_tempBuffer;
  691. }
  692. if (ffmpegReader->standard == STD_VP9) {
  693. VP9Superframe* superframe = &ffmpegReader->superframe;
  694. if (superframe->nframes > 0) {
  695. Uint32 index = superframe->currentIndex;
  696. if (index < superframe->nframes) {
  697. osal_memcpy(packet->data, superframe->frames[index], superframe->frameSize[index]);
  698. packet->size = superframe->frameSize[index];
  699. superframe->currentIndex++;
  700. return packet->size;
  701. }
  702. else {
  703. Uint32 i;
  704. for (i=0; i<VP9_MAX_SUBFRAMES; i++) {
  705. if (superframe->frames[i] != NULL) {
  706. osal_free(superframe->frames[i]);
  707. }
  708. }
  709. osal_memset((void*)superframe, 0x00, sizeof(VP9Superframe));
  710. }
  711. }
  712. }
  713. seqHeaderSize = 0;
  714. picHeaderSize = 0;
  715. thoSeqSize = 0;
  716. av_init_packet(&avpacket);
  717. while (TRUE) {
  718. error = av_read_frame(avFormatContext, &avpacket);
  719. if (error < 0) {
  720. if (error == AVERROR_EOF || avFormatContext->pb->eof_reached == TRUE) {
  721. packet->eos = TRUE;
  722. return 0;
  723. }
  724. else {
  725. VLOG(ERR, "%s:%d failed to av_read_frame error(0x%08x)\n",
  726. __FUNCTION__, __LINE__, error);
  727. goto __end_read;
  728. }
  729. }
  730. if (avpacket.stream_index != ffmpegReader->videoIndex)
  731. continue;
  732. break;
  733. }
  734. if (avpacket.size == 0)
  735. return 0;
  736. if (avpacket.size >= (signed)packet->size )
  737. {
  738. VLOG(ERR, "one packet size(%d) is bigger than STREAM_BUF_SIZE(%d)\n", avpacket.size, packet->size);
  739. return -1;
  740. }
  741. osal_memset(packet->data, 0x00, packet->size);
  742. vindex = ffmpegReader->videoIndex;
  743. if (ffmpegReader->isFirstPacket) {
  744. AVCodecContext* codec;
  745. tho_parser_t* theora = ffmpegReader->theora;
  746. Int32 ret;
  747. struct {
  748. int frameWidth ;
  749. int frameHeight ;
  750. int picWidth ;
  751. int picHeight ;
  752. int picOffsetX ;
  753. int picOffsetY ;
  754. } theoraScaleInfo;
  755. codec = ffmpegReader->avContext->streams[vindex]->codec;
  756. seqHeader = (Uint8*)osal_malloc(codec->extradata_size + 1024);
  757. if (seqHeader == NULL) {
  758. goto __end_read;
  759. }
  760. osal_memset((void*)seqHeader, 0x00, codec->extradata_size + 1024);
  761. seqHeaderSize = BuildSeqHeader(seqHeader, (CodStd)ffmpegReader->standard,
  762. ffmpegReader->avContext->streams[vindex], &retSize);
  763. if (seqHeaderSize < 0) {
  764. VLOG(ERR, "%s:%d Can't make sequence header!\n", __FUNCTION__, __LINE__);
  765. packetSize = -1;
  766. goto __end_read;
  767. }
  768. if (ffmpegReader->standard == STD_THO || ffmpegReader->standard == STD_VP3) {
  769. size = seqHeaderSize;
  770. if (ffmpegReader->standard == STD_VP3) {
  771. size = 0;
  772. }
  773. ret = theora->open(theora->handle, seqHeader, size, (Int32*)&theoraScaleInfo);
  774. if (ret < 0) {
  775. VLOG(ERR, "%s:%d failed to open theora parser error(%d)\n", __FUNCTION__, __LINE__, ret);
  776. goto __end_read;
  777. }
  778. thoSeqSize = theora_make_stream(theora->handle, (Uint8*)packet->data, DEC_SEQ_INIT);
  779. osal_free(seqHeader);
  780. seqHeader = NULL;
  781. seqHeaderSize = 0;
  782. }
  783. ffmpegReader->isFirstPacket = FALSE;
  784. }
  785. picHeader = (Uint8*)osal_malloc(1024);
  786. picHeaderSize = BuildPicHeader(picHeader, (CodStd)ffmpegReader->standard,
  787. ffmpegReader->avContext->streams[vindex], &avpacket, 0);
  788. if (picHeaderSize < 0) {
  789. VLOG(ERR, "%s:%d failed to build picture header\n", __FUNCTION__, __LINE__);
  790. goto __end_read;
  791. }
  792. ptr = avpacket.data;
  793. size = avpacket.size;
  794. switch (ffmpegReader->standard) {
  795. case STD_RV:
  796. if (seqHeaderSize)
  797. osal_memcpy((char*)packet->data, seqHeader, seqHeaderSize);
  798. if (picHeaderSize)
  799. osal_memcpy((char*)packet->data+seqHeaderSize, picHeader, picHeaderSize);
  800. if (ffmpegReader->standard == STD_RV) {
  801. int cSlice = ptr[0] + 1;
  802. int nSlice = avpacket.size - 1 - (cSlice*8);
  803. ptr += (1+(cSlice*8));
  804. size = nSlice;
  805. }
  806. osal_memcpy((char*)packet->data+seqHeaderSize+picHeaderSize, ptr, size);
  807. packetSize = seqHeaderSize + picHeaderSize + size;
  808. break;
  809. case STD_THO:
  810. case STD_VP3:
  811. packetSize = MakeupTheoraPacket(ffmpegReader->theora, packet, &avpacket, thoSeqSize);
  812. break;
  813. case STD_VP9:
  814. packet->size = size;
  815. osal_memcpy((char *)packet->data, ptr, size);
  816. packetSize = size;
  817. break;
  818. default:
  819. if (picHeaderSize)
  820. osal_memcpy((char*)packet->data, picHeader, picHeaderSize);
  821. osal_memcpy((char*)packet->data+picHeaderSize, ptr, size);
  822. packetSize = picHeaderSize + size;
  823. break;
  824. }
  825. if (avFormatContext->pb->eof_reached && avFormatContext->packet_buffer == NULL) {
  826. packet->eos = TRUE;
  827. }
  828. // Sequence header data should be only one chunk data unit.
  829. // In case of RV, 1st chunk should be Sequence header + 1st frame.
  830. if (ffmpegReader->standard != STD_VP9 && ffmpegReader->standard != STD_RV) {
  831. if (seqHeaderSize > 0) {
  832. ffmpegReader->tempBuffer = (Uint8*)osal_malloc(packetSize);
  833. ffmpegReader->tempWrPtr = packetSize;
  834. osal_memcpy(ffmpegReader->tempBuffer, (Uint8*)(packet->data), packetSize);
  835. osal_memcpy(packet->data, seqHeader, seqHeaderSize);
  836. packetSize = seqHeaderSize;
  837. goto __end_read;
  838. }
  839. }
  840. else if (ffmpegReader->standard == STD_VP9) {
  841. Uint8* pData = (Uint8*)packet->data;
  842. Uint32 lastIndex = packet->size - 1;
  843. if (IS_VP9_SUPERFRAME(pData[lastIndex]) == TRUE) {
  844. VP9Superframe* superframe = &ffmpegReader->superframe;
  845. if (VP9ParseSuperframe(pData, packet->size, superframe) == TRUE) {
  846. osal_memcpy(packet->data, superframe->frames[0], superframe->frameSize[0]);
  847. packet->size = superframe->frameSize[0];
  848. packetSize = packet->size;
  849. superframe->currentIndex++;
  850. }
  851. }
  852. }
  853. __end_read:
  854. av_free_packet(&avpacket);
  855. if (picHeader)
  856. osal_free(picHeader);
  857. if (seqHeader)
  858. osal_free(seqHeader);
  859. return packetSize;
  860. __consume_tempBuffer:
  861. if (ffmpegReader->tempBuffer != NULL) {
  862. osal_memcpy(packet->data, ffmpegReader->tempBuffer, ffmpegReader->tempWrPtr);
  863. packetSize = ffmpegReader->tempWrPtr;
  864. osal_free(ffmpegReader->tempBuffer);
  865. ffmpegReader->tempBuffer = NULL;
  866. ffmpegReader->tempWrPtr = 0;
  867. ffmpegReader->tempRdPtr = 0;
  868. }
  869. return packetSize;
  870. }
  871. BOOL BSFeederFrameSize_Rewind(
  872. void* feeder
  873. )
  874. {
  875. ReaderContext* ffmpegReader = (ReaderContext*)feeder;
  876. AVFormatContext* avFormatContext = ffmpegReader->avContext;
  877. Int32 ret;
  878. if ((ret=av_seek_frame(avFormatContext, ffmpegReader->videoIndex, 0, 0)) < 0) {
  879. VLOG(ERR, "%s:%d Failed to av_seek_frame:(ret:%d)\n", __FUNCTION__, __LINE__, ret);
  880. return FALSE;
  881. }
  882. return TRUE;
  883. }
  884. #else
  885. void* BSFeederFrameSize_Create(
  886. const char* path,
  887. CodStd codecId,
  888. CodStd* retCodecId,
  889. Uint32* retMp4ClassId,
  890. Uint32* retSeqWidth,
  891. Uint32* retSeqHeight
  892. )
  893. {
  894. UNREFERENCED_PARAMETER(path);
  895. UNREFERENCED_PARAMETER(codecId);
  896. UNREFERENCED_PARAMETER(retCodecId);
  897. UNREFERENCED_PARAMETER(retMp4ClassId);
  898. UNREFERENCED_PARAMETER(retSeqWidth);
  899. UNREFERENCED_PARAMETER(retSeqHeight);
  900. VLOG(ERR, "PLEASE PORT THIS %s ON YOUR ANDROID SYSTEM\n", __FUNCTION__);
  901. return NULL;
  902. }
  903. BOOL BSFeederFrameSize_Destroy(
  904. void* feeder
  905. )
  906. {
  907. UNREFERENCED_PARAMETER(feeder);
  908. VLOG(ERR, "PLEASE PORT THIS %s ON YOUR ANDROID SYSTEM\n", __FUNCTION__);
  909. return FALSE;
  910. }
  911. Int32 BSFeederFrameSize_Act(
  912. void* feeder,
  913. BSChunk* packet
  914. )
  915. {
  916. UNREFERENCED_PARAMETER(feeder);
  917. UNREFERENCED_PARAMETER(packet);
  918. VLOG(ERR, "PLEASE PORT THIS %s ON YOUR ANDROID SYSTEM\n", __FUNCTION__);
  919. return -1;
  920. }
  921. #endif /* SUPPORT_FFMPEG_DEMUX */