PageRenderTime 66ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/amplayer/player/player_av.c

https://github.com/J1nx-Hackable-Gadgets/libamplayer-m3
C | 3147 lines | 2674 code | 252 blank | 221 comment | 1083 complexity | e96f6389f21f9136bd2a5eaffabb6113 MD5 | raw file
Possible License(s): LGPL-3.0, CC-BY-SA-3.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. /************************************************
  2. * name :av_decoder.c
  3. * function :decoder relative functions
  4. * data :2010.2.4
  5. *************************************************/
  6. //header file
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <player_error.h>
  12. #include "player_priv.h"
  13. #include "player_av.h"
  14. #include "player_hwdec.h"
  15. #include "player_set_sys.h"
  16. #include "h263vld.h"
  17. #include "thread_mgt.h"
  18. #include "player_update.h"
  19. #include <cutils/properties.h>
  20. #define DUMP_READ_RAW_DATA (1<<0)
  21. #define DUMP_WRITE_RAW_DATA (1<<1)
  22. #define DUMP_READ_ES_VIDEO (1<<2)
  23. #define DUMP_WRITE_ES_VIDEO (1<<3)
  24. #define DUMP_READ_ES_AUDIO (1<<4)
  25. #define DUMP_WRITE_ES_AUDIO (1<<5)
  26. #include <fcntl.h>
  27. int fdr_raw = -1, fdr_video = -1, fdr_audio = -1;
  28. int fdw_raw = -1, fdw_video = -1, fdw_audio = -1;
  29. es_sub_t es_sub_buf[9];
  30. char sub_buf[9][SUBTITLE_SIZE];
  31. int sub_stream;
  32. static const media_type media_array[] = {
  33. {"mpegts", MPEG_FILE, STREAM_TS},
  34. {"mpeg", MPEG_FILE, STREAM_PS},
  35. {"rm", RM_FILE, STREAM_RM},
  36. {"avi", AVI_FILE, STREAM_ES},
  37. {"mkv", MKV_FILE, STREAM_ES},
  38. {"matroska", MKV_FILE, STREAM_ES},
  39. {"mov", MOV_FILE, STREAM_ES},
  40. {"mp4", MP4_FILE, STREAM_ES},
  41. {"flv", FLV_FILE, STREAM_ES},
  42. {"aac", AAC_FILE, STREAM_AUDIO},
  43. {"ac3", AC3_FILE, STREAM_AUDIO},
  44. {"mp3", MP3_FILE, STREAM_AUDIO},
  45. {"mp2", MP3_FILE, STREAM_AUDIO},
  46. {"wav", WAV_FILE, STREAM_AUDIO},
  47. {"dts", DTS_FILE, STREAM_AUDIO},
  48. {"flac", FLAC_FILE, STREAM_AUDIO},
  49. {"h264", H264_FILE, STREAM_VIDEO},
  50. {"cavs", AVS_FILE, STREAM_VIDEO},
  51. {"mpegvideo", M2V_FILE, STREAM_VIDEO},
  52. {"p2p", P2P_FILE, STREAM_ES},
  53. {"asf", ASF_FILE, STREAM_ES},
  54. {"m4a", MP4_FILE, STREAM_ES},
  55. {"m4v", MP4_FILE, STREAM_ES},
  56. {"rtsp", STREAM_FILE, STREAM_ES},
  57. {"ape", APE_FILE, STREAM_ES},
  58. {"hls,applehttp", MP4_FILE, STREAM_ES},
  59. {"DRMdemux", MP4_FILE, STREAM_ES},
  60. {"cmf", MP4_FILE, STREAM_ES},
  61. {"amr", AMR_FILE, STREAM_AUDIO},
  62. };
  63. aformat_t audio_type_convert(enum CodecID id, pfile_type File_type)
  64. {
  65. aformat_t format = -1;
  66. switch (id) {
  67. case CODEC_ID_PCM_MULAW:
  68. //format = AFORMAT_MULAW;
  69. format = AFORMAT_ADPCM;
  70. break;
  71. case CODEC_ID_PCM_ALAW:
  72. //format = AFORMAT_ALAW;
  73. format = AFORMAT_ADPCM;
  74. break;
  75. case CODEC_ID_MP1:
  76. case CODEC_ID_MP2:
  77. case CODEC_ID_MP3:
  78. format = AFORMAT_MPEG;
  79. break;
  80. case CODEC_ID_AAC_LATM:
  81. format = AFORMAT_AAC_LATM;
  82. break;
  83. case CODEC_ID_AAC:
  84. if (File_type == RM_FILE) {
  85. format = AFORMAT_RAAC;
  86. } else {
  87. format = AFORMAT_AAC;
  88. }
  89. break;
  90. case CODEC_ID_AC3:
  91. format = AFORMAT_AC3;
  92. break;
  93. case CODEC_ID_EAC3:
  94. format = AFORMAT_EAC3;
  95. break;
  96. case CODEC_ID_DTS:
  97. format = AFORMAT_DTS;
  98. break;
  99. case CODEC_ID_PCM_S16BE:
  100. format = AFORMAT_PCM_S16BE;
  101. break;
  102. case CODEC_ID_PCM_S16LE:
  103. format = AFORMAT_PCM_S16LE;
  104. break;
  105. case CODEC_ID_PCM_U8:
  106. format = AFORMAT_PCM_U8;
  107. break;
  108. case CODEC_ID_COOK:
  109. format = AFORMAT_COOK;
  110. break;
  111. case CODEC_ID_ADPCM_IMA_WAV:
  112. case CODEC_ID_ADPCM_MS:
  113. format = AFORMAT_ADPCM;
  114. break;
  115. case CODEC_ID_AMR_NB:
  116. case CODEC_ID_AMR_WB:
  117. format = AFORMAT_AMR;
  118. break;
  119. case CODEC_ID_WMAV1:
  120. case CODEC_ID_WMAV2:
  121. format = AFORMAT_WMA;
  122. break;
  123. case CODEC_ID_FLAC:
  124. format = AFORMAT_FLAC;
  125. break;
  126. case CODEC_ID_WMAPRO:
  127. format = AFORMAT_WMAPRO;
  128. break;
  129. case CODEC_ID_PCM_BLURAY:
  130. format = AFORMAT_PCM_BLURAY;
  131. break;
  132. case CODEC_ID_ALAC:
  133. format = AFORMAT_ALAC;
  134. break;
  135. case CODEC_ID_VORBIS:
  136. format = AFORMAT_VORBIS;
  137. break;
  138. case CODEC_ID_APE:
  139. format = AFORMAT_APE;
  140. break;
  141. case CODEC_ID_PCM_WIFIDISPLAY:
  142. format = AFORMAT_PCM_WIFIDISPLAY;
  143. break;
  144. default:
  145. format = AFORMAT_UNSUPPORT;
  146. log_print("audio codec_id=0x%x\n", id);
  147. }
  148. log_print("[audio_type_convert]audio codec_id=0x%x format=%d\n", id, format);
  149. return format;
  150. }
  151. vformat_t video_type_convert(enum CodecID id)
  152. {
  153. vformat_t format;
  154. switch (id) {
  155. case CODEC_ID_MPEG1VIDEO:
  156. case CODEC_ID_MPEG2VIDEO:
  157. case CODEC_ID_MPEG2VIDEO_XVMC:
  158. format = VFORMAT_MPEG12;
  159. break;
  160. case CODEC_ID_H263:
  161. case CODEC_ID_MPEG4:
  162. case CODEC_ID_H263P:
  163. case CODEC_ID_H263I:
  164. case CODEC_ID_XVID:
  165. case CODEC_ID_MSMPEG4V2:
  166. case CODEC_ID_MSMPEG4V3:
  167. case CODEC_ID_FLV1:
  168. format = VFORMAT_MPEG4;
  169. break;
  170. case CODEC_ID_RV10:
  171. case CODEC_ID_RV20:
  172. case CODEC_ID_RV30:
  173. case CODEC_ID_RV40:
  174. format = VFORMAT_REAL;
  175. break;
  176. //case CODEC_ID_AVC1:
  177. case CODEC_ID_H264:
  178. format = VFORMAT_H264;
  179. break;
  180. case CODEC_ID_H264MVC:
  181. format = VFORMAT_H264MVC;
  182. break;
  183. case CODEC_ID_MJPEG:
  184. format = VFORMAT_MJPEG;
  185. break;
  186. case CODEC_ID_VC1:
  187. case CODEC_ID_WMV3:
  188. //case CODEC_ID_WMV1: //not support
  189. // case CODEC_ID_WMV2: //not support
  190. format = VFORMAT_VC1;
  191. break;
  192. case CODEC_ID_VP6F:
  193. format = VFORMAT_SW;
  194. break;
  195. case CODEC_ID_CAVS:
  196. case CODEC_ID_AVS:
  197. format = VFORMAT_AVS;
  198. break;
  199. default:
  200. format = VFORMAT_UNSUPPORT;
  201. log_print("video_type_convert failed:unsupport video,codec_id=0x%x\n", id);
  202. }
  203. log_print("[video_type_convert]video codec_id=0x%x format=%d\n", id, format);
  204. return format;
  205. }
  206. vdec_type_t video_codec_type_convert(unsigned int id)
  207. {
  208. vdec_type_t dec_type;
  209. log_print("[video_codec_type_convert]id=(0x%x) ", id);
  210. switch (id) {
  211. case CODEC_TAG_MJPEG:
  212. case CODEC_TAG_mjpeg:
  213. case CODEC_TAG_jpeg:
  214. case CODEC_TAG_mjpa:
  215. log_print("VIDEO_TYPE_MJPEG\n");
  216. dec_type = VIDEO_DEC_FORMAT_MJPEG;
  217. break;
  218. // xvid
  219. case CODEC_TAG_XVID:
  220. case CODEC_TAG_xvid:
  221. case CODEC_TAG_XVIX:
  222. log_print("VIDEO_TYPE_XVID\n");
  223. dec_type = VIDEO_DEC_FORMAT_MPEG4_5;
  224. break;
  225. //divx3.11
  226. case CODEC_TAG_COL1:
  227. case CODEC_TAG_DIV3:
  228. case CODEC_TAG_MP43:
  229. log_print("VIDEO_TYPE_DIVX311\n");
  230. dec_type = VIDEO_DEC_FORMAT_MPEG4_3;
  231. break;
  232. // divx4
  233. case CODEC_TAG_DIV4:
  234. case CODEC_TAG_DIVX:
  235. case CODEC_TAG_divx:
  236. log_print("VIDEO_TYPE_DIVX4\n");
  237. dec_type = VIDEO_DEC_FORMAT_MPEG4_4;
  238. break;
  239. // divx5
  240. case CODEC_TAG_DIV5:
  241. case CODEC_TAG_DX50:
  242. case CODEC_TAG_M4S2:
  243. case CODEC_TAG_FMP4:
  244. case CODEC_TAG_FVFW:
  245. log_print("VIDEO_TYPE_DIVX 5\n");
  246. dec_type = VIDEO_DEC_FORMAT_MPEG4_5;
  247. break;
  248. // divx6
  249. case CODEC_TAG_DIV6:
  250. log_print("VIDEO_TYPE_DIVX 6\n");
  251. dec_type = VIDEO_DEC_FORMAT_MPEG4_5;
  252. break;
  253. // mp4
  254. case CODEC_TAG_MP4V:
  255. case CODEC_TAG_RMP4:
  256. case CODEC_TAG_MPG4:
  257. case CODEC_TAG_mp4v:
  258. case CODEC_ID_MPEG4:
  259. log_print("VIDEO_DEC_FORMAT_MPEG4_5\n");
  260. dec_type = VIDEO_DEC_FORMAT_MPEG4_5;
  261. break;
  262. // h263
  263. case CODEC_ID_H263:
  264. case CODEC_TAG_H263:
  265. case CODEC_TAG_h263:
  266. case CODEC_TAG_s263:
  267. case CODEC_TAG_F263:
  268. log_print("VIDEO_DEC_FORMAT_H263\n");
  269. dec_type = VIDEO_DEC_FORMAT_H263;
  270. break;
  271. //avc1
  272. case CODEC_TAG_AVC1:
  273. case CODEC_TAG_avc1:
  274. // h264
  275. case CODEC_TAG_H264:
  276. case CODEC_TAG_h264:
  277. log_print("VIDEO_TYPE_H264\n");
  278. dec_type = VIDEO_DEC_FORMAT_H264;
  279. break;
  280. case CODEC_ID_RV30:
  281. log_print("[video_codec_type_convert]VIDEO_DEC_FORMAT_REAL_8(0x%x)\n", id);
  282. dec_type = VIDEO_DEC_FORMAT_REAL_8;
  283. break;
  284. case CODEC_ID_RV40:
  285. log_print("[video_codec_type_convert]VIDEO_DEC_FORMAT_REAL_9(0x%x)\n", id);
  286. dec_type = VIDEO_DEC_FORMAT_REAL_9;
  287. break;
  288. case CODEC_ID_H264:
  289. log_print("[video_codec_type_convert]VIDEO_DEC_FORMAT_H264(0x%x)\n", id);
  290. dec_type = VIDEO_DEC_FORMAT_H264;
  291. break;
  292. case CODEC_ID_H264MVC:
  293. log_print("[video_codec_type_convert]VIDEO_DEC_FORMAT_H264MVC(0x%x)\n", id);
  294. dec_type = VIDEO_DEC_FORMAT_H264;
  295. break;
  296. //case CODEC_TAG_WMV1: //not support
  297. //case CODEC_TAG_WMV2: //not support
  298. case CODEC_TAG_WMV3:
  299. log_print("[video_codec_type_convert]VIDEO_DEC_FORMAT_WMV3(0x%x)\n", id);
  300. dec_type = VIDEO_DEC_FORMAT_WMV3;
  301. break;
  302. case CODEC_TAG_WVC1:
  303. case CODEC_ID_VC1:
  304. case CODEC_TAG_WMVA:
  305. log_print("[video_codec_type_convert]VIDEO_DEC_FORMAT_WVC1(0x%x)\n", id);
  306. dec_type = VIDEO_DEC_FORMAT_WVC1;
  307. break;
  308. case CODEC_ID_VP6F:
  309. log_print("[video_codec_type_convert]VIDEO_DEC_FORMAT_SW(0x%x)\n", id);
  310. dec_type = VIDEO_DEC_FORMAT_SW;
  311. break;
  312. case CODEC_ID_CAVS:
  313. case CODEC_ID_AVS:
  314. log_print("[video_codec_type_convert]VIDEO_DEC_FORMAT_AVS(0x%x)\n", id);
  315. dec_type = VIDEO_DEC_FORMAT_AVS;
  316. break;
  317. default:
  318. log_print("[video_codec_type_convert]error:VIDEO_TYPE_UNKNOW id = 0x%x\n", id);
  319. dec_type = VIDEO_DEC_FORMAT_UNKNOW;
  320. break;
  321. }
  322. return dec_type;
  323. }
  324. stream_type_t stream_type_convert(pstream_type type, char vflag, char aflag)
  325. {
  326. switch (type) {
  327. case STREAM_TS:
  328. return STREAM_TYPE_TS;
  329. case STREAM_PS:
  330. return STREAM_TYPE_PS;
  331. case STREAM_RM:
  332. return STREAM_TYPE_RM;
  333. case STREAM_ES:
  334. if (vflag == 1) {
  335. return STREAM_TYPE_ES_VIDEO;
  336. }
  337. if (aflag == 1) {
  338. return STREAM_TYPE_ES_AUDIO;
  339. } else {
  340. return STREAM_TYPE_UNKNOW;
  341. }
  342. case STREAM_AUDIO:
  343. return STREAM_TYPE_ES_AUDIO;
  344. case STREAM_VIDEO:
  345. return STREAM_TYPE_ES_VIDEO;
  346. case STREAM_UNKNOWN:
  347. default:
  348. return STREAM_TYPE_UNKNOW;
  349. }
  350. }
  351. int set_file_type(const char *name, pfile_type *ftype, pstream_type *stype)
  352. {
  353. int i, j ;
  354. j = sizeof(media_array) / sizeof(media_type);
  355. for (i = 0; i < j; i++) {
  356. //log_print("[set_file_type:0]name = %s mname=%s\n",name ,media_array[i].file_ext);
  357. if (strcmp(name, media_array[i].file_ext) == 0) {
  358. break;
  359. }
  360. }
  361. if (i == j) {
  362. for (i = 0; i < j; i++) {
  363. //log_print("[set_file_type:1]name = %s mname=%s\n",name ,media_array[i].file_ext);
  364. if (strstr(name, media_array[i].file_ext) != NULL) {
  365. break;
  366. }
  367. if (i == j) {
  368. log_print("Unsupport file type %s\n", name);
  369. return PLAYER_UNSUPPORT;
  370. }
  371. }
  372. }
  373. *ftype = media_array[i].file_type;
  374. *stype = media_array[i].stream_type;
  375. log_info("[set_file_type]file_type=%d stream_type=%d\n", *ftype, *stype);
  376. return PLAYER_SUCCESS;
  377. }
  378. static int compare_pkt(AVPacket *src, AVPacket *dst)
  379. {
  380. if (dst->pts != (int64_t)AV_NOPTS_VALUE) {
  381. if (dst->pts <= src->pts) {
  382. return 1;
  383. } else {
  384. return 0;
  385. }
  386. } else if (dst->dts != (int64_t)AV_NOPTS_VALUE) {
  387. if (dst->dts <= src->dts) {
  388. return 1;
  389. } else {
  390. return 0;
  391. }
  392. } else {
  393. int compare_size = MIN(src->size, dst->size);
  394. compare_size = compare_size > 1024 ? 1024 : compare_size;
  395. //log_print("dst size %d, src size %d, dst data 0x%x, src data 0x%x\n",
  396. // dst->size, src->size, dst->data, src->data);
  397. if (memcmp(dst->data, src->data, compare_size) == 0) {
  398. return 1;
  399. } else {
  400. //log_print("Packet is different\n");
  401. return 0;
  402. }
  403. }
  404. }
  405. static int backup_packet(play_para_t *para, AVPacket *src, AVPacket *dst)
  406. {
  407. if (dst->data != NULL) {
  408. if (dst->pos >= url_ftell(para->pFormatCtx->pb)) {
  409. log_print("[%s:%d]dst->pos >= url_ftell(pb)\n", __FUNCTION__, __LINE__);
  410. return 0;
  411. } else {
  412. FREE(dst->data);
  413. }
  414. }
  415. dst->data = MALLOC(src->size);
  416. if (dst->data == NULL) {
  417. log_error("[%s:%d]No memory!\n", __FUNCTION__, __LINE__);
  418. return -1;
  419. }
  420. dst->pts = src->pts;
  421. dst->dts = src->dts;
  422. dst->size = src->size;
  423. dst->pos = url_ftell(para->pFormatCtx->pb);
  424. MEMCPY(dst->data, src->data, src->size);
  425. return 0;
  426. }
  427. static int raw_read(play_para_t *para)
  428. {
  429. int rev_byte = -1;
  430. ByteIOContext *pb = para->pFormatCtx->pb;
  431. am_packet_t *pkt = para->p_pkt;
  432. unsigned char *pbuf ;
  433. static int try_count = 0;
  434. int64_t cur_offset = 0;
  435. float value;
  436. int dump_data_mode = 0;
  437. char dump_path[128];
  438. #ifdef ANDROID
  439. if (am_getconfig_float("media.libplayer.dumpmode", &value) == 0) {
  440. dump_data_mode = (int)value;
  441. }
  442. #endif
  443. if (dump_data_mode == DUMP_READ_RAW_DATA) {
  444. if (fdr_raw == -1) {
  445. sprintf(dump_path, "/temp/pid%d_dump_read.dat", para->player_id);
  446. fdr_raw = open(dump_path, O_CREAT | O_RDWR, 0666);
  447. if (fdr_raw < 0) {
  448. log_print("creat %s failed!fd=%d\n", dump_path, fdr_raw);
  449. }
  450. }
  451. }
  452. if (pkt->data_size > 0) {
  453. if (!para->enable_rw_on_pause) {
  454. player_thread_wait(para, RW_WAIT_TIME);
  455. }
  456. return PLAYER_SUCCESS;
  457. }
  458. if(para->buffering_enable&&para->buffering_threshhold_max){
  459. int maxlimitsize=0;
  460. maxlimitsize=((int)((1-para->buffering_threshhold_max)*AB_SIZE-1))&~0x2000;
  461. if (para->max_raw_size > maxlimitsize) {
  462. para->max_raw_size =maxlimitsize;
  463. log_print("Enable autobuf , max_raw_size set to =%d\n",para->max_raw_size);
  464. }
  465. }
  466. if (pkt->buf == NULL || pkt->buf_size != (MAX_RAW_DATA_SIZE+16)) { /*may chaged to short,enarge it*/
  467. pkt->buf_size = MAX_RAW_DATA_SIZE+16;
  468. pkt->buf = MALLOC(pkt->buf_size);
  469. if (pkt->buf == NULL) {
  470. log_print("not enough memory,please fre memory\n");
  471. return PLAYER_RD_EMPTYP;
  472. }
  473. }
  474. pkt->data = pkt->buf;
  475. pbuf = pkt->data;
  476. cur_offset = url_ftell(pb);
  477. #ifdef DEBUG_VARIABLE_DUR
  478. if (para->playctrl_info.info_variable) {
  479. if ((cur_offset + para->max_raw_size) >= para->pFormatCtx->valid_offset) {
  480. update_variable_info(para);
  481. }
  482. }
  483. #endif
  484. if (!para->playctrl_info.read_end_flag && (0 == pkt->data_size)) {
  485. int tryread_size;
  486. if (para->playctrl_info.lowbuffermode_flag) {
  487. tryread_size = 1024; /*keep in low buffer level for less latency ....*/
  488. } else {
  489. tryread_size = para->max_raw_size;
  490. }
  491. rev_byte = get_buffer(pb, pbuf, tryread_size);
  492. log_debug1("get_buffer,%d,cur_offset=%lld,para->pFormatCtx->valid_offset==%lld\n", rev_byte , cur_offset, para->pFormatCtx->valid_offset);
  493. if (AVERROR(ETIMEDOUT) == rev_byte && para->state.current_time >= para->state.full_time) {
  494. //read timeout ,if playing current time reached end time,we think it is eof
  495. rev_byte = AVERROR_EOF;
  496. }
  497. if ((rev_byte > 0) && (cur_offset <= para->pFormatCtx->valid_offset)) {
  498. try_count = 0;
  499. pkt->data_size = rev_byte;
  500. para->read_size.total_bytes += rev_byte;
  501. pkt->avpkt_newflag = 1;
  502. pkt->avpkt_isvalid = 1;
  503. pkt->pts_checkin_ok = 0;
  504. if (fdr_raw > 0) {
  505. int dsize;
  506. dsize = write(fdr_raw, pkt->data, pkt->data_size);
  507. if (dsize != pkt->data_size) {
  508. log_print("dump data write failed!size=%d len=%d\n", dsize, pkt->data_size);
  509. }
  510. //log_print("dump data write succeed!size=%d len=%d\n",dsize,pkt->data_size);
  511. }
  512. } else if ((rev_byte == AVERROR_EOF) || (cur_offset > para->pFormatCtx->valid_offset)) { //if(rev_byte != AVERROR(EAGAIN))
  513. static int reach_end = 0;
  514. if (para->stream_type == STREAM_AUDIO && para->astream_info.audio_format == AFORMAT_MPEG)
  515. //if(0)
  516. {
  517. if (reach_end < 5) {
  518. reach_end++;
  519. //if(!get_readend_set_flag())
  520. //set_readend_flag(1);
  521. memset(pbuf, 0, tryread_size);
  522. // strncpy(pbuf,"FREND",5);
  523. pkt->data_size = tryread_size;
  524. pkt->avpkt_newflag = 0;
  525. pkt->avpkt_isvalid = 1;
  526. pkt->pts_checkin_ok = 0;
  527. } else {
  528. reach_end = 0;
  529. para->playctrl_info.read_end_flag = 1;
  530. log_print("raw read2: read end!,%d,%lld,%lld add :%d byte zero data\n", rev_byte , cur_offset, para->pFormatCtx->valid_offset, tryread_size * 5);
  531. }
  532. } else {
  533. reach_end = 0;
  534. /*if the return is EAGAIN,we need to try more times*/
  535. para->playctrl_info.read_end_flag = 1;
  536. log_print("raw read: read end!,%d,%lld,%lld\n", rev_byte , cur_offset, para->pFormatCtx->valid_offset);
  537. if (fdr_raw > 0) {
  538. close(fdr_raw);
  539. }
  540. }
  541. #if 0 //old
  542. /*if the return is EAGAIN,we need to try more times*/
  543. para->playctrl_info.read_end_flag = 1;
  544. log_print("raw read: read end!,%d,%lld,%lld\n", rev_byte , cur_offset, para->pFormatCtx->valid_offset);
  545. #if DUMP_READ_AVDATA
  546. if (fdr > 0) {
  547. close(fdr);
  548. }
  549. #endif
  550. #endif
  551. } else {
  552. if (rev_byte != AVERROR(EAGAIN)) {
  553. log_print("raw_read buffer error!,%d\n", rev_byte);
  554. return PLAYER_RD_FAILED;
  555. } else {
  556. try_count ++;
  557. if (try_count >= para->playctrl_info.read_max_retry_cnt) {
  558. log_print("raw_read buffer try too more counts,exit!\n");
  559. return PLAYER_RD_TIMEOUT;
  560. } else {
  561. return PLAYER_RD_AGAIN;
  562. }
  563. }
  564. }
  565. }
  566. if (para->stream_type == STREAM_TS) {
  567. pkt->codec = para->codec;
  568. pkt->type = CODEC_COMPLEX;
  569. } else if (para->stream_type == STREAM_PS) {
  570. pkt->codec = para->codec;
  571. pkt->type = CODEC_COMPLEX;
  572. } else if (para->stream_type == STREAM_RM) {
  573. pkt->codec = para->codec;
  574. pkt->type = CODEC_COMPLEX;
  575. } else if (para->stream_type == STREAM_AUDIO) {
  576. pkt->codec = para->acodec;
  577. pkt->type = CODEC_AUDIO;
  578. } else if (para->stream_type == STREAM_VIDEO) {
  579. pkt->codec = para->vcodec;
  580. pkt->type = CODEC_VIDEO;
  581. }
  582. return PLAYER_SUCCESS;
  583. }
  584. static int non_raw_read(play_para_t *para)
  585. {
  586. static int try_count = 0;
  587. am_packet_t *pkt = para->p_pkt;
  588. signed short video_idx = para->vstream_info.video_index;
  589. signed short audio_idx = para->astream_info.audio_index;
  590. signed short sub_idx = para->sstream_info.sub_index;
  591. int has_video = para->vstream_info.has_video;
  592. int has_audio = para->astream_info.has_audio;
  593. int has_sub = para->sstream_info.has_sub;
  594. float value;
  595. int dump_data_mode = 0;
  596. char dump_path[128];
  597. #ifdef ANDROID
  598. if (am_getconfig_float("media.libplayer.dumpmode", &value) == 0) {
  599. dump_data_mode = (int)value;
  600. }
  601. #endif
  602. if (pkt->data_size > 0) {
  603. if (!para->enable_rw_on_pause) {
  604. player_thread_wait(para, RW_WAIT_TIME);
  605. }
  606. //log_print("[%s:%d]wait---data_size=%d!\n",__FUNCTION__, __LINE__,pkt->data_size);
  607. return PLAYER_SUCCESS;
  608. }
  609. if (para->vstream_info.has_video && !para->vcodec) {
  610. log_print("[non_raw_read]video codec invalid!\n");
  611. return PLAYER_RD_EMPTYP;
  612. }
  613. if (para->astream_info.has_audio && !para->acodec) {
  614. log_print("[non_raw_read]audio codec invalid!\n");
  615. return PLAYER_RD_EMPTYP;
  616. }
  617. if (pkt->avpkt == NULL) {
  618. log_print("non_raw_read error:avpkt pointer NULL!\n");
  619. return PLAYER_RD_EMPTYP;
  620. }
  621. while (!para->playctrl_info.read_end_flag && (0 == pkt->data_size)) {
  622. int ret;
  623. ret = av_read_frame(para->pFormatCtx, pkt->avpkt);
  624. if (ret < 0) {
  625. if (AVERROR(EAGAIN) != ret) {
  626. /*if the return is EAGAIN,we need to try more times*/
  627. log_error("[%s:%d]av_read_frame return (%d)\n", __FUNCTION__, __LINE__, ret);
  628. if (AVERROR(ETIMEDOUT) == ret && para->state.current_time >= para->state.full_time) {
  629. //read timeout ,if playing current time reached end time,we think it is eof
  630. ret = AVERROR_EOF;
  631. }
  632. if (AVERROR_EOF != ret) {
  633. return PLAYER_RD_FAILED;
  634. } else {
  635. /*reach end add 6k audio data*/
  636. static int reach_end = 0;
  637. AVStream *st;
  638. if (reach_end < 3) {
  639. reach_end++;
  640. if (audio_idx >= 0) {
  641. st = para->pFormatCtx->streams[audio_idx];
  642. if (st->codec->codec_type == CODEC_TYPE_AUDIO && (st->codec->codec_id == CODEC_ID_APE || st->codec->codec_id == CODEC_ID_AAC || st->codec->codec_id == CODEC_ID_AMR_NB || st->codec->codec_id == CODEC_ID_MP3)) {
  643. //if (st->codec->codec_type==CODEC_TYPE_AUDIO) {
  644. //set attr
  645. if (!get_readend_set_flag()) {
  646. set_readend_flag(1);
  647. }
  648. pkt->avpkt->data = av_mallocz(2048);
  649. //strncpy(pkt->avpkt->data,"FREND",5);
  650. pkt->avpkt->size = 2048;
  651. pkt->avpkt->stream_index = st->index;
  652. ret = 0;
  653. }
  654. }
  655. } else {//audio data add end
  656. reach_end = 0;
  657. para->playctrl_info.read_end_flag = 1;
  658. log_print("non_raw_read: read end!\n");
  659. if (fdr_video >= 0) {
  660. close(fdr_video);
  661. }
  662. if (fdr_audio >= 0) {
  663. close(fdr_audio);
  664. }
  665. }
  666. }
  667. } else {
  668. try_count ++;
  669. if (try_count >= para->playctrl_info.read_max_retry_cnt) {
  670. log_print("try %d counts, can't get packet,exit!\n", para->playctrl_info.read_max_retry_cnt);
  671. return PLAYER_RD_TIMEOUT;
  672. } else {
  673. log_print("[non_raw_read]EAGAIN, try count=%d\n", try_count);
  674. return PLAYER_RD_AGAIN;
  675. }
  676. }
  677. } else { //read success
  678. try_count = 0;
  679. if (dump_data_mode == DUMP_READ_ES_VIDEO) {
  680. if (fdr_video == -1) {
  681. sprintf(dump_path, "/temp/pid%d_dump_vread.dat", para->player_id);
  682. fdr_video = open(dump_path, O_CREAT | O_RDWR, 0666);
  683. if (fdr_video < 0) {
  684. log_print("creat %s failed!fd=%d\n", dump_path, fdr_video);
  685. }
  686. }
  687. } else if (dump_data_mode == DUMP_READ_ES_AUDIO) {
  688. if (fdr_audio == -1) {
  689. sprintf(dump_path, "/temp/pid%d_dump_aread.dat", para->player_id);
  690. fdr_audio = open(dump_path, O_CREAT | O_RDWR, 0666);
  691. if (fdr_audio < 0) {
  692. log_error("creat %s failed!fd=%d\n", dump_path, fdr_audio);
  693. }
  694. }
  695. }
  696. }
  697. //log_print("av_read_frame return (%d) pkt->avpkt=%p pkt->avpkt->data=%p\r",ret,pkt->avpkt,pkt->avpkt->data);
  698. if (pkt->avpkt->size >= MAX_PACKET_SIZE) {
  699. log_print("non_raw_read error:packet size exceed malloc memory! size %d\n", pkt->avpkt->size);
  700. av_free_packet(pkt->avpkt);
  701. return PLAYER_RD_FAILED;
  702. }
  703. if (para->stream_type == STREAM_ES && !para->playctrl_info.read_end_flag) {
  704. if (has_video && video_idx == pkt->avpkt->stream_index) {
  705. if (para->playctrl_info.audio_switch_vmatch) {
  706. if (compare_pkt(pkt->avpkt, &pkt->bak_avpkt) == 0) {
  707. av_free_packet(pkt->avpkt);
  708. continue;
  709. } else {
  710. //FREE(pkt->bak_avpkt.data);
  711. pkt->bak_avpkt.pos = 0;
  712. para->playctrl_info.audio_switch_vmatch = 0;
  713. }
  714. } else if (para->vstream_info.discard_pkt == 1) {
  715. av_free_packet(pkt->avpkt);
  716. para->vstream_info.discard_pkt = 2;
  717. continue;
  718. } else if (para->vstream_info.discard_pkt == 2) {
  719. para->vstream_info.discard_pkt = 1;
  720. }
  721. pkt->codec = para->vcodec;
  722. pkt->type = CODEC_VIDEO;
  723. if (ret != AVERROR_EOF) {
  724. para->read_size.vpkt_num ++;
  725. }
  726. } else if (has_audio && audio_idx == pkt->avpkt->stream_index) {
  727. pkt->codec = para->acodec;
  728. pkt->type = CODEC_AUDIO;
  729. para->read_size.apkt_num ++;
  730. //} else if (has_sub && ((1<<(pkt->avpkt->stream_index))&sub_stream)/*&& sub_idx == pkt->avpkt->stream_index*/) {
  731. } else if (has_sub && ((1<<(para->pFormatCtx->streams[pkt->avpkt->stream_index]->id))&sub_stream)/*&& sub_idx == pkt->avpkt->stream_index*/) {
  732. #if 0
  733. /* here we get the subtitle data, something should to be done */
  734. if (para->playctrl_info.audio_switch_smatch) {
  735. if (compare_pkt(pkt->avpkt, &pkt->bak_spkt) == 0) {
  736. pkt->codec = NULL;
  737. pkt->type = CODEC_UNKNOW;
  738. av_free_packet(pkt->avpkt);
  739. continue;
  740. } else {
  741. //FREE(pkt->bak_avpkt.data);
  742. pkt->bak_avpkt.pos = 0;
  743. para->playctrl_info.audio_switch_smatch = 0;
  744. }
  745. }
  746. #endif
  747. para->read_size.spkt_num ++;
  748. pkt->type = CODEC_SUBTITLE;
  749. pkt->codec = para->scodec;
  750. } else {
  751. pkt->codec = NULL;
  752. pkt->type = CODEC_UNKNOW;
  753. av_free_packet(pkt->avpkt);
  754. continue;
  755. }
  756. if (para->first_index == -1) {
  757. para->first_index = pkt->avpkt->stream_index;
  758. }
  759. }
  760. if (ret == 0) {
  761. //discard the first package when resume from pause state for ape
  762. if (para->astream_info.audio_format == AFORMAT_APE && pkt->type == CODEC_AUDIO) {
  763. if (para->state.current_time > 0 && pkt->avpkt->pts == 0) {
  764. av_free_packet(pkt->avpkt);
  765. continue;
  766. }
  767. }
  768. pkt->data = pkt->avpkt->data;
  769. pkt->data_size = pkt->avpkt->size;
  770. int dsize;
  771. if (fdr_video >= 0 && pkt->type == CODEC_VIDEO) {
  772. dsize = write(fdr_video, pkt->data, pkt->data_size);
  773. } else if (fdr_audio >= 0 && pkt->type == CODEC_AUDIO) {
  774. dsize = write(fdr_audio, pkt->data, pkt->data_size);
  775. }
  776. if ((fdr_audio >= 0 || fdr_video >= 0) && (dsize != pkt->data_size)) {
  777. log_print("dump read es data failed!type=%d size=%d len=%d\n",
  778. pkt->type, dsize, pkt->data_size);
  779. }
  780. //log_print("[%s:%d]dump data read size=%d, want len=%d\n", __FUNCTION__, __LINE__, dsize, pkt->data_size);
  781. pkt->avpkt_newflag = 1;
  782. pkt->avpkt_isvalid = 1;
  783. pkt->pts_checkin_ok = 0;
  784. //log_print("[%s:%d]read finish-data_size=%d!\r",__FUNCTION__, __LINE__,pkt->data_size);
  785. }
  786. break;
  787. }
  788. return PLAYER_SUCCESS;
  789. }
  790. int read_av_packet(play_para_t *para)
  791. {
  792. am_packet_t *pkt = para->p_pkt;
  793. char raw_mode = para->playctrl_info.raw_mode;
  794. int ret = PLAYER_SUCCESS;
  795. if (para == NULL || pkt == NULL) {
  796. return PLAYER_RD_EMPTYP;
  797. }
  798. if (raw_mode == 1) {
  799. player_mate_wake(para, 100 * 1000);
  800. ret = raw_read(para);
  801. if(ret <0 && para->playctrl_info.ignore_ffmpeg_errors){
  802. para->playctrl_info.ignore_ffmpeg_errors=0;
  803. if(para->pFormatCtx&& para->pFormatCtx->pb)
  804. para->pFormatCtx->pb->error=0;
  805. ret=0;
  806. }
  807. player_mate_sleep(para);
  808. if (ret != PLAYER_SUCCESS && ret != PLAYER_RD_AGAIN) {
  809. log_print("raw read failed!\n");
  810. return ret;
  811. }
  812. } else if (raw_mode == 0) {
  813. player_mate_wake(para, 100 * 1000);
  814. ret = non_raw_read(para);
  815. if(ret <0 && para->playctrl_info.ignore_ffmpeg_errors){
  816. para->playctrl_info.ignore_ffmpeg_errors=0;
  817. if(para->pFormatCtx&& para->pFormatCtx->pb)
  818. para->pFormatCtx->pb->error=0;
  819. ret=0;
  820. }
  821. player_mate_sleep(para);
  822. if (ret != PLAYER_SUCCESS && ret != PLAYER_RD_AGAIN) {
  823. log_print("non raw read failed!\n");
  824. return ret;
  825. }
  826. }
  827. return ret;
  828. }
  829. static int write_header(play_para_t *para)
  830. {
  831. int write_bytes = 0, len = 0;
  832. am_packet_t *pkt = para->p_pkt;
  833. if (para->pFormatCtx->skip_extradata) {
  834. //log_print("skip header!\n");
  835. return PLAYER_EMPTY_P;
  836. }
  837. if (pkt->hdr && pkt->hdr->size > 0) {
  838. if ((NULL == pkt->codec) || (NULL == pkt->hdr->data)) {
  839. log_error("[write_header]codec null!\n");
  840. return PLAYER_EMPTY_P;
  841. }
  842. //some wvc1 es data not need to add header
  843. if (pkt->type == CODEC_VIDEO && para->vstream_info.video_format == VFORMAT_VC1 && para->vstream_info.video_codec_type == VIDEO_DEC_FORMAT_WVC1) {
  844. if ((pkt->data) && (pkt->data_size >= 4) && (pkt->data[0] == 0) && (pkt->data[1] == 0) && (pkt->data[2] == 1) && (pkt->data[3] == 0xd || pkt->data[3] == 0xf)) {
  845. return PLAYER_SUCCESS;
  846. }
  847. }
  848. while (1) {
  849. write_bytes = codec_write(pkt->codec, pkt->hdr->data + len, pkt->hdr->size - len);
  850. if (write_bytes < 0 || write_bytes > (pkt->hdr->size - len)) {
  851. if (-errno != AVERROR(EAGAIN)) {
  852. log_print("ERROR:write header failed!\n");
  853. return PLAYER_WR_FAILED;
  854. } else {
  855. log_print("[write_header]need write again\n");
  856. //continue;
  857. return PLAYER_WR_AGAIN;
  858. }
  859. } else {
  860. int size;
  861. if (fdw_video >= 0 && pkt->type == CODEC_VIDEO) {
  862. size = write(fdw_video, pkt->hdr->data + len, write_bytes);
  863. } else if (fdw_audio >= 0 && pkt->type == CODEC_AUDIO) {
  864. size = write(fdw_audio, pkt->hdr->data + len, write_bytes);
  865. } else if (fdw_raw >= 0 && pkt->type == CODEC_COMPLEX) {
  866. size = write(fdw_raw, pkt->hdr->data + len, write_bytes);
  867. }
  868. if ((fdw_video >= 0 || fdw_audio >= 0 || fdw_raw >= 0) &&
  869. (size != write_bytes)) {
  870. log_print("dump data write failed!size=%d bytes=%d\n", size, write_bytes);
  871. }
  872. // log_print("[%s:%d]dump data write size=%d, want len=%d\n", __FUNCTION__, __LINE__, size, len);
  873. }
  874. len += write_bytes;
  875. if (len == pkt->hdr->size) {
  876. break;
  877. }
  878. }
  879. }
  880. return PLAYER_SUCCESS;
  881. }
  882. static int check_write_finish(play_para_t *para)
  883. {
  884. am_packet_t *pkt = para->p_pkt;
  885. if (para->playctrl_info.read_end_flag) {
  886. if (para->playctrl_info.raw_mode
  887. && (para->write_size.total_bytes == para->read_size.total_bytes)) {
  888. return PLAYER_WR_FINISH;
  889. }
  890. if (!para->playctrl_info.raw_mode
  891. && (para->write_size.vpkt_num == para->read_size.vpkt_num)
  892. && (para->write_size.apkt_num == para->read_size.apkt_num)) {
  893. return PLAYER_WR_FINISH;
  894. }
  895. }
  896. return PLAYER_WR_FAILED;
  897. }
  898. static int64_t rm_offset_search_pts(AVStream *pStream, float timepoint)
  899. {
  900. int64_t wanted_pts = (int64_t)(timepoint * 1000);
  901. int index_entry, index_entry_f, index_entry_b;
  902. int64_t pts_f, pts_b;
  903. index_entry_f = av_index_search_timestamp(pStream, wanted_pts, 0);
  904. index_entry_b = av_index_search_timestamp(pStream, wanted_pts, AVSEEK_FLAG_BACKWARD);
  905. if (index_entry_f < 0) {
  906. if (index_entry_b < 0) {
  907. log_error("[%s]not found valid backward index entry\n", __FUNCTION__);
  908. return 0;
  909. } else {
  910. log_print("[%s:%d]time_point=%d pos=0x%llx\n", __FUNCTION__, __LINE__, timepoint, pStream->index_entries[index_entry_b].pos);
  911. return pStream->index_entries[index_entry_b].pos;
  912. }
  913. }
  914. #if 0
  915. if (index_entry_b < 0) {
  916. if (index_entry_f < 0) {
  917. return 0;
  918. } else {
  919. return pStream->index_entries[index_entry_f].pos;
  920. }
  921. }
  922. pts_f = pStream->index_entries[index_entry_f].timestamp;
  923. pts_b = pStream->index_entries[index_entry_b].timestamp;
  924. if ((wanted_pts - pts_b) < (pts_f - wanted_pts)) {
  925. index_entry = index_entry_b;
  926. } else {
  927. index_entry = index_entry_f;
  928. }
  929. #endif
  930. index_entry = index_entry_f;
  931. return pStream->index_entries[index_entry].pos;
  932. }
  933. static int64_t rm_offset_search(play_para_t *am_p, int64_t offset, float time_point)
  934. {
  935. int read_length = 0;
  936. unsigned char *data;
  937. unsigned short video_id = am_p->vstream_info.video_pid;
  938. unsigned short audio_id = am_p->astream_info.audio_pid;
  939. unsigned skip_byte = 0;
  940. unsigned char *pkt;
  941. const unsigned int read_size = 16 * 1024;
  942. int64_t cur_offset = 0;
  943. unsigned short sync_flag = 0;
  944. unsigned int i = 0;
  945. AVStream *pStream;
  946. int retry_get_data = 0;
  947. AVFormatContext *s = am_p->pFormatCtx;
  948. /* first check the video stream index table */
  949. for (i = 0; i < s->nb_streams; i++) {
  950. pStream = s->streams[i];
  951. if (pStream->index == am_p->vstream_info.video_index) {
  952. break;
  953. }
  954. }
  955. if (i < s->nb_streams) {
  956. if (s->index_builded && (pStream->nb_index_entries > 1)) {
  957. cur_offset = rm_offset_search_pts(pStream, time_point);
  958. log_info("rm time search by index:pos=%f offset=%lld\n", time_point, cur_offset);
  959. return cur_offset;
  960. }
  961. }
  962. /* no index, then search byte by byte */
  963. data = MALLOC(read_size + 12);
  964. if (!data) {
  965. log_error("[%s]malloc failed \n", __FUNCTION__);
  966. return am_p->data_offset;
  967. }
  968. cur_offset = offset;
  969. while (1) {
  970. url_fseek(s->pb, offset, SEEK_SET);
  971. retry_get_data = 0;
  972. do {
  973. read_length = get_buffer(s->pb, data + 12, read_size);
  974. if (read_length <= 0) {
  975. if (read_length == AVERROR(EAGAIN)) {
  976. retry_get_data ++;
  977. continue;
  978. } else {
  979. FREE(data);
  980. log_error("[%s]get data failed. ret=%d\n", __FUNCTION__, read_length);
  981. return am_p->data_offset;
  982. }
  983. } else {
  984. break;
  985. }
  986. } while (retry_get_data < am_p->playctrl_info.read_max_retry_cnt);
  987. pkt = data + 12;
  988. for (;;) {
  989. for (i = 0; i < read_size; i++) {
  990. if (skip_byte > 0) {
  991. skip_byte--;
  992. if (skip_byte == 0) {
  993. //media_packet_header
  994. unsigned short version = (pkt[0] << 8) | pkt[1];
  995. unsigned short size = (pkt[2] << 8) | pkt[3];
  996. unsigned short streamid = (pkt[4] << 8) | pkt[5];
  997. unsigned char flag = pkt[11];
  998. unsigned int timestamp;
  999. if (((version == 0) || (version == 1))
  1000. && (size >= 12)
  1001. && ((streamid == video_id) || (streamid == audio_id))) {
  1002. if ((flag & 2) && (streamid == video_id)) {
  1003. timestamp = (pkt[6] << 24) | (pkt[7] << 16) | (pkt[8] << 8) | pkt[9];
  1004. cur_offset += pkt - (data + 12);
  1005. FREE(data);
  1006. log_print("[%s:%d]find key_frame offset=0x%llx\n", __FUNCTION__, __LINE__, cur_offset);
  1007. return cur_offset;
  1008. } else {
  1009. skip_byte = size;
  1010. }
  1011. sync_flag = 0;
  1012. }
  1013. }
  1014. } else {
  1015. unsigned short version = (pkt[0] << 8) | pkt[1];
  1016. unsigned short size = (pkt[2] << 8) | pkt[3];
  1017. unsigned short streamid = (pkt[4] << 8) | pkt[5];
  1018. if (((version == 0) || (version == 1))
  1019. && (size >= 12)
  1020. && ((streamid == video_id) || (streamid == audio_id))) {
  1021. skip_byte = size;
  1022. sync_flag = 0;
  1023. }
  1024. }
  1025. pkt++;
  1026. }
  1027. sync_flag++;
  1028. MEMCPY(data, data + read_size, 12);
  1029. cur_offset += read_size;
  1030. //log_print("[%s:%d]cur_offset=%x file_size=%x\n",__FUNCTION__, __LINE__,cur_offset,s->file_size);
  1031. if (cur_offset < s->file_size) {
  1032. url_fseek(s->pb, cur_offset, SEEK_SET);
  1033. }
  1034. retry_get_data = 0;
  1035. do {
  1036. read_length = get_buffer(s->pb, data + 12, read_size);
  1037. if ((read_length <= 0) || (sync_flag == 1024)) {
  1038. if (read_length == AVERROR(EAGAIN)) {
  1039. continue;
  1040. } else {
  1041. FREE(data);
  1042. log_error("[%s]get data failed. ret=%d\n", __FUNCTION__, read_length);
  1043. return am_p->data_offset;
  1044. }
  1045. } else {
  1046. break;
  1047. }
  1048. } while (retry_get_data < am_p->playctrl_info.read_max_retry_cnt);
  1049. pkt = data;
  1050. }
  1051. }
  1052. FREE(data);
  1053. log_error("[%s]not found key frame. ret=0\n", __FUNCTION__);
  1054. return am_p->data_offset;
  1055. }
  1056. #ifdef DEBUG_VARIABLE_DUR
  1057. int update_variable_info(play_para_t *para)
  1058. {
  1059. int64_t t_fsize = 0;
  1060. int t_fulltime = 0;
  1061. int byte_rate = para->media_info.stream_info.bitrate >> 3;
  1062. int64_t file_size = para->file_size;
  1063. int full_time = para->state.full_time;
  1064. int aac_nb_frames = 0;
  1065. if (para && para->pFormatCtx && para->pFormatCtx->pb) {
  1066. t_fsize = url_fsize2(para->pFormatCtx->pb);
  1067. log_print("[%s:%dtfsize=%lld fsize=%lld\n", __FUNCTION__, __LINE__, t_fsize, file_size);
  1068. if (t_fsize > file_size && t_fsize > 0) {
  1069. para->pFormatCtx->file_size = t_fsize;
  1070. para->pFormatCtx->valid_offset = t_fsize;
  1071. para->file_size = t_fsize;
  1072. if (byte_rate) {
  1073. if ((unsigned int)(file_size / byte_rate) == full_time) {
  1074. t_fulltime = t_fsize / byte_rate;
  1075. }
  1076. } else {
  1077. t_fulltime = (unsigned int)(full_time * ((double)t_fsize / (double)file_size));
  1078. }
  1079. log_print("[%s:%d]fulltime=%d tfulltime=%d\n", __FUNCTION__, __LINE__, full_time, t_fulltime);
  1080. if (t_fulltime > para->state.full_time) {
  1081. para->state.full_time = t_fulltime;
  1082. para->pFormatCtx->duration = t_fulltime * AV_TIME_BASE;
  1083. }
  1084. } else {
  1085. para->pFormatCtx->valid_offset = INT64_MAX; /*Is a no ended streaming*/
  1086. }
  1087. }
  1088. //log_print("[%s:%d]stream_type=%d fulltime=%d aformat=%d\n", __FUNCTION__, __LINE__, para->stream_type, para->state.full_time,para->astream_info.audio_format = AFORMAT_AAC);
  1089. return PLAYER_SUCCESS;
  1090. }
  1091. #endif
  1092. int time_search(play_para_t *am_p)
  1093. {
  1094. AVFormatContext *s = am_p->pFormatCtx;
  1095. float time_point = am_p->playctrl_info.time_point;
  1096. int64_t timestamp = 0;
  1097. int64_t offset = 0;
  1098. unsigned int temp = 0;
  1099. int stream_index = -1;
  1100. int64_t ret = PLAYER_SUCCESS;
  1101. #ifdef ANDROID
  1102. int seek_flags = am_getconfig_bool("media.libplayer.seek.fwdsearch") ? 0 : AVSEEK_FLAG_BACKWARD;
  1103. #else
  1104. int seek_flags = AVSEEK_FLAG_BACKWARD;
  1105. #endif
  1106. int sample_size;
  1107. //-----------------------------------
  1108. // forcing updating the value of <first_time> when seeking to the start of file,
  1109. // other wise: <bug 69038 > will happend,
  1110. // in this case, after push the <last_song_buton>, the libplayer will seek to start of file instead of
  1111. // switching to last song, then <first_time> was not the value of first packge and become unvalid,
  1112. // need to update it;
  1113. if(time_point==0)
  1114. am_p->state.first_time=-1;
  1115. //----------------------------------
  1116. url_start_user_seek(s->pb);
  1117. /* If swith audio, then use audio stream index */
  1118. if (am_p->playctrl_info.seek_base_audio) {
  1119. seek_flags |= AVSEEK_FLAG_ANY;
  1120. stream_index = am_p->astream_info.audio_index;
  1121. am_p->playctrl_info.seek_base_audio = 0;
  1122. log_info("[time_search]switch audio, audio_idx=%d time=%f\n", stream_index, time_point);
  1123. }
  1124. if (s->duration > 0) {
  1125. temp = (unsigned int)(s->duration / AV_TIME_BASE);
  1126. log_info("[time_search:%d]time_point =%f temp=%d duration= %lld\n", __LINE__, time_point, temp, s->duration);
  1127. }
  1128. /* if seeking requested, we execute it */
  1129. if (url_support_time_seek(s->pb) && time_point >= 0) {
  1130. log_info("[time_search:%d] direct seek to time_point =%f\n", __LINE__, time_point);
  1131. ret = url_fseektotime(s->pb, time_point, seek_flags);
  1132. if (ret >= 0) {
  1133. av_read_frame_flush(s);
  1134. am_p->discontinue_point = (int)ret;
  1135. am_p->playctrl_info.time_point = (int)ret;
  1136. log_info("[time_search:%d] direct seek discontinue_point =%d\n", __LINE__, am_p->discontinue_point);
  1137. ret = PLAYER_SUCCESS;
  1138. goto searchexit;
  1139. }
  1140. /*failed*/
  1141. log_error("[time_search:%d] seek failed , ret=%d\n", __LINE__, ret);
  1142. ret = PLAYER_SEEK_FAILED;
  1143. goto searchexit;
  1144. } else if (time_point <= temp || temp <= 0) {
  1145. if (am_p->file_type == AVI_FILE ||
  1146. am_p->file_type == MP4_FILE ||
  1147. am_p->file_type == MKV_FILE ||
  1148. am_p->file_type == FLV_FILE ||
  1149. am_p->file_type == MOV_FILE ||
  1150. am_p->file_type == P2P_FILE ||
  1151. am_p->file_type == ASF_FILE ||
  1152. am_p->file_type == STREAM_FILE) {
  1153. if (am_p->file_type == AVI_FILE && !s->seekable) {
  1154. time_point = am_p->state.current_time;
  1155. }
  1156. timestamp = (int64_t)(time_point * AV_TIME_BASE);
  1157. /* add the stream start time */
  1158. if (s->start_time != (int64_t)AV_NOPTS_VALUE) {
  1159. timestamp += s->start_time;
  1160. }
  1161. if (timestamp == s->start_time) {
  1162. if (am_p->file_type == AVI_FILE) {
  1163. //stream_index = am_p->first_index;
  1164. seek_flags |= AVSEEK_FLAG_ANY;
  1165. }
  1166. }
  1167. if (am_p->vstream_info.video_format == VFORMAT_MJPEG ||
  1168. am_p->file_type == MKV_FILE) {
  1169. seek_flags |= AVSEEK_FLAG_ANY;
  1170. }
  1171. log_info("[time_search:%d] stream_index %d, time_point=%f timestamp=%lld start_time=%lld\n",
  1172. __LINE__, stream_index, time_point, timestamp, s->start_time);
  1173. if ((am_p->vstream_info.video_index == -1 || !am_p->vstream_info.has_video)
  1174. && am_p->stream_type != STREAM_ES) {
  1175. offset = ((int64_t)(time_point * (s->bit_rate >> 3)));
  1176. ret = url_fseek(s->pb, offset, SEEK_SET);
  1177. if (ret < 0) {
  1178. log_info("%s: could not seek to position 0x%llx ret=0x%llx\n", s->filename, offset, ret);
  1179. ret = PLAYER_SEEK_FAILED;
  1180. goto searchexit;
  1181. }
  1182. } else {
  1183. log_info("[%s:%d] stream_index=%d, timestamp=%llx, seek_flags=%d,\n",__FUNCTION__, __LINE__,stream_index, timestamp, seek_flags);
  1184. ret = (int64_t)av_seek_frame(s, stream_index, timestamp, seek_flags);
  1185. if (ret < 0) {
  1186. log_info("[%s] could not seek to position %0.3f s ret=%lld\n", __FUNCTION__, (double)timestamp / AV_TIME_BASE, ret);
  1187. ret = PLAYER_SEEK_FAILED;
  1188. goto searchexit;
  1189. }
  1190. offset = url_ftell(s->pb);
  1191. if ((am_p->playctrl_info.last_seek_time_point != (int)time_point)
  1192. && (am_p->playctrl_info.last_seek_offset == offset)) {
  1193. am_p->playctrl_info.seek_offset_same = 1;
  1194. } else {
  1195. am_p->playctrl_info.seek_offset_same = 0;
  1196. am_p->playctrl_info.last_seek_offset = offset;
  1197. }
  1198. am_p->playctrl_info.last_seek_time_point = time_point;
  1199. }
  1200. } else {
  1201. if (((am_p->file_type == MPEG_FILE&&time_point > 0) || (am_p->file_type == APE_FILE&& time_point >= 0))
  1202. && !am_p->playctrl_info.seek_frame_fail
  1203. && (s->pb && !s->pb->is_slowmedia)) {
  1204. timestamp = (int64_t)(time_point * AV_TIME_BASE);
  1205. if (s->start_time != (int64_t)AV_NOPTS_VALUE) {
  1206. timestamp += s->start_time;
  1207. }
  1208. log_info("av_seek_frame:time_point = %f timestamp=%x, starttime=%xn", time_point, timestamp, s->start_time);
  1209. ret = (int64_t)av_seek_frame(s, stream_index, timestamp, seek_flags);
  1210. if (ret >= 0) {
  1211. ret = PLAYER_SUCCESS;
  1212. goto searchexit;
  1213. } else {
  1214. am_p->playctrl_info.seek_frame_fail = 1;
  1215. }
  1216. }
  1217. if (am_p->file_type == MPEG_FILE && time_point > 0 && am_p->pFormatCtx->is_vbr == 1) {
  1218. double factor_tm = (double)time_point * AV_TIME_BASE / (double)am_p->pFormatCtx->duration;
  1219. if (factor_tm > 1.0 || factor_tm < 0.0) {
  1220. offset = ((int64_t)(time_point * (s->bit_rate >> 3)));
  1221. }
  1222. else {
  1223. offset = (int64_t)(s->valid_offset * factor_tm);
  1224. }
  1225. }
  1226. else {
  1227. offset = ((int64_t)(time_point * (s->bit_rate >> 3)));
  1228. }
  1229. log_info("time_point = %f bit_rate=%x offset=0x%llx\n", time_point, s->bit_rate, offset);
  1230. if (am_p->file_type == RM_FILE) {
  1231. if (offset > 0) {
  1232. offset = rm_offset_search(am_p, am_p->data_offset + offset, time_point);
  1233. } else {
  1234. offset = am_p->data_offset;
  1235. }
  1236. }
  1237. /**
  1238. * all of PCM format need align to sample size
  1239. * **/
  1240. if (am_p->file_type == WAV_FILE) { //&&am_p->astream_info.audio_format == AFORMAT_ADPCM) {
  1241. AVCodecContext *codec = s->streams[am_p->astream_info.audio_index]->codec;
  1242. if (codec->sample_fmt == 0) { //AV_SAMPLE_FMT_U8
  1243. sample_size = 1;
  1244. } else if (codec->sample_fmt == 2) { //AV_SAMPLE_FMT_S32
  1245. sample_size = 4;
  1246. } else {
  1247. sample_size = 2;
  1248. }
  1249. offset = /*am_p->data_offset + */((int64_t)(time_point * (s->bit_rate >> 3)));
  1250. offset -= offset % codec->block_align;
  1251. offset -= (offset % (codec->channels * sample_size));
  1252. offset += am_p->data_offset;
  1253. }
  1254. log_info("time_point = %f offset=%llx \n", time_point, offset);
  1255. if (offset > s->valid_offset) {
  1256. if (time_point > am_p->state.full_time) {
  1257. offset = url_ftell(s->pb);
  1258. log_info("seek offset exceed, use current 0x%llx\n", offset);
  1259. } else {
  1260. timestamp = (int64_t)(time_point * AV_TIME_BASE);
  1261. offset = s->file_size * (timestamp - s->start_time)/(s->duration - s->start_time);
  1262. log_info("## file_size=%llx, time_point=%f, timestamp=%lld,s->duration=%lld,offset=%llx,--------\n",
  1263. s->file_size, time_point, timestamp, s->duration, offset);
  1264. if (offset > s->valid_offset){
  1265. offset = url_ftell(s->pb);
  1266. }
  1267. }
  1268. }
  1269. ret = url_fseek(s->pb, offset, SEEK_SET);
  1270. if (ret < 0) {
  1271. log_info("%s: could not seek to position 0x%llx ret=0x%llx\n", s->filename, offset, ret);
  1272. ret = PLAYER_SEEK_FAILED;
  1273. goto searchexit;
  1274. }
  1275. else if (am_p->off_init == 0 && am_p->file_type == MPEG_FILE)
  1276. {
  1277. #define MAX_DIFF_PTS 100
  1278. #define MAX_FIND_NUM 10
  1279. char is_find = 0;
  1280. int64_t l_off = -5;
  1281. int64_t r_off = 5;
  1282. char num_search = MAX_FIND_NUM;
  1283. int64_t tot_dur = am_p->pFormatCtx->duration/AV_TIME_BASE;
  1284. AVPacket pkt;
  1285. do {
  1286. av_init_packet(&pkt);
  1287. ret = av_read_frame(s, &pkt);
  1288. if (ret != 0) {
  1289. break;
  1290. }
  1291. else {
  1292. double actiont = 0.0;
  1293. int64_t pts_diff = 0;
  1294. pts_diff = (int64_t)time_point - pkt.pts/90000;
  1295. //if search time is more than 10, we large the pts diff range.
  1296. if (num_search <= 5) {
  1297. r_off++;
  1298. l_off--;
  1299. }
  1300. //if the difference between target and getted pts less than 1%, or less than 3s for very short stream, just find it.
  1301. //if (abs(pts_diff * 100) < tot_dur || (abs(pts_diff) <= 3 && tot_dur <= 300)){
  1302. if (pts_diff < r_off && pts_diff > l_off){
  1303. is_find = 1;
  1304. break;
  1305. }
  1306. actiont = (double) abs(pts_diff)/(double)tot_dur;
  1307. // if the pts getted is nomal, then adjust the offset.
  1308. if (actiont <= 1.0 && actiont >= 0.0){
  1309. int64_t cur_off = 0;
  1310. cur_off = (int64_t)((pts_diff >= 0) ? (offset + actiont * (double)s->valid_offset):
  1311. (offset - actiont * (double)s->valid_offset));
  1312. ret = url_fseek(s->pb, cur_off, SEEK_SET);
  1313. // if find a invalid offset, then reback the last valid offset.
  1314. if (ret < 0) {
  1315. ret = url_fseek(s->pb, offset, SEEK_SET);
  1316. break;
  1317. }
  1318. //get new offset.
  1319. offset = cur_off;
  1320. num_search--;
  1321. }
  1322. else {
  1323. break;
  1324. }
  1325. }
  1326. }while(!is_find && num_search > 0);
  1327. //if find or just use the last offset, then confirm the offset.
  1328. if (is_find || num_search <= 0) {
  1329. ret = url_fseek(s->pb, offset, SEEK_SET);
  1330. if (ret < 0) {
  1331. ret = PLAYER_SEEK_FAILED;
  1332. av_free_packet(&pkt);
  1333. goto searchexit;
  1334. }
  1335. }
  1336. av_free_packet(&pkt);
  1337. }
  1338. }
  1339. /* reset seek info */
  1340. //time_point = 0;
  1341. }
  1342. ret = PLAYER_SUCCESS;
  1343. searchexit:
  1344. url_finished_user_seek(s->pb);
  1345. return (int)ret;
  1346. }
  1347. static void write_es_sub_all(int sid, char *buf, int length)
  1348. {
  1349. //log_print("[%s:%d]write_es_sub_all, sid = %d, length = %d, \n", __FUNCTION__, __LINE__, sid, length);
  1350. //log_print("[%s:%d]write_es_sub_all, rdp = %d, wrp = %d, size = %d\n", __FUNCTION__, __LINE__, es_sub_buf[sid].rdp, es_sub_buf[sid].wrp, es_sub_buf[sid].size);
  1351. if (es_sub_buf[sid].rdp < es_sub_buf[sid].wrp) {
  1352. if ((es_sub_buf[sid].wrp + length) <= SUBTITLE_SIZE) {
  1353. memcpy(es_sub_buf[sid].sub_buf + es_sub_buf[sid].wrp, buf, length);
  1354. es_sub_buf[sid].wrp += length;
  1355. } else {
  1356. memcpy(es_sub_buf[sid].sub_buf + es_sub_buf[sid].wrp, buf, SUBTITLE_SIZE - es_sub_buf[sid].wrp);
  1357. memcpy(es_sub_buf[sid].sub_buf, buf + SUBTITLE_SIZE - es_sub_buf[sid].wrp, length - SUBTITLE_SIZE + es_sub_buf[sid].wrp);
  1358. es_sub_buf[sid].wrp += length;
  1359. es_sub_buf[sid].wrp %= SUBTITLE_SIZE;
  1360. if (es_sub_buf[sid].wrp > es_sub_buf[sid].rdp) {
  1361. es_sub_buf[sid].rdp = es_sub_buf[sid].wrp;
  1362. }
  1363. }
  1364. } else {
  1365. if ((es_sub_buf[sid].wrp + length) < SUBTITLE_SIZE) {
  1366. memcpy(es_sub_buf[sid].sub_buf + es_sub_buf[sid].wrp, buf, length);
  1367. es_sub_buf[sid].wrp += length;
  1368. if ((es_sub_buf[sid].wrp > es_sub_buf[sid].rdp) && (es_sub_buf[sid].size == SUBTITLE_SIZE)) {
  1369. es_sub_buf[sid].rdp = es_sub_buf[sid].wrp;
  1370. }
  1371. } else {
  1372. memcpy(es_sub_buf[sid].sub_buf + es_sub_buf[sid].wrp, buf, SUBTITLE_SIZE - es_sub_buf[sid].wrp);
  1373. memcpy(es_sub_buf[sid].sub_buf, buf + SUBTITLE_SIZE - es_sub_buf[sid].wrp, length - SUBTITLE_SIZE + es_sub_buf[sid].wrp);
  1374. es_sub_buf[sid].wrp += length;
  1375. es_sub_buf[sid].wrp %= SUBTITLE_SIZE;
  1376. es_sub_buf[sid].rdp = es_sub_buf[sid].wrp;
  1377. }
  1378. }
  1379. if (es_sub_buf[sid].wrp > es_sub_buf[sid].rdp) {
  1380. es_sub_buf[sid].size = es_sub_buf[sid].wrp - es_sub_buf[sid].rdp;
  1381. } else {
  1382. es_sub_buf[sid].size = SUBTITLE_SIZE - es_sub_buf[sid].rdp + es_sub_buf[sid].wrp;
  1383. }
  1384. //log_print("[%s:%d]write_es_sub_all, rdp = %d, wrp = %d, size=%d \n", __FUNCTION__, __LINE__, es_sub_buf[sid].rdp, es_sub_buf[sid].wrp, es_sub_buf[sid].size);
  1385. }
  1386. int write_av_packet(play_para_t *para)
  1387. {
  1388. am_packet_t *pkt = para->p_pkt;
  1389. int write_bytes = 0, len = 0, ret;
  1390. unsigned char *buf;
  1391. int size ;
  1392. float value;
  1393. int dump_data_mode = 0;
  1394. char dump_path[128];
  1395. signed short audio_idx = para->astream_info.audio_index;
  1396. if (pkt->avpkt_newflag && pkt->avpkt_isvalid &&
  1397. pkt->type == CODEC_AUDIO && pkt->avpkt->stream_index!=audio_idx)
  1398. {
  1399. log_print("[%s:%d] free packet pkttype=%d,switchaid:%d,pktaid:%d,curaid:%d,valid=%d,newflag=%d,size=%d,\n", __FUNCTION__, __LINE__,
  1400. pkt->type, para->playctrl_info.switch_audio_id, pkt->avpkt->stream_index, audio_idx,
  1401. pkt->avpkt_isvalid,pkt->avpkt_newflag,pkt->data_size);
  1402. if ((pkt->type == CODEC_AUDIO) && (!para->playctrl_info.raw_mode)) {
  1403. para->write_size.apkt_num ++;
  1404. }
  1405. if (pkt->avpkt) {
  1406. av_free_packet(pkt->avpkt);
  1407. }
  1408. pkt->avpkt_isvalid = 0;
  1409. pkt->avpkt_newflag= 0;
  1410. pkt->data_size = 0;
  1411. return PLAYER_SUCCESS;
  1412. }
  1413. #ifdef ANDROID
  1414. if (am_getconfig_float("media.libplayer.dumpmode", &value) == 0) {
  1415. dump_data_mode = (int)value;
  1416. }
  1417. #endif
  1418. if (dump_data_mode == DUMP_WRITE_RAW_DATA && fdw_raw == -1) {
  1419. sprintf(dump_path, "/temp/pid%d_dump_write.dat", para->player_id);
  1420. fdw_raw = open(dump_path, O_CREAT | O_RDWR, 0666);
  1421. if (fdw_raw < 0) {
  1422. log_error("creat %s failed!fd=%d\n", dump_path, fdw_raw);
  1423. }
  1424. } else {
  1425. if (dump_data_mode == DUMP_WRITE_ES_VIDEO && fdw_video == -1) {
  1426. sprintf(dump_path, "/temp/pid%d_dump_vwrite.dat", para->player_id);
  1427. fdw_video = open(dump_path, O_CREAT | O_RDWR, 0666);
  1428. if (fdw_video < 0) {
  1429. log_error("creat %s failed!fd=%d\n", dump_path, fdw_video);
  1430. }
  1431. }
  1432. if (dump_data_mode == DUMP_WRITE_ES_AUDIO && fdw_audio == -1) {
  1433. sprintf(dump_path, "/temp/pid%d_dump_awrite.dat", para->player_id);
  1434. fdw_audio = open(dump_path, O_CREAT | O_RDWR, 0666);
  1435. if (fdw_audio < 0) {
  1436. log_print("creat %s failed!fd=%d\n", dump_path, fdw_audio);
  1437. }
  1438. }
  1439. }
  1440. if ((para->playctrl_info.fast_forward || para->playctrl_info.fast_backward)
  1441. && para->playctrl_info.seek_offset_same) {
  1442. if (pkt->type == CODEC_VIDEO) {
  1443. para->write_size.vpkt_num ++;
  1444. } else if (pkt->type == CODEC_AUDIO) {
  1445. para->write_size.apkt_num ++;
  1446. } else if (pkt->type == CODEC_COMPLEX) {
  1447. para->write_size.total_bytes += pkt->data_size;
  1448. }
  1449. av_free_packet(pkt->avpkt);
  1450. pkt->avpkt_isvalid = 0;
  1451. return PLAYER_SUCCESS;
  1452. }
  1453. //new packet
  1454. if (pkt->avpkt_newflag) {
  1455. if (pkt->type != CODEC_SUBTITLE) {
  1456. if (pkt->avpkt_isvalid) {
  1457. if (!pkt->pts_checkin_ok) {
  1458. ret = check_in_pts(para);
  1459. if (ret != PLAYER_SUCCESS) {
  1460. log_error("check in pts failed\n");
  1461. return PLAYER_WR_FAILED;
  1462. }
  1463. pkt->pts_checkin_ok = 1;
  1464. }
  1465. }
  1466. ret = write_header(para);
  1467. if (ret == PLAYER_WR_FAILED) {
  1468. log_error("[%s]write header failed!\n", __FUNCTION__);
  1469. return PLAYER_WR_FAILED;
  1470. } else if (ret == PLAYER_WR_AGAIN) {
  1471. if (!para->enable_rw_on_pause) {
  1472. player_thread_wait(para, RW_WAIT_TIME);
  1473. }
  1474. return PLAYER_SUCCESS;
  1475. }
  1476. } else {
  1477. process_es_subtitle(para);
  1478. }
  1479. pkt->avpkt_newflag = 0;
  1480. }
  1481. buf = pkt->data;
  1482. size = pkt->data_size ;
  1483. if (size <= 0 && pkt->avpkt_isvalid) {
  1484. if ((pkt->type == CODEC_VIDEO) && (!para->playctrl_info.raw_mode)) {
  1485. para->write_size.vpkt_num ++;
  1486. } else if ((pkt->type == CODEC_AUDIO) && (!para->playctrl_info.raw_mode)) {
  1487. para->write_size.apkt_num ++;
  1488. }
  1489. if (pkt->avpkt) {
  1490. av_free_packet(pkt->avpkt);
  1491. }
  1492. pkt->avpkt_isvalid = 0;
  1493. pkt->data_size =0;
  1494. }
  1495. if (pkt->type == CODEC_AUDIO && para->astream_info.audio_format == AFORMAT_APE) {
  1496. while (size > 0 && pkt->avpkt_isvalid) {
  1497. if (pkt->type == CODEC_SUBTITLE) {
  1498. int i;
  1499. for (i = 0; i < 8; i++) {
  1500. if (pkt->avpkt->stream_index == es_sub_buf[i].subid) {
  1501. //log_print("[%s:%d]i = %d, pkt->avpkt->stream_index = %d, \n", __FUNCTION__, __LINE__, i, pkt->avpkt->stream_index);
  1502. write_es_sub_all(i, (char *)buf, size);
  1503. break;
  1504. }
  1505. }
  1506. }
  1507. if ((para->sstream_info.sub_index != pkt->avpkt->stream_index) && (pkt->type == CODEC_SUBTITLE)) {
  1508. if (pkt->avpkt) {
  1509. av_free_packet(pkt->avpkt);
  1510. }
  1511. pkt->avpkt_isvalid = 0;
  1512. pkt->data_size = 0;
  1513. break;
  1514. }
  1515. //if ape frame write 10k every time
  1516. int nCurrentWriteCount = (size > AUDIO_WRITE_SIZE_PER_TIME) ? AUDIO_WRITE_SIZE_PER_TIME : size;
  1517. write_bytes = codec_write(pkt->codec, (char *)buf, nCurrentWriteCount);
  1518. if (write_bytes < 0 || write_bytes > nCurrentWriteCount) {
  1519. if (-errno != AVERROR(EAGAIN)) {
  1520. para->playctrl_info.check_lowlevel_eagain_cnt = 0;
  1521. log_print("write codec data failed! ret=%d,errno=%d\n",write_bytes,errno);
  1522. return PLAYER_WR_FAILED;
  1523. } else {
  1524. /* EAGAIN to see if buffer full or write time out too much */
  1525. if (check_avbuffer_enough_for_ape(para)) {
  1526. ++ para->playctrl_info.check_lowlevel_eagain_cnt;
  1527. } else {
  1528. para->playctrl_info.check_lowlevel_eagain_cnt = 0;
  1529. }
  1530. if (para->playctrl_info.check_lowlevel_eagain_cnt > 50) {
  1531. /* reset decoder */
  1532. para->playctrl_info.check_lowlevel_eagain_cnt = 0;
  1533. para->playctrl_info.reset_flag = 1;
  1534. set_black_policy(0);
  1535. para->playctrl_info.end_flag = 1;
  1536. if (para->state.start_time != -1) {
  1537. para->playctrl_info.time_point = (para->state.pts_video - para->state.start_time) / PTS_FREQ;
  1538. } else {
  1539. para->playctrl_info.time_point = para->state.pts_video / PTS_FREQ;
  1540. }
  1541. log_print("$$$$$$[type:%d] write blocked, need reset decoder!$$$$$$\n", pkt->type);
  1542. }
  1543. pkt->data += len;
  1544. pkt->data_size -= len;
  1545. if (!para->enable_rw_on_pause) {
  1546. player_thread_wait(para, RW_WAIT_TIME);
  1547. }
  1548. if (para->playctrl_info.check_lowlevel_eagain_cnt > 0) {
  1549. log_print("[%s]eagain:data_size=%d type=%d rsize=%lld wsize=%lld cnt=%d\n", \
  1550. __FUNCTION__, nCurrentWriteCount, pkt->type, para->read_size.total_bytes, \
  1551. para->write_size.total_bytes, para->playctrl_info.check_lowlevel_eagain_cnt);
  1552. }
  1553. return PLAYER_SUCCESS;
  1554. }
  1555. } else {
  1556. int dsize;
  1557. if (fdw_raw >= 0 && pkt->type == CODEC_COMPLEX) {
  1558. dsize = write(fdw_raw, buf, write_bytes);
  1559. } else {
  1560. if (fdw_video >= 0 && pkt->type == CODEC_VIDEO) {
  1561. dsize = write(fdw_video, buf, write_bytes);
  1562. }
  1563. if (fdw_audio >= 0 && pkt->type == CODEC_AUDIO) {
  1564. dsize = write(fdw_audio, buf, write_bytes);
  1565. }
  1566. }
  1567. if ((fdw_raw >= 0 || fdw_audio >= 0 || fdw_video >= 0) &&
  1568. (dsize != write_bytes)) {
  1569. log_print("dump data write failed!size=%d len=%d\n", size, len);
  1570. }
  1571. // log_print("[%s:%d]dump data write size=%d, want len=%d\n", __FUNCTION__, __LINE__, size, len);
  1572. }
  1573. para->playctrl_info.check_lowlevel_eagain_cnt = 0;
  1574. len += write_bytes;
  1575. if (len == pkt->data_size) {
  1576. if ((pkt->type == CODEC_AUDIO) && (!para->playctrl_info.raw_mode)) {
  1577. para->write_size.apkt_num ++;
  1578. }
  1579. if (para->playctrl_info.raw_mode) {
  1580. para->write_size.total_bytes += len;
  1581. }
  1582. if (pkt->avpkt) {
  1583. av_free_packet(pkt->avpkt);
  1584. }
  1585. pkt->avpkt_isvalid = 0;
  1586. pkt->data_size = 0;
  1587. //log_print("[%s:%d]write finish pkt->data_size=%d\r",__FUNCTION__, __LINE__,pkt->data_size);
  1588. break;
  1589. } else if (len < pkt->data_size) {
  1590. buf += write_bytes;
  1591. size -= write_bytes;
  1592. } else {
  1593. return PLAYER_WR_FAILED;
  1594. }
  1595. }
  1596. } else {
  1597. while (size > 0 && pkt->avpkt_isvalid) {
  1598. if (pkt->type == CODEC_SUBTITLE) {
  1599. int i;
  1600. //log_print("## 111 [%s:%d]i = %d, pkt->avpkt->stream_index = %d, \n", __FUNCTION__, __LINE__, i, pkt->avpkt->stream_index);
  1601. for (i = 0; i < 8; i++) {
  1602. //subid should be equal to stream->id
  1603. //if (pkt->avpkt->stream_index == es_sub_buf[i].subid) {
  1604. if (para->pFormatCtx->streams[pkt->avpkt->stream_index]->id == es_sub_buf[i].subid) {
  1605. //log_print("## 222 [%s:%d]i = %d, pkt->avpkt->stream_index = %d, size=%d,----------\n", __FUNCTION__, __LINE__, i, pkt->avpkt->stream_index, size);
  1606. write_es_sub_all(i, (char *)buf, size);
  1607. break;
  1608. }
  1609. }
  1610. }
  1611. if ((para->sstream_info.sub_index != pkt->avpkt->stream_index) && (pkt->type == CODEC_SUBTITLE)) {
  1612. if (pkt->avpkt) {
  1613. av_free_packet(pkt->avpkt);
  1614. }
  1615. pkt->avpkt_isvalid = 0;
  1616. pkt->data_size = 0;
  1617. break;
  1618. }
  1619. /*
  1620. * add two property media.amplayer.vbufthreshold and media.amplayer.abufthreshold to control the a-v-buf data
  1621. * level, for some raw read file, the audio data would be dropped after switch audio stream, if the abuf level is too big,
  1622. * the dropped audio data would be too much and lead to apts bigger than vpts
  1623. */
  1624. if (para->vstream_info.has_video && para->astream_info.has_audio
  1625. && para->astream_num > 1 && para->playctrl_info.raw_mode&&get_player_state(para) != PLAYER_BUFFERING) {
  1626. char value[PROPERTY_VALUE_MAX]={0};
  1627. float vbuf_threshold = 0.8;
  1628. float abuf_threshold = 0.6;
  1629. if (property_get("media.amplayer.vbufthreshold", value, NULL) > 0)
  1630. vbuf_threshold = atof(value);
  1631. memset(value,0,sizeof(value));
  1632. if (property_get("media.amplayer.abufthreshold", value, NULL) > 0)
  1633. abuf_threshold = atof(value);
  1634. if (para->state.video_bufferlevel >= vbuf_threshold || para->state.audio_bufferlevel >= abuf_threshold)
  1635. {
  1636. player_thread_wait(para, 10*1000);
  1637. return PLAYER_SUCCESS;
  1638. }
  1639. }
  1640. write_bytes = codec_write(pkt->codec, (char *)buf, size);
  1641. if (write_bytes < 0 || write_bytes > size) {
  1642. if (-errno != AVERROR(EAGAIN)) {
  1643. para->playctrl_info.check_lowlevel_eagain_cnt = 0;
  1644. log_print("write codec data failed! ret=%d,errno=%d\n",write_bytes,errno);
  1645. return PLAYER_WR_FAILED;
  1646. } else {
  1647. /* EAGAIN to see if buffer full or write time out too much */
  1648. if (check_avbuffer_enough(para)) {
  1649. ++ para->playctrl_info.check_lowlevel_eagain_cnt;
  1650. } else {
  1651. para->playctrl_info.check_lowlevel_eagain_cnt = 0;
  1652. }
  1653. if (para->playctrl_info.check_lowlevel_eagain_cnt > 50) {
  1654. /* reset decoder */
  1655. para->playctrl_info.check_lowlevel_eagain_cnt = 0;
  1656. para->playctrl_info.reset_flag = 1;
  1657. set_black_policy(0);
  1658. para->playctrl_info.end_flag = 1;
  1659. if (para->state.start_time != -1) {
  1660. para->playctrl_info.time_point = (para->state.pts_video - para->state.start_time) / PTS_FREQ;
  1661. } else {
  1662. para->playctrl_info.time_point = para->state.pts_video / PTS_FREQ;
  1663. }
  1664. log_print("$$$$$$[type:%d] write blocked, need reset decoder!$$$$$$\n", pkt->type);
  1665. }
  1666. pkt->data += len;
  1667. pkt->data_size -= len;
  1668. if (!para->enable_rw_on_pause) {
  1669. player_thread_wait(para, RW_WAIT_TIME);
  1670. }
  1671. if (para->playctrl_info.check_lowlevel_eagain_cnt > 0) {
  1672. log_print("[%s]eagain:data_size=%d type=%d rsize=%lld wsize=%lld cnt=%d\n", \
  1673. __FUNCTION__, pkt->data_size, pkt->type, para->read_size.total_bytes, \
  1674. para->write_size.total_bytes, para->playctrl_info.check_lowlevel_eagain_cnt);
  1675. }
  1676. return PLAYER_SUCCESS;
  1677. }
  1678. } else {
  1679. int dsize;
  1680. if (fdw_raw >= 0 && pkt->type == CODEC_COMPLEX) {
  1681. dsize = write(fdw_raw, buf, write_bytes);
  1682. } else {
  1683. if (fdw_video >= 0 && pkt->type == CODEC_VIDEO) {
  1684. dsize = write(fdw_video, buf, write_bytes);
  1685. }
  1686. if (fdw_audio >= 0 && pkt->type == CODEC_AUDIO) {
  1687. dsize = write(fdw_audio, buf, write_bytes);
  1688. }
  1689. }
  1690. if ((fdw_raw >= 0 || fdw_video >= 0 || fdw_audio >= 0) &&
  1691. (dsize != write_bytes)) {
  1692. log_print("dump data write failed!size=%d len=%d\n", size, len);
  1693. }
  1694. // log_print("[%s:%d]dump data write size=%d, want len=%d\n", __FUNCTION__, __LINE__, size, len);
  1695. para->playctrl_info.check_lowlevel_eagain_cnt = 0;
  1696. len += write_bytes;
  1697. if (len == pkt->data_size) {
  1698. if ((pkt->type == CODEC_VIDEO) && (!para->playctrl_info.raw_mode)) {
  1699. para->write_size.vpkt_num ++;
  1700. } else if ((pkt->type == CODEC_AUDIO) && (!para->playctrl_info.raw_mode)) {
  1701. para->write_size.apkt_num ++;
  1702. }
  1703. if (para->playctrl_info.raw_mode) {
  1704. para->write_size.total_bytes += len;
  1705. }
  1706. if (pkt->avpkt) {
  1707. av_free_packet(pkt->avpkt);
  1708. }
  1709. pkt->avpkt_isvalid = 0;
  1710. pkt->data_size = 0;
  1711. //log_print("[%s:%d]write finish pkt->data_size=%d\r",__FUNCTION__, __LINE__,pkt->data_size);
  1712. break;
  1713. } else if (len < pkt->data_size) {
  1714. buf += write_bytes;
  1715. size -= write_bytes;
  1716. } else {
  1717. return PLAYER_WR_FAILED;
  1718. }
  1719. }
  1720. }
  1721. }
  1722. if (check_write_finish(para) == PLAYER_WR_FINISH) {
  1723. if (((para->stream_type == STREAM_ES) || (para->stream_type == STREAM_VIDEO))
  1724. && (para->vstream_info.video_format == VFORMAT_H264)) {
  1725. h264_write_end_header(para);
  1726. }
  1727. if (fdw_raw >= 0) {
  1728. close(fdw_raw);
  1729. }
  1730. if (fdw_video >= 0) {
  1731. close(fdw_video);
  1732. }
  1733. if (fdw_audio >= 0) {
  1734. close(fdw_audio);
  1735. }
  1736. return PLAYER_WR_FINISH;
  1737. }
  1738. return PLAYER_SUCCESS;
  1739. }
  1740. int check_in_pts(play_para_t *para)
  1741. {
  1742. am_packet_t *pkt = para->p_pkt;
  1743. int last_duration = 0;
  1744. static int last_v_duration = 0, last_a_duration = 0;
  1745. int64_t pts;
  1746. float time_base_ratio = 0;
  1747. long long start_time = 0;
  1748. char value[PROPERTY_VALUE_MAX];
  1749. int ret;
  1750. int pts_offset = 0;
  1751. ret = property_get("media.apts.offset",value,NULL);
  1752. //log_error("pts_offset = %d, value = %s \n", pts_offset, value);
  1753. if (ret > 0)
  1754. {
  1755. pts_offset = atoi(value);
  1756. }
  1757. //log_error("pts_offset = %d, value = %s, ret = %d \n", pts_offset, value, ret);
  1758. if (pkt->type == CODEC_AUDIO) {
  1759. time_base_ratio = para->astream_info.audio_duration;
  1760. start_time = para->astream_info.start_time;
  1761. last_duration = last_a_duration;
  1762. } else if (pkt->type == CODEC_VIDEO) {
  1763. time_base_ratio = para->vstream_info.video_pts;
  1764. start_time = para->vstream_info.start_time;
  1765. last_duration = last_v_duration;
  1766. }
  1767. if (para->stream_type == STREAM_ES && (pkt->type == CODEC_VIDEO || pkt->type == CODEC_AUDIO)) {
  1768. if ((int64_t)INT64_0 != pkt->avpkt->pts) {
  1769. pts = pkt->avpkt->pts * time_base_ratio;
  1770. /*** for mmsh,asf,the pts may rollback,so Don't care the pts < start time.,some other streams have the same problem.
  1771. for numbric only pts,do mul on demux..
  1772. if (pts < start_time) {
  1773. pts = pts * last_duration;
  1774. }
  1775. **/
  1776. if (pkt->type == CODEC_AUDIO){
  1777. pts += pts_offset;
  1778. //log_error("pts_offset = %d, pts = 0x%llx\n", pts_offset, pts);
  1779. }
  1780. if (codec_checkin_pts(pkt->codec, pts) != 0) {
  1781. log_error("ERROR pid[%d]: check in pts error!\n", para->player_id);
  1782. return PLAYER_PTS_ERROR;
  1783. }
  1784. ///log_print("[check_in_pts:%d]type=%d pkt->pts=%llx pts=%llx start_time=%llx \n",__LINE__,pkt->type,pkt->avpkt->pts,pts, start_time);
  1785. } else if ((int64_t)INT64_0 != pkt->avpkt->dts) {
  1786. pts = pkt->avpkt->dts * time_base_ratio * last_duration;
  1787. //log_print("[check_in_pts:%d]type=%d pkt->dts=%llx pts=%llx time_base_ratio=%.2f last_duration=%d\n",__LINE__,pkt->type,pkt->avpkt->dts,pts,time_base_ratio,last_duration);
  1788. if (pkt->type == CODEC_AUDIO){
  1789. pts += pts_offset;
  1790. //log_error("pts_offset = %d, pts = 0x%llx\n", pts_offset, pts);
  1791. }
  1792. if (codec_checkin_pts(pkt->codec, pts) != 0) {
  1793. log_error("ERROR pid[%d]: check in dts error!\n", para->player_id);
  1794. return PLAYER_PTS_ERROR;
  1795. }
  1796. if (pkt->type == CODEC_AUDIO) {
  1797. last_a_duration = pkt->avpkt->duration ? pkt->avpkt->duration : 1;
  1798. } else if (pkt->type == CODEC_VIDEO) {
  1799. last_v_duration = pkt->avpkt->duration ? pkt->avpkt->duration : 1;
  1800. }
  1801. } else {
  1802. if (!para->astream_info.check_first_pts && pkt->type == CODEC_AUDIO) {
  1803. if (codec_checkin_pts(pkt->codec, 0) != 0) {
  1804. log_print("ERROR pid[%d]: check in 0 to audio pts error!\n", para->player_id);
  1805. return PLAYER_PTS_ERROR;
  1806. }
  1807. }
  1808. if (!para->vstream_info.check_first_pts && pkt->type == CODEC_VIDEO) {
  1809. if (codec_checkin_pts(pkt->codec, 0) != 0) {
  1810. log_print("ERROR pid[%d]: check in 0 to audio pts error!\n", para->player_id);
  1811. return PLAYER_PTS_ERROR;
  1812. }
  1813. }
  1814. }
  1815. if (pkt->type == CODEC_AUDIO && !para->astream_info.check_first_pts) {
  1816. para->astream_info.check_first_pts = 1;
  1817. } else if (pkt->type == CODEC_VIDEO && !para->vstream_info.check_first_pts) {
  1818. para->vstream_info.check_first_pts = 1;
  1819. }
  1820. } else if (para->stream_type == STREAM_AUDIO) {
  1821. if (!para->astream_info.check_first_pts) {
  1822. if (!url_support_time_seek(para->pFormatCtx->pb) &&
  1823. (para->playctrl_info.time_point == -1)) {
  1824. para->playctrl_info.time_point = 0;
  1825. }
  1826. pts = para->playctrl_info.time_point * PTS_FREQ;
  1827. if (pkt->type == CODEC_AUDIO){
  1828. pts += pts_offset;
  1829. //log_error("pts_offset = %d, pts = 0x%llx\n", pts_offset, pts);
  1830. }
  1831. if (codec_checkin_pts(pkt->codec, pts) != 0) {
  1832. log_print("ERROR pid[%d]: check in 0 to audio pts error!\n", para->player_id);
  1833. return PLAYER_PTS_ERROR;
  1834. }
  1835. para->astream_info.check_first_pts = 1;
  1836. }
  1837. }
  1838. return PLAYER_SUCCESS;
  1839. }
  1840. int set_header_info(play_para_t *para)
  1841. {
  1842. int ret;
  1843. am_packet_t *pkt = para->p_pkt;
  1844. if (pkt->avpkt_newflag) {
  1845. if (pkt->hdr) {
  1846. pkt->hdr->size = 0;
  1847. }
  1848. if (pkt->type == CODEC_VIDEO) {
  1849. if (((para->vstream_info.video_format == VFORMAT_H264) || (para->vstream_info.video_format == VFORMAT_H264MVC)) &&
  1850. (para->file_type != STREAM_FILE)) {
  1851. if(para->file_type == AVI_FILE){
  1852. if((pkt->data_size>=3&&(pkt->data[0]==0)&&(pkt->data[1]==0)&&(pkt->data[2]==1))
  1853. ||(pkt->data_size>=4&&(pkt->data[0]==0)&&(pkt->data[1]==0)&&(pkt->data[2]==0)&&(pkt->data[3]==1))){
  1854. return PLAYER_SUCCESS;
  1855. }
  1856. }
  1857. ret = h264_update_frame_header(pkt);
  1858. if (ret != PLAYER_SUCCESS) {
  1859. return ret;
  1860. }
  1861. } else if (para->vstream_info.video_format == VFORMAT_MPEG4) {
  1862. if (para->vstream_info.video_codec_type == VIDEO_DEC_FORMAT_MPEG4_3) {
  1863. return divx3_prefix(pkt);
  1864. } else if (para->vstream_info.video_codec_type == VIDEO_DEC_FORMAT_H263) {
  1865. unsigned char *vld_buf;
  1866. int vld_len, vld_buf_size = para->vstream_info.video_width * para->vstream_info.video_height * 2;
  1867. if (!pkt->data_size) {
  1868. return PLAYER_SUCCESS;
  1869. }
  1870. if ((pkt->data[0] == 0) && (pkt->data[1] == 0) && (pkt->data[2] == 1) && (pkt->data[3] == 0xb6)) {
  1871. return PLAYER_SUCCESS;
  1872. }
  1873. vld_buf = (unsigned char *)MALLOC(vld_buf_size);
  1874. if (!vld_buf) {
  1875. return PLAYER_NOMEM;
  1876. }
  1877. if (para->vstream_info.flv_flag) {
  1878. vld_len = h263vld(pkt->data, vld_buf, pkt->data_size, 1);
  1879. } else {
  1880. if (0 == para->vstream_info.h263_decodable) {
  1881. para->vstream_info.h263_decodable = decodeble_h263(pkt->data);
  1882. if (0 == para->vstream_info.h263_decodable) {
  1883. para->vstream_info.has_video = 0;
  1884. if (para->astream_info.has_audio) {
  1885. set_player_error_no(para, PLAYER_UNSUPPORT_VIDEO);
  1886. update_player_states(para, 1);
  1887. /*set_tsync_enable(0);
  1888. para->playctrl_info.avsync_enable = 0;*/
  1889. } else {
  1890. set_player_state(para, PLAYER_ERROR);
  1891. log_error("[%s]h263 unsupport video and audio, exit\n", __FUNCTION__);
  1892. return PLAYER_UNSUPPORT;
  1893. }
  1894. }
  1895. }
  1896. vld_len = h263vld(pkt->data, vld_buf, pkt->data_size, 0);
  1897. }
  1898. //printf("###%02x %02x %02x %02x %02x %02x %02x %02x###\n", pkt->data[0], pkt->data[1], pkt->data[2], pkt->data[3], pkt->data[4], pkt->data[5], pkt->data[6], pkt->data[7]);
  1899. //printf("###pkt->data_size = %d, vld_buf_size = %d, vld_len = %d###\n", pkt->data_size, vld_buf_size, vld_len);
  1900. if (vld_len > 0) {
  1901. if (pkt->buf) {
  1902. FREE(pkt->buf);
  1903. }
  1904. pkt->buf = vld_buf;
  1905. pkt->buf_size = vld_buf_size;
  1906. pkt->data = pkt->buf;
  1907. pkt->data_size = vld_len;
  1908. } else {
  1909. FREE(vld_buf);
  1910. pkt->data_size = 0;
  1911. }
  1912. }else if (para->vstream_info.video_codec_type == VIDEO_DEC_FORMAT_MPEG4_4) {
  1913. /*
  1914. * for mpeg4, the global headers can be in the bitstream or extradata, encounter two mov divx file, there are not sequence header in
  1915. * the bitstream, so send the extradata to decode frame, if don't send the extradata, the frame is green colour.if the file is divx and
  1916. * global headers exist not only in the bitstream but also in pCtx->extradata, insert the extradata would affect the file play abnormally,
  1917. * so here add the para->file_type == MOV_FILE judgement.
  1918. */
  1919. signed short video_idx = para->vstream_info.video_index;
  1920. AVCodecContext *pCtx= para->pFormatCtx->streams[video_idx]->codec;
  1921. if (para->file_type == MOV_FILE && pCtx->extradata_size) {
  1922. if ((pkt->hdr != NULL) && (pkt->hdr->data != NULL)) {
  1923. FREE(pkt->hdr->data);
  1924. pkt->hdr->data = NULL;
  1925. }
  1926. if (pkt->hdr == NULL) {
  1927. pkt->hdr = MALLOC(sizeof(hdr_buf_t));
  1928. if (!pkt->hdr) {
  1929. log_print("[wvc1_prefix] NOMEM!");
  1930. return PLAYER_FAILED;
  1931. }
  1932. pkt->hdr->data = NULL;
  1933. pkt->hdr->size = 0;
  1934. }
  1935. pkt->hdr->data = MALLOC(pCtx->extradata_size);
  1936. if (pkt->hdr->data == NULL) {
  1937. log_print("[wvc1_prefix] NOMEM!");
  1938. return PLAYER_FAILED;
  1939. }
  1940. memcpy(pkt->hdr->data, (uint8_t *)pCtx->extradata, pCtx->extradata_size);
  1941. pkt->hdr->size = pCtx->extradata_size;
  1942. pkt->avpkt_newflag = 1;
  1943. }
  1944. }
  1945. } else if (para->vstream_info.video_format == VFORMAT_VC1) {
  1946. if (para->vstream_info.video_codec_type == VIDEO_DEC_FORMAT_WMV3) {
  1947. unsigned i, check_sum = 0, data_len = 0;
  1948. if ((pkt->hdr != NULL) && (pkt->hdr->data != NULL)) {
  1949. FREE(pkt->hdr->data);
  1950. pkt->hdr->data = NULL;
  1951. }
  1952. if (pkt->hdr == NULL) {
  1953. pkt->hdr = MALLOC(sizeof(hdr_buf_t));
  1954. if (!pkt->hdr) {
  1955. log_print("[wmv3_prefix]pid=%d NOMEM!", para->player_id);
  1956. return PLAYER_FAILED;
  1957. }
  1958. pkt->hdr->data = NULL;
  1959. pkt->hdr->size = 0;
  1960. }
  1961. if (pkt->avpkt->flags) {
  1962. pkt->hdr->data = MALLOC(para->vstream_info.extradata_size + 26 + 22);
  1963. if (pkt->hdr->data == NULL) {
  1964. log_print("[wmv3_prefix]pid=%d NOMEM!", para->player_id);
  1965. return PLAYER_FAILED;
  1966. }
  1967. pkt->hdr->data[0] = 0;
  1968. pkt->hdr->data[1] = 0;
  1969. pkt->hdr->data[2] = 1;
  1970. pkt->hdr->data[3] = 0x10;
  1971. data_len = para->vstream_info.extradata_size + 4;
  1972. pkt->hdr->data[4] = 0;
  1973. pkt->hdr->data[5] = (data_len >> 16) & 0xff;
  1974. pkt->hdr->data[6] = 0x88;
  1975. pkt->hdr->data[7] = (data_len >> 8) & 0xff;
  1976. pkt->hdr->data[8] = data_len & 0xff;
  1977. pkt->hdr->data[9] = 0x88;
  1978. pkt->hdr->data[10] = 0xff;
  1979. pkt->hdr->data[11] = 0xff;
  1980. pkt->hdr->data[12] = 0x88;
  1981. pkt->hdr->data[13] = 0xff;
  1982. pkt->hdr->data[14] = 0xff;
  1983. pkt->hdr->data[15] = 0x88;
  1984. for (i = 4 ; i < 16 ; i++) {
  1985. check_sum += pkt->hdr->data[i];
  1986. }
  1987. pkt->hdr->data[16] = (check_sum >> 8) & 0xff;
  1988. pkt->hdr->data[17] = check_sum & 0xff;
  1989. pkt->hdr->data[18] = 0x88;
  1990. pkt->hdr->data[19] = (check_sum >> 8) & 0xff;
  1991. pkt->hdr->data[20] = check_sum & 0xff;
  1992. pkt->hdr->data[21] = 0x88;
  1993. pkt->hdr->data[22] = (para->vstream_info.video_width >> 8) & 0xff;
  1994. pkt->hdr->data[23] = para->vstream_info.video_width & 0xff;
  1995. pkt->hdr->data[24] = (para->vstream_info.video_height >> 8) & 0xff;
  1996. pkt->hdr->data[25] = para->vstream_info.video_height & 0xff;
  1997. MEMCPY(pkt->hdr->data + 26, para->vstream_info.extradata, para->vstream_info.extradata_size);
  1998. check_sum = 0;
  1999. data_len = para->vstream_info.extradata_size + 26;
  2000. } else {
  2001. pkt->hdr->data = MALLOC(22);
  2002. if (pkt->hdr->data == NULL) {
  2003. log_print("[wmv3_prefix]pid=%d NOMEM!", para->player_id);
  2004. return PLAYER_FAILED;
  2005. }
  2006. }
  2007. pkt->hdr->data[data_len + 0] = 0;
  2008. pkt->hdr->data[data_len + 1] = 0;
  2009. pkt->hdr->data[data_len + 2] = 1;
  2010. pkt->hdr->data[data_len + 3] = 0xd;
  2011. pkt->hdr->data[data_len + 4] = 0;
  2012. pkt->hdr->data[data_len + 5] = (pkt->data_size >> 16) & 0xff;
  2013. pkt->hdr->data[data_len + 6] = 0x88;
  2014. pkt->hdr->data[data_len + 7] = (pkt->data_size >> 8) & 0xff;
  2015. pkt->hdr->data[data_len + 8] = pkt->data_size & 0xff;
  2016. pkt->hdr->data[data_len + 9] = 0x88;
  2017. pkt->hdr->data[data_len + 10] = 0xff;
  2018. pkt->hdr->data[data_len + 11] = 0xff;
  2019. pkt->hdr->data[data_len + 12] = 0x88;
  2020. pkt->hdr->data[data_len + 13] = 0xff;
  2021. pkt->hdr->data[data_len + 14] = 0xff;
  2022. pkt->hdr->data[data_len + 15] = 0x88;
  2023. for (i = data_len + 4 ; i < data_len + 16 ; i++) {
  2024. check_sum += pkt->hdr->data[i];
  2025. }
  2026. pkt->hdr->data[data_len + 16] = (check_sum >> 8) & 0xff;
  2027. pkt->hdr->data[data_len + 17] = check_sum & 0xff;
  2028. pkt->hdr->data[data_len + 18] = 0x88;
  2029. pkt->hdr->data[data_len + 19] = (check_sum >> 8) & 0xff;
  2030. pkt->hdr->data[data_len + 20] = check_sum & 0xff;
  2031. pkt->hdr->data[data_len + 21] = 0x88;
  2032. pkt->hdr->size = data_len + 22;
  2033. pkt->avpkt_newflag = 1;
  2034. } else if (para->vstream_info.video_codec_type == VIDEO_DEC_FORMAT_WVC1) {
  2035. if ((pkt->hdr != NULL) && (pkt->hdr->data != NULL)) {
  2036. FREE(pkt->hdr->data);
  2037. pkt->hdr->data = NULL;
  2038. }
  2039. if (pkt->hdr == NULL) {
  2040. pkt->hdr = MALLOC(sizeof(hdr_buf_t));
  2041. if (!pkt->hdr) {
  2042. log_print("[wvc1_prefix] NOMEM!");
  2043. return PLAYER_FAILED;
  2044. }
  2045. pkt->hdr->data = NULL;
  2046. pkt->hdr->size = 0;
  2047. }
  2048. pkt->hdr->data = MALLOC(4);
  2049. if (pkt->hdr->data == NULL) {
  2050. log_print("[wvc1_prefix] NOMEM!");
  2051. return PLAYER_FAILED;
  2052. }
  2053. pkt->hdr->data[0] = 0;
  2054. pkt->hdr->data[1] = 0;
  2055. pkt->hdr->data[2] = 1;
  2056. pkt->hdr->data[3] = 0xd;
  2057. pkt->hdr->size = 4;
  2058. pkt->avpkt_newflag = 1;
  2059. }
  2060. } else if (para->vstream_info.video_format == VFORMAT_MJPEG) {
  2061. if ((para->file_type != MP4_FILE) && (para->file_type != MOV_FILE)) {
  2062. return PLAYER_SUCCESS;
  2063. }
  2064. /* MJPEG video in MP4 container, assume each sample is
  2065. * a frame start, use dual SOI image header to get better
  2066. * error correction.
  2067. * MJPEG decoder driver inside kernel must have corresponding
  2068. * process to dual head.
  2069. */
  2070. if ((pkt->hdr != NULL) && (pkt->hdr->data != NULL)) {
  2071. FREE(pkt->hdr->data);
  2072. pkt->hdr->data = NULL;
  2073. }
  2074. if (pkt->hdr == NULL) {
  2075. pkt->hdr = MALLOC(sizeof(hdr_buf_t));
  2076. if (!pkt->hdr) {
  2077. log_print("[mjpeg_prefix] NOMEM!");
  2078. return PLAYER_FAILED;
  2079. }
  2080. pkt->hdr->data = NULL;
  2081. pkt->hdr->size = 0;
  2082. }
  2083. pkt->hdr->data = MALLOC(2);
  2084. if (pkt->hdr->data == NULL) {
  2085. log_print("[mjpeg_prefix] NOMEM!");
  2086. return PLAYER_FAILED;
  2087. }
  2088. pkt->hdr->data[0] = 0xff;
  2089. pkt->hdr->data[1] = 0xd8;
  2090. pkt->hdr->size = 2;
  2091. pkt->avpkt_newflag = 1;
  2092. }
  2093. } else if (pkt->type == CODEC_AUDIO) {
  2094. if ((!para->playctrl_info.raw_mode) &&
  2095. para->file_type != MPEG_FILE &&/*if mpeg file used softdemux,have adts header before*/
  2096. (para->astream_info.audio_format == AFORMAT_AAC || para->astream_info.audio_format == AFORMAT_AAC_LATM)) {
  2097. if (pkt->hdr == NULL) {
  2098. pkt->hdr = MALLOC(sizeof(hdr_buf_t));
  2099. memset(pkt->hdr, 0, sizeof(hdr_buf_t));
  2100. if (!pkt->hdr) {
  2101. return PLAYER_NOMEM;
  2102. }
  2103. pkt->hdr->data = (char *)MALLOC(ADTS_HEADER_SIZE);
  2104. if (!pkt->hdr->data) {
  2105. return PLAYER_NOMEM;
  2106. }
  2107. }
  2108. adts_add_header(para);
  2109. }
  2110. if (((!para->playctrl_info.raw_mode) &&
  2111. (para->astream_info.audio_format == AFORMAT_ALAC)) ||
  2112. ((!para->playctrl_info.raw_mode) &&
  2113. (para->astream_info.audio_format == AFORMAT_ADPCM) &&
  2114. (!para->acodec->audio_info.block_align) &&
  2115. ((para->acodec->audio_info.codec_id == CODEC_ID_ADPCM_IMA_WAV) ||
  2116. (para->acodec->audio_info.codec_id == CODEC_ID_ADPCM_MS)))) {
  2117. if ((pkt->hdr != NULL) && (pkt->hdr->data != NULL)) {
  2118. FREE(pkt->hdr->data);
  2119. pkt->hdr->data = NULL;
  2120. }
  2121. if (pkt->hdr == NULL) {
  2122. pkt->hdr = MALLOC(sizeof(hdr_buf_t));
  2123. memset(pkt->hdr, 0, sizeof(hdr_buf_t));
  2124. if (!pkt->hdr) {
  2125. return PLAYER_NOMEM;
  2126. }
  2127. pkt->hdr->data = NULL;
  2128. pkt->hdr->size = 0;
  2129. }
  2130. if (!pkt->hdr->data) {
  2131. pkt->hdr->data = (char *)MALLOC(6);
  2132. if (!pkt->hdr->data) {
  2133. return PLAYER_NOMEM;
  2134. }
  2135. }
  2136. pkt->hdr->data[0] = 0x11;
  2137. pkt->hdr->data[1] = 0x22;
  2138. pkt->hdr->data[2] = 0x33;
  2139. pkt->hdr->data[3] = 0x44;
  2140. pkt->hdr->data[4] = (pkt->data_size >> 8) & 0xff;
  2141. pkt->hdr->data[5] = (pkt->data_size) & 0xff;
  2142. pkt->hdr->size = 6;
  2143. }
  2144. // add the frame head
  2145. if ((!para->playctrl_info.raw_mode) && (para->astream_info.audio_format == AFORMAT_APE)) {
  2146. if ((pkt->hdr != NULL) && (pkt->hdr->data != NULL)) {
  2147. FREE(pkt->hdr->data);
  2148. pkt->hdr->data = NULL;
  2149. }
  2150. if (pkt->hdr == NULL) {
  2151. pkt->hdr = MALLOC(sizeof(hdr_buf_t));
  2152. if (!pkt->hdr) {
  2153. return PLAYER_NOMEM;
  2154. }
  2155. pkt->hdr->data = NULL;
  2156. pkt->hdr->size = 0;
  2157. }
  2158. if (!pkt->hdr->data) {
  2159. pkt->hdr->data = (char *)MALLOC(8);
  2160. if (!pkt->hdr->data) {
  2161. return PLAYER_NOMEM;
  2162. }
  2163. }
  2164. int extra_data = 8;
  2165. pkt->hdr->data[0] = 'A';
  2166. pkt->hdr->data[1] = 'P';
  2167. pkt->hdr->data[2] = 'T';
  2168. pkt->hdr->data[3] = 'S';
  2169. pkt->hdr->data[4] = (pkt->data_size - extra_data) & 0xff;
  2170. pkt->hdr->data[5] = (pkt->data_size - extra_data >> 8) & 0xff;
  2171. pkt->hdr->data[6] = (pkt->data_size - extra_data >> 16) & 0xff;
  2172. pkt->hdr->data[7] = (pkt->data_size - extra_data >> 24) & 0xff;
  2173. pkt->hdr->size = 8;
  2174. }
  2175. }
  2176. }
  2177. return PLAYER_SUCCESS;
  2178. }
  2179. void av_packet_release(am_packet_t *pkt)
  2180. {
  2181. if (pkt->avpkt_isvalid) {
  2182. av_free_packet(pkt->avpkt);
  2183. pkt->avpkt_isvalid = 0;
  2184. }
  2185. if (pkt->buf != NULL) {
  2186. FREE(pkt->buf);
  2187. pkt->buf = NULL;
  2188. }
  2189. if (pkt->hdr != NULL) {
  2190. FREE(pkt->hdr->data);
  2191. pkt->hdr->data = NULL;
  2192. FREE(pkt->hdr);
  2193. pkt->hdr = NULL;
  2194. }
  2195. if (pkt->bak_avpkt.data != NULL) {
  2196. FREE(pkt->bak_avpkt.data);
  2197. pkt->bak_avpkt.data = NULL;
  2198. }
  2199. if (pkt->bak_spkt.data != NULL) {
  2200. FREE(pkt->bak_spkt.data);
  2201. pkt->bak_spkt.data = NULL;
  2202. }
  2203. pkt->codec = NULL;
  2204. }
  2205. int poll_sub(am_packet_t *pkt)
  2206. {
  2207. if (pkt->codec) {
  2208. return codec_poll_sub(pkt->codec);
  2209. } else {
  2210. return 0;
  2211. }
  2212. }
  2213. int get_sub_size(am_packet_t *pkt)
  2214. {
  2215. if (pkt->codec) {
  2216. return codec_get_sub_size(pkt->codec);
  2217. } else {
  2218. return 0;
  2219. }
  2220. }
  2221. int read_sub_data(am_packet_t *pkt, char *buf, unsigned int length)
  2222. {
  2223. if (pkt->codec) {
  2224. return codec_read_sub_data(pkt->codec, buf, length);
  2225. } else {
  2226. return 0;
  2227. }
  2228. }
  2229. int write_sub_data(am_packet_t *pkt, char *buf, unsigned int length)
  2230. {
  2231. int write_bytes, size;
  2232. unsigned int len = 0;
  2233. if (!pkt || !pkt->codec) {
  2234. return 0;
  2235. }
  2236. size = length;
  2237. while (size > 0) {
  2238. write_bytes = codec_write_sub_data(pkt->codec, buf, size);
  2239. if (write_bytes < 0) {
  2240. if (-errno != AVERROR(EAGAIN)) {
  2241. log_print("[%s:%d]write sub data failed!\n", __FUNCTION__, __LINE__);
  2242. return PLAYER_WR_FAILED;
  2243. } else {
  2244. continue;
  2245. }
  2246. } else {
  2247. len += write_bytes;
  2248. if (len == length) {
  2249. break;
  2250. }
  2251. size -= write_bytes;
  2252. }
  2253. }
  2254. return PLAYER_SUCCESS;
  2255. }
  2256. int process_es_subtitle(play_para_t *para)
  2257. {
  2258. AVStream *pstream;
  2259. AVFormatContext *pFCtx = para->pFormatCtx;
  2260. am_packet_t *pkt = para->p_pkt;
  2261. unsigned char sub_header[20] = {0x41, 0x4d, 0x4c, 0x55, 0xaa, 0};
  2262. unsigned int sub_type;
  2263. int64_t sub_pts = 0;
  2264. static int last_duration = 0;
  2265. float duration = para->sstream_info.sub_pts;
  2266. long long start_time = para->sstream_info.start_time;
  2267. int data_size = pkt->avpkt->size;
  2268. int i;
  2269. /* find stream for new id */
  2270. for (i = 0; i < pFCtx->nb_streams; i++) {
  2271. pstream = pFCtx->streams[i];
  2272. //if ((unsigned int)pstream->id == pkt->avpkt->stream_index) {
  2273. if (i == pkt->avpkt->stream_index) {
  2274. break;
  2275. }
  2276. }
  2277. if (i == pFCtx->nb_streams) {
  2278. log_print("[%s:%d]no stream found for new sid\n", __FUNCTION__, __LINE__);
  2279. //return;
  2280. }
  2281. /* get pkt pts */
  2282. if ((int64_t)INT64_0 != pkt->avpkt->pts) {
  2283. sub_pts = pkt->avpkt->pts * duration;
  2284. if (sub_pts < start_time) {
  2285. sub_pts = sub_pts * last_duration;
  2286. }
  2287. } else if ((int64_t)INT64_0 != pkt->avpkt->dts) {
  2288. sub_pts = pkt->avpkt->dts * duration * last_duration;
  2289. last_duration = pkt->avpkt->duration;
  2290. } else {
  2291. if (!para->sstream_info.check_first_pts) {
  2292. sub_pts = 0;
  2293. }
  2294. }
  2295. if (!para->sstream_info.check_first_pts) {
  2296. para->sstream_info.check_first_pts = 1;
  2297. }
  2298. /* first write the header */
  2299. //sub_type = para->sstream_info.sub_type;
  2300. sub_type = pstream->codec->codec_id;
  2301. if (sub_type == 0x17000) {
  2302. sub_type = 0x1700a;
  2303. }
  2304. if (sub_type == 0x17002) {
  2305. last_duration = (unsigned)pkt->avpkt->convergence_duration * 90;
  2306. }
  2307. sub_header[5] = (sub_type >> 16) & 0xff;
  2308. sub_header[6] = (sub_type >> 8) & 0xff;
  2309. sub_header[7] = sub_type & 0xff;
  2310. sub_header[8] = (data_size >> 24) & 0xff;
  2311. sub_header[9] = (data_size >> 16) & 0xff;
  2312. sub_header[10] = (data_size >> 8) & 0xff;
  2313. sub_header[11] = data_size & 0xff;
  2314. sub_header[12] = (sub_pts >> 24) & 0xff;
  2315. sub_header[13] = (sub_pts >> 16) & 0xff;
  2316. sub_header[14] = (sub_pts >> 8) & 0xff;
  2317. sub_header[15] = sub_pts & 0xff;
  2318. sub_header[16] = (last_duration >> 24) & 0xff;
  2319. sub_header[17] = (last_duration >> 16) & 0xff;
  2320. sub_header[18] = (last_duration >> 8) & 0xff;
  2321. sub_header[19] = last_duration & 0xff;
  2322. //log_print("## [ sub_type:0x%x, data_size:%d, sub_pts:%lld last_duration %d]\n", sub_type , data_size, sub_pts, last_duration);
  2323. //log_print("## [ sizeof:%d , sub_index=%d, pkt_stream_index=%d,]\n", sizeof(sub_header), para->sstream_info.sub_index, pkt->avpkt->stream_index);
  2324. if (para->sstream_info.sub_index == pkt->avpkt->stream_index) {
  2325. if (write_sub_data(pkt, (char *)&sub_header, sizeof(sub_header))) {
  2326. log_print("[%s:%d]write sub header failed\n", __FUNCTION__, __LINE__);
  2327. }
  2328. }
  2329. for (i = 0; i < 8; i++) {
  2330. //subid should be equal to stream->id
  2331. //if (pkt->avpkt->stream_index == es_sub_buf[i].subid) {
  2332. if (pFCtx->streams[pkt->avpkt->stream_index]->id == es_sub_buf[i].subid) {
  2333. write_es_sub_all(i, (char *)sub_header, sizeof(sub_header));
  2334. #if 0
  2335. log_print("[%s:%d]i = %d, pkt->avpkt->stream_index = %d, sub_type=%d, size=%d, sub_id=%d, sub_type=%d,--------\n", __FUNCTION__, __LINE__, i, pkt->avpkt->stream_index, sub_type, data_size, pkt->codec->sub_pid, pstream->codec->codec_id);
  2336. log_print("## write_sub_header: %x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x ,%x %x ,%x ,%x-----------\n",
  2337. sub_header[0], sub_header[1], sub_header[2], sub_header[3],
  2338. sub_header[4], sub_header[5], sub_header[6], sub_header[7],
  2339. sub_header[8], sub_header[9], sub_header[10], sub_header[11],
  2340. sub_header[12], sub_header[13], sub_header[14], sub_header[15],
  2341. sub_header[16], sub_header[17], sub_header[18], sub_header[19]
  2342. ;
  2343. #endif
  2344. break;
  2345. }
  2346. }
  2347. return PLAYER_SUCCESS;
  2348. }
  2349. int poll_cntl(am_packet_t *pkt)
  2350. {
  2351. if (pkt->codec) {
  2352. return codec_poll_cntl(pkt->codec);
  2353. } else {
  2354. return 0;
  2355. }
  2356. }
  2357. int get_cntl_state(am_packet_t *pkt)
  2358. {
  2359. if (pkt->codec) {
  2360. return codec_get_cntl_state(pkt->codec);
  2361. } else {
  2362. return 0;
  2363. }
  2364. }
  2365. int set_cntl_mode(play_para_t *para, unsigned int mode)
  2366. {
  2367. if (para->vstream_info.has_video == 0) {
  2368. return 0;
  2369. }
  2370. if (para->vcodec) {
  2371. return codec_set_cntl_mode(para->vcodec, mode);
  2372. } else if (para->codec) {
  2373. return codec_set_cntl_mode(para->codec, mode);
  2374. }
  2375. return 0;
  2376. }
  2377. int set_cntl_avthresh(play_para_t *para, unsigned int avthresh)
  2378. {
  2379. if (para->vstream_info.has_video == 0) {
  2380. return 0;
  2381. }
  2382. if (para->vcodec) {
  2383. return codec_set_cntl_avthresh(para->vcodec, avthresh);
  2384. } else {
  2385. return codec_set_cntl_avthresh(para->codec, avthresh);
  2386. }
  2387. }
  2388. int set_cntl_syncthresh(play_para_t *para)
  2389. {
  2390. if (para->vstream_info.has_video == 0) {
  2391. return 0;
  2392. }
  2393. if (para->vcodec) {
  2394. return codec_set_cntl_syncthresh(para->vcodec, para->astream_info.has_audio);
  2395. } else {
  2396. return codec_set_cntl_syncthresh(para->codec, para->astream_info.has_audio);
  2397. }
  2398. }
  2399. void player_switch_audio(play_para_t *para)
  2400. {
  2401. codec_para_t *pcodec;
  2402. AVStream *pstream;
  2403. unsigned int i;
  2404. short audio_index;
  2405. AVCodecContext *pCodecCtx;
  2406. AVFormatContext *pFCtx = para->pFormatCtx;
  2407. int ret = -1;
  2408. /* find stream for new id */
  2409. for (i = 0; i < pFCtx->nb_streams; i++) {
  2410. pstream = pFCtx->streams[i];
  2411. if (pstream->codec->codec_type == CODEC_TYPE_AUDIO &&
  2412. (unsigned int)pstream->id == para->playctrl_info.switch_audio_id) {
  2413. break;
  2414. }
  2415. }
  2416. if (i == pFCtx->nb_streams) {
  2417. log_print("[%s:%d]no stream found for new aid\n", __FUNCTION__, __LINE__);
  2418. return;
  2419. }
  2420. /* get new information */
  2421. audio_index = pstream->index;
  2422. log_print("[%s:%d]audio_index %d, i %d\n", __FUNCTION__, __LINE__, audio_index, i);
  2423. if (audio_index == -1) {
  2424. log_print("[%s:%d]no index found\n", __FUNCTION__, __LINE__);
  2425. return;
  2426. } else {
  2427. pCodecCtx = pFCtx->streams[audio_index]->codec;
  2428. }
  2429. para->astream_info.audio_format = audio_type_convert(pCodecCtx->codec_id, para->file_type);
  2430. if (pFCtx->drmcontent) {
  2431. log_print("[%s:%d]DRM content found, not support yet.\n", __FUNCTION__, __LINE__);
  2432. para->astream_info.audio_format = VFORMAT_UNSUPPORT;
  2433. }
  2434. if (para->astream_info.audio_format < 0 || para->astream_info.audio_format >= AFORMAT_MAX) {
  2435. log_error("[%s:%d]unkown audio format\n", __FUNCTION__, __LINE__);
  2436. para->astream_info.has_audio = 0;
  2437. set_player_error_no(para, PLAYER_NO_AUDIO);
  2438. update_player_states(para, 1);
  2439. return;
  2440. } else if (para->astream_info.audio_format == AFORMAT_UNSUPPORT) {
  2441. log_error("[%s:%d]unsupport audio format\n", __FUNCTION__, __LINE__);
  2442. para->astream_info.has_audio = 0;
  2443. set_player_error_no(para, PLAYER_UNSUPPORT_AUDIO);
  2444. update_player_states(para, 1);
  2445. return;
  2446. }
  2447. /* check if it has audio */
  2448. if (para->astream_info.has_audio == 0) {
  2449. return;
  2450. }
  2451. if (0 != pstream->time_base.den) {
  2452. para->astream_info.audio_duration = PTS_FREQ * ((float)pstream->time_base.num / pstream->time_base.den);
  2453. para->astream_info.start_time = pstream->start_time * pstream->time_base.num * PTS_FREQ / pstream->time_base.den;
  2454. }
  2455. para->astream_info.audio_channel = pCodecCtx->channels;
  2456. para->astream_info.audio_samplerate = pCodecCtx->sample_rate;
  2457. para->astream_info.audio_index = audio_index;
  2458. para->media_info.stream_info.cur_audio_index = audio_index;
  2459. para->astream_info.audio_pid = pstream->id;
  2460. if (!para->playctrl_info.raw_mode
  2461. && para->astream_info.audio_format == AFORMAT_AAC) {
  2462. ret = extract_adts_header_info(para);
  2463. if (ret != PLAYER_SUCCESS) {
  2464. log_error("[%s:%d]extract adts header failed! ret=0x%x\n", __FUNCTION__, __LINE__, -ret);
  2465. return;
  2466. }
  2467. }
  2468. if (para->playctrl_info.read_end_flag) {
  2469. para->playctrl_info.reset_flag = 1;
  2470. para->playctrl_info.end_flag = 1;
  2471. para->playctrl_info.time_point = para->state.current_time;
  2472. log_print("[%s]read end, reset decoder for switch!curtime=%d\n", __FUNCTION__, para->playctrl_info.time_point);
  2473. return ;
  2474. }
  2475. /*
  2476. if (para->playctrl_info.raw_mode
  2477. && para->astream_info.audio_format == AFORMAT_PCM_BLURAY) {
  2478. para->playctrl_info.reset_flag = 1;
  2479. para->playctrl_info.end_flag = 1;
  2480. para->playctrl_info.time_point = para->state.current_time;
  2481. return;
  2482. }
  2483. */
  2484. if (para->acodec) {
  2485. pcodec = para->acodec;
  2486. } else {
  2487. pcodec = para->codec;
  2488. }
  2489. /* automute */
  2490. codec_audio_automute(pcodec->adec_priv, 1);
  2491. /* close audio */
  2492. codec_close_audio(pcodec);
  2493. /* first set an invalid audio id */
  2494. pcodec->audio_pid = 0xffff;
  2495. //para->astream_info.audio_index = -1;
  2496. if (codec_set_audio_pid(pcodec)) {
  2497. log_print("[%s:%d]set invalid audio pid failed\n", __FUNCTION__, __LINE__);
  2498. return;
  2499. }
  2500. /* reinit audio info */
  2501. pcodec->has_audio = 1;
  2502. pcodec->audio_type = para->astream_info.audio_format;
  2503. pcodec->audio_pid = pstream->id;
  2504. pcodec->audio_channels = para->astream_info.audio_channel;
  2505. pcodec->audio_samplerate = para->astream_info.audio_samplerate;
  2506. pcodec->switch_audio_flag = 1;
  2507. /*if ((pcodec->audio_type == AFORMAT_ADPCM) || (pcodec->audio_type == AFORMAT_WMA)
  2508. || (pcodec->audio_type == AFORMAT_WMAPRO) || (pcodec->audio_type == AFORMAT_PCM_S16BE)
  2509. || (pcodec->audio_type == AFORMAT_PCM_S16LE) || (pcodec->audio_type == AFORMAT_PCM_U8)
  2510. || (pcodec->audio_type == AFORMAT_PCM_BLURAY)||(pcodec->audio_type == AFORMAT_AMR)) {*/
  2511. if (IS_AUIDO_NEED_EXT_INFO(pcodec->audio_type)) {
  2512. pcodec->audio_info.bitrate = pCodecCtx->sample_fmt;
  2513. pcodec->audio_info.sample_rate = pCodecCtx->sample_rate;
  2514. pcodec->audio_info.channels = pCodecCtx->channels;
  2515. pcodec->audio_info.codec_id = pCodecCtx->codec_id;
  2516. pcodec->audio_info.block_align = pCodecCtx->block_align;
  2517. pcodec->audio_info.extradata_size = pCodecCtx->extradata_size;
  2518. if (pcodec->audio_info.extradata_size > 0) {
  2519. if (pcodec->audio_info.extradata_size > AUDIO_EXTRA_DATA_SIZE) {
  2520. log_print("[%s:%d],extra data size exceed max extra data buffer,cut it to max buffer size ", __FUNCTION__, __LINE__);
  2521. pcodec->audio_info.extradata_size = AUDIO_EXTRA_DATA_SIZE;
  2522. }
  2523. MEMCPY((char*)pcodec->audio_info.extradata, pCodecCtx->extradata, pcodec->audio_info.extradata_size);
  2524. }
  2525. pcodec->audio_info.valid = 1;
  2526. log_print("[%s]fmt=%d srate=%d chanels=%d extrasize=%d\n", __FUNCTION__, pcodec->audio_type, \
  2527. pcodec->audio_info.sample_rate, pcodec->audio_info.channels, pcodec->audio_info.extradata_size);
  2528. } else {
  2529. pcodec->audio_info.valid = 0;
  2530. }
  2531. if (codec_audio_reinit(pcodec)) {
  2532. log_print("[%s:%d]audio reinit failed\n", __FUNCTION__, __LINE__);
  2533. return;
  2534. }
  2535. /* reset audio */
  2536. if (codec_reset_audio(pcodec)) {
  2537. log_print("[%s:%d]reset audio failed\n", __FUNCTION__, __LINE__);
  2538. return;
  2539. }
  2540. /* backup next video packet and time search if it is ES */
  2541. if (para->stream_type == STREAM_ES && para->vstream_info.has_video) {
  2542. AVPacket *avPkt = para->p_pkt->avpkt;
  2543. int end_flag = para->playctrl_info.read_end_flag;
  2544. log_print("[%s:%d]vidx=%d sidx=%d\n", __FUNCTION__, __LINE__, para->vstream_info.video_index, para->sstream_info.sub_index);
  2545. para->playctrl_info.audio_switch_vmatch = 0;
  2546. para->playctrl_info.audio_switch_smatch = 0;
  2547. /* find the next video packet and save it */
  2548. while (!para->playctrl_info.read_end_flag) {
  2549. ret = av_read_frame(para->pFormatCtx, avPkt);
  2550. log_print("[%s:%d]av_read_frame return (%d) idx=%d, vmatch %d, smatch %d\n",
  2551. __FUNCTION__, __LINE__, ret, avPkt->stream_index,
  2552. para->playctrl_info.audio_switch_vmatch, para->playctrl_info.audio_switch_smatch);
  2553. if (ret < 0) {
  2554. if (AVERROR(EAGAIN) != ret) {
  2555. /*if the return is EAGAIN,we need to try more times*/
  2556. log_error("[%s:%d]av_read_frame return (%d)\n", __FUNCTION__, __LINE__, ret);
  2557. if (AVERROR_EOF != ret) {
  2558. return;
  2559. } else {
  2560. para->playctrl_info.read_end_flag = 1;
  2561. log_print("player_switch_audio: read end!\n");
  2562. break;
  2563. }
  2564. } else {
  2565. continue;
  2566. }
  2567. } else { //read success
  2568. if (avPkt->size >= MAX_PACKET_SIZE) {
  2569. log_print("non_raw_read error:packet size exceed malloc memory! size %d\n", avPkt->size);
  2570. return;
  2571. }
  2572. if ((!para->playctrl_info.audio_switch_vmatch)
  2573. && (avPkt->stream_index == para->vstream_info.video_index)) {
  2574. /* back up this packet */
  2575. AVPacket *bakpkt = &para->p_pkt->bak_avpkt;
  2576. if (backup_packet(para, avPkt, bakpkt) == 0) {
  2577. av_free_packet(avPkt);
  2578. para->playctrl_info.audio_switch_vmatch = 1;
  2579. #if 0
  2580. if (para->sstream_info.has_sub && (!para->playctrl_info.audio_switch_smatch)) {
  2581. log_print("[%s:%d]Backup video, to backup sub\n", __FUNCTION__, __LINE__);
  2582. continue;
  2583. } else {
  2584. break;
  2585. }
  2586. #endif
  2587. break;
  2588. } else {
  2589. av_free_packet(avPkt);
  2590. return;
  2591. }
  2592. } /*else if ((para->sstream_info.has_sub)
  2593. && (!para->playctrl_info.audio_switch_smatch)
  2594. && (avPkt->stream_index == para->sstream_info.sub_index)){
  2595. AVPacket *bakpkt = &para->p_pkt->bak_spkt;
  2596. if (backup_packet(para, avPkt, bakpkt) == 0) {
  2597. av_free_packet(avPkt);
  2598. para->playctrl_info.audio_switch_smatch = 1;
  2599. if (!para->playctrl_info.audio_switch_vmatch) {
  2600. log_print("[%s:%d]Backup sub, to backup video\n", __FUNCTION__, __LINE__);
  2601. continue;
  2602. } else {
  2603. break;
  2604. }
  2605. } else {
  2606. av_free_packet(avPkt);
  2607. return;
  2608. }
  2609. }*/ else {
  2610. av_free_packet(avPkt);
  2611. continue;
  2612. }
  2613. }
  2614. }
  2615. log_print("[%s:%d]finish bakup packet,do seek\n", __FUNCTION__, __LINE__);
  2616. /* time search based on audio */
  2617. para->playctrl_info.time_point = para->state.current_time;
  2618. ret = time_search(para);
  2619. if (ret != PLAYER_SUCCESS) {
  2620. log_error("[%s:%d]time_search to pos:%ds failed!", __FUNCTION__, __LINE__, para->playctrl_info.time_point);
  2621. }
  2622. para->playctrl_info.read_end_flag = end_flag;
  2623. }
  2624. if (IS_AUIDO_NEED_PREFEED_HEADER(pcodec->audio_type)) {
  2625. pre_header_feeding(para);
  2626. }
  2627. /* resume audio */
  2628. codec_resume_audio(pcodec, para->astream_info.resume_audio);
  2629. /* unmute*/
  2630. codec_audio_automute(pcodec->adec_priv, 0);
  2631. //for ts case, when switch aid , reset audio&video decoder
  2632. //controled by property "media.ts.switchaid.policy"
  2633. if (para->stream_type == STREAM_TS && para->vstream_info.has_video) {
  2634. int ret;
  2635. char value[PROPERTY_VALUE_MAX];
  2636. #ifdef ANDROID
  2637. ret = property_get("media.ts.switchaid.policy", value, NULL);
  2638. if (ret > 0 && match_types("reset", value)) {
  2639. log_print("media.ts.switchaid.policy = %s\n", value);
  2640. set_player_state(para, PLAYER_INITING);
  2641. para->playctrl_info.time_point = para->state.current_time;
  2642. player_dec_reset(para);
  2643. set_player_state(para, PLAYER_RUNNING);
  2644. }
  2645. #endif
  2646. }
  2647. return;
  2648. }
  2649. static int get_cur_sub(int id)
  2650. {
  2651. int index = 0;
  2652. int size = 0;
  2653. int i = 0;
  2654. for (index = 0; index < 8; index++) {
  2655. if (id == es_sub_buf[index].subid) {
  2656. break;
  2657. }
  2658. }
  2659. size = es_sub_buf[index].size;
  2660. es_sub_buf[8].size = 0;
  2661. es_sub_buf[8].rdp = 0;
  2662. //log_print("[%s:%d] id = %d, index = %d\n", __FUNCTION__, __LINE__, id, index);
  2663. //log_print("[%s:%d] es_sub_buf[index].rdp = %d, es_sub_buf[index].wrp = %d, es_sub_buf[index].size = %d\n", __FUNCTION__, __LINE__,es_sub_buf[index].rdp, es_sub_buf[index].wrp, es_sub_buf[index].size);
  2664. if (es_sub_buf[index].rdp < es_sub_buf[index].wrp) {
  2665. memcpy(es_sub_buf[8].sub_buf, es_sub_buf[index].sub_buf + es_sub_buf[index].rdp, es_sub_buf[index].size);
  2666. } else {
  2667. int part_size = SUBTITLE_SIZE - es_sub_buf[index].rdp;
  2668. memcpy(es_sub_buf[8].sub_buf, es_sub_buf[index].sub_buf + es_sub_buf[index].rdp, part_size);
  2669. memcpy(es_sub_buf[8].sub_buf + part_size, es_sub_buf[index].sub_buf, es_sub_buf[index].wrp);
  2670. }
  2671. while (i < size) {
  2672. if ((sub_buf[8][i] == 0x41) && (sub_buf[8][i+1] == 0x4d) && (sub_buf[8][i+2] == 0x4c) && (sub_buf[8][i+3] == 0x55) && (sub_buf[8][i+4] == 0xaa)) {
  2673. es_sub_buf[8].rdp = i;
  2674. es_sub_buf[8].size = size - i;
  2675. break;
  2676. }
  2677. i++;
  2678. }
  2679. //log_print("[%s:%d] es_sub_buf[8].rdp = %d, es_sub_buf[8].wrp = %d, es_sub_buf[8].size = %d \n", __FUNCTION__, __LINE__, es_sub_buf[8].rdp, es_sub_buf[8].wrp, es_sub_buf[8].size);
  2680. return es_sub_buf[8].size;
  2681. }
  2682. void player_switch_sub(play_para_t *para)
  2683. {
  2684. codec_para_t *pcodec;
  2685. AVStream *pstream;
  2686. unsigned int i;
  2687. AVFormatContext *pFCtx = para->pFormatCtx;
  2688. int write_size = 0;
  2689. int total_size = 0;
  2690. s_stream_info_t *sinfo = &para->sstream_info;
  2691. /* check if it has audio */
  2692. if (para->sstream_info.has_sub == 0) {
  2693. return;
  2694. }
  2695. /* find stream for new id */
  2696. for (i = 0; i < pFCtx->nb_streams; i++) {
  2697. pstream = pFCtx->streams[i];
  2698. if ((unsigned int)pstream->id == para->playctrl_info.switch_sub_id) {
  2699. break;
  2700. }
  2701. }
  2702. if (i == pFCtx->nb_streams) {
  2703. log_print("[%s:%d]no stream found for new sid\n", __FUNCTION__, __LINE__);
  2704. return;
  2705. }
  2706. if (pstream->codec->codec_id == CODEC_ID_DVD_SUBTITLE) {
  2707. set_subtitle_subtype(0);
  2708. } else if (pstream->codec->codec_id == CODEC_ID_HDMV_PGS_SUBTITLE) {
  2709. set_subtitle_subtype(1);
  2710. } else if (pstream->codec->codec_id == CODEC_ID_XSUB) {
  2711. set_subtitle_subtype(2);
  2712. } else if (pstream->codec->codec_id == CODEC_ID_TEXT || \
  2713. pstream->codec->codec_id == CODEC_ID_SSA) {
  2714. set_subtitle_subtype(3);
  2715. } else {
  2716. set_subtitle_subtype(4);
  2717. }
  2718. /* only ps and ts stream */
  2719. //if (para->codec == NULL)// codec always has value
  2720. if (para->stream_type == STREAM_ES) {
  2721. para->sstream_info.sub_index = i;
  2722. para->sstream_info.sub_pid = (unsigned short)pstream->id;
  2723. para->sstream_info.sub_type = pstream->codec->codec_id;
  2724. if (pstream->time_base.num && (0 != pstream->time_base.den)) {
  2725. para->sstream_info.sub_duration = UNIT_FREQ * ((float)pstream->time_base.num / pstream->time_base.den);
  2726. para->sstream_info.sub_pts = PTS_FREQ * ((float)pstream->time_base.num / pstream->time_base.den);
  2727. para->sstream_info.start_time = pstream->start_time * pstream->time_base.num * PTS_FREQ / pstream->time_base.den;
  2728. } else {
  2729. para->sstream_info.start_time = pstream->start_time * PTS_FREQ;
  2730. }
  2731. if (codec_reset_subtile(para->scodec)) {
  2732. log_print("[%s:%d]reset subtile failed\n", __FUNCTION__, __LINE__);
  2733. }
  2734. write_size = get_cur_sub(pstream->id);
  2735. log_print("[%s:%d]pstream->id = %d, write_size = %d, es_sub_buf[8].size = %d\n", __FUNCTION__, __LINE__, pstream->id, write_size, es_sub_buf[8].size);
  2736. while ((es_sub_buf[8].size - total_size) > 0) {
  2737. log_print("[%s:%d]total_size = %d\n", __FUNCTION__, __LINE__, total_size);
  2738. write_size = codec_write(para->scodec, (char *)&sub_buf[8][0] + es_sub_buf[8].rdp + total_size, es_sub_buf[8].size - total_size);
  2739. if (write_size == -1) {
  2740. log_print("[%s:%d]write error! total_size = %d, write_size = %d\n", __FUNCTION__, __LINE__, total_size, write_size);
  2741. break;
  2742. }
  2743. total_size += write_size;
  2744. }
  2745. log_print("[%s:%d]write finished! total_size = %d, write_size = %d\n", __FUNCTION__, __LINE__, total_size, write_size);
  2746. //set curr for cts
  2747. int index;
  2748. for (index = 0; index < 8; index++) {
  2749. if (pstream->id == es_sub_buf[index].subid) {
  2750. break;
  2751. }
  2752. }
  2753. if(-1==set_subtitle_index(index))
  2754. {
  2755. log_print("set cur subtitle index = %d failed ! \n",index);
  2756. usleep(1000);
  2757. }
  2758. return;
  2759. } else {
  2760. pcodec = para->codec;
  2761. }
  2762. codec_reset_subtile(para->codec);
  2763. /* first set an invalid sub id */
  2764. pcodec->sub_pid = 0xffff;
  2765. if (codec_set_sub_id(pcodec)) {
  2766. log_print("[%s:%d]set invalid sub pid failed\n", __FUNCTION__, __LINE__);
  2767. return;
  2768. }
  2769. /* reset sub */
  2770. pcodec->sub_pid = pstream->id;
  2771. if (codec_set_sub_id(pcodec)) {
  2772. log_print("[%s:%d]set invalid sub pid failed\n", __FUNCTION__, __LINE__);
  2773. return;
  2774. }
  2775. sinfo->sub_pid = pcodec->sub_pid;
  2776. if (codec_reset_subtile(pcodec)) {
  2777. log_print("[%s:%d]reset subtile failed\n", __FUNCTION__, __LINE__);
  2778. }
  2779. return;
  2780. }
  2781. void av_packet_init(am_packet_t *pkt)
  2782. {
  2783. pkt->avpkt = NULL;
  2784. pkt->avpkt_isvalid = 0;
  2785. pkt->avpkt_newflag = 0;
  2786. pkt->codec = NULL;
  2787. pkt->hdr = NULL;
  2788. pkt->buf = NULL;
  2789. pkt->buf_size = 0;
  2790. pkt->data = NULL;
  2791. pkt->data_size = 0;
  2792. MEMSET(&pkt->bak_avpkt, 0, sizeof(AVPacket));
  2793. MEMSET(&pkt->bak_spkt, 0, sizeof(AVPacket));
  2794. }
  2795. static void av_packet_reset(am_packet_t *pkt)
  2796. {
  2797. pkt->avpkt_isvalid = 0;
  2798. pkt->avpkt_newflag = 0;
  2799. pkt->data_size = 0;
  2800. }
  2801. int player_reset(play_para_t *p_para)
  2802. {
  2803. am_packet_t *pkt = p_para->p_pkt;
  2804. int ret = PLAYER_SUCCESS;
  2805. player_para_reset(p_para);
  2806. av_packet_reset(pkt);
  2807. ret = player_dec_reset(p_para);
  2808. return ret;
  2809. }
  2810. void set_tsync_enable_codec(play_para_t *p_para, int enable)
  2811. {
  2812. if (p_para->codec) {
  2813. codec_set_syncenable(p_para->codec, enable);
  2814. } else if (p_para->vcodec) {
  2815. codec_set_syncenable(p_para->vcodec, enable);
  2816. }
  2817. return;
  2818. }
  2819. int check_avbuffer_enough(play_para_t *para)
  2820. {
  2821. #define VIDEO_RESERVED_SPACE (0x10000) // 64k
  2822. #define AUDIO_RESERVED_SPACE (0x2000) // 8k
  2823. am_packet_t *pkt = para->p_pkt;
  2824. int vbuf_enough = 1;
  2825. int abuf_enough = 1;
  2826. int ret = 1;
  2827. float high_limit = (para->buffering_threshhold_max > 0) ? para->buffering_threshhold_max : 0.8;
  2828. if (pkt->type == CODEC_COMPLEX) {
  2829. if (para->vstream_info.has_video &&
  2830. (para->state.video_bufferlevel >= high_limit)) {
  2831. vbuf_enough = 0;
  2832. }
  2833. if (para->astream_info.has_audio &&
  2834. (para->state.audio_bufferlevel >= high_limit)) {
  2835. abuf_enough = 0;
  2836. }
  2837. ret = vbuf_enough && abuf_enough;
  2838. } else if (pkt->type == CODEC_VIDEO || pkt->type == CODEC_AUDIO) {
  2839. /*if(pkt->type == CODEC_VIDEO)
  2840. log_print("[%s]type:%d data=%x size=%x total=%x\n", __FUNCTION__, pkt->type, para->vbuffer.data_level,pkt->data_size,para->vbuffer.buffer_size);
  2841. if(pkt->type == CODEC_AUDIO)
  2842. log_print("[%s]type:%d data=%x size=%x total=%x\n", __FUNCTION__, pkt->type, para->abuffer.data_level,pkt->data_size,para->abuffer.buffer_size);
  2843. */
  2844. if (para->vstream_info.has_video && (pkt->type == CODEC_VIDEO) &&
  2845. ((para->vbuffer.data_level + pkt->data_size) >= (para->vbuffer.buffer_size - VIDEO_RESERVED_SPACE))) {
  2846. vbuf_enough = 0;
  2847. }
  2848. if (para->astream_info.has_audio && (pkt->type == CODEC_AUDIO) &&
  2849. ((para->abuffer.data_level + pkt->data_size) >= (para->abuffer.buffer_size - AUDIO_RESERVED_SPACE))) {
  2850. abuf_enough = 0;
  2851. }
  2852. ret = vbuf_enough && abuf_enough;
  2853. }
  2854. /*if(!abuf_enough || !vbuf_enough) {
  2855. log_print("check_avbuffer_enough abuflevel %f, vbuflevel %f, limit %f aenough=%d venought=%d\n",
  2856. para->state.audio_bufferlevel, para->state.video_bufferlevel, high_limit,abuf_enough,vbuf_enough);
  2857. */
  2858. return ret;
  2859. }
  2860. int check_avbuffer_enough_for_ape(play_para_t *para)
  2861. {
  2862. #define VIDEO_RESERVED_SPACE (0x10000) // 64k
  2863. #define AUDIO_RESERVED_SPACE (0x2000) // 8k
  2864. am_packet_t *pkt = para->p_pkt;
  2865. int vbuf_enough = 1;
  2866. int abuf_enough = 1;
  2867. int ret = 0;
  2868. float high_limit = 0.8;
  2869. int nCurrentWriteCount = (pkt->data_size > AUDIO_WRITE_SIZE_PER_TIME) ? AUDIO_WRITE_SIZE_PER_TIME : pkt->data_size;
  2870. if (pkt->type == CODEC_AUDIO) {
  2871. /*
  2872. if(pkt->type == CODEC_AUDIO)
  2873. log_print("[%s]type:%d data=%x size=%x total=%x\n", __FUNCTION__, pkt->type, para->abuffer.data_level,nCurrentWriteCount,para->abuffer.buffer_size);
  2874. */
  2875. if (para->astream_info.has_audio && (pkt->type == CODEC_AUDIO) &&
  2876. ((para->abuffer.data_level + nCurrentWriteCount) >= (para->abuffer.buffer_size - AUDIO_RESERVED_SPACE))) {
  2877. abuf_enough = 0;
  2878. }
  2879. ret = vbuf_enough && abuf_enough;
  2880. }
  2881. else{
  2882. if((float)(para->abuffer.data_level/para->abuffer.buffer_size)>high_limit && para->astream_info.has_audio ){
  2883. abuf_enough=0;
  2884. }else if((float)(para->vbuffer.data_level/para->vbuffer.buffer_size)>high_limit && para->vstream_info.has_video){
  2885. vbuf_enough=0;
  2886. }
  2887. ret = vbuf_enough && abuf_enough;
  2888. }
  2889. /*if(!abuf_enough || !vbuf_enough) {
  2890. log_print("check_avbuffer_enough abuflevel %f, vbuflevel %f, limit %f aenough=%d venought=%d\n",
  2891. para->state.audio_bufferlevel, para->state.video_bufferlevel, high_limit,abuf_enough,vbuf_enough);
  2892. */
  2893. return ret;
  2894. }