PageRenderTime 62ms CodeModel.GetById 20ms 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

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

  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);

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