forked from xbmc/xbmc
/
AMLCodec.cpp
2312 lines (2044 loc) · 73.9 KB
/
AMLCodec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "system.h"
#include "AMLCodec.h"
#include "DynamicDll.h"
#include "cores/VideoPlayer/DVDClock.h"
#include "cores/VideoPlayer/VideoRenderers/RenderFlags.h"
#include "cores/VideoPlayer/VideoRenderers/RenderManager.h"
#include "guilib/GraphicContext.h"
#include "settings/DisplaySettings.h"
#include "settings/MediaSettings.h"
#include "settings/Settings.h"
#include "utils/AMLUtils.h"
#include "utils/log.h"
#include "utils/StringUtils.h"
#include "utils/SysfsUtils.h"
#include "utils/TimeUtils.h"
#if defined(TARGET_ANDROID)
#include "platform/android/activity/AndroidFeatures.h"
#include "utils/BitstreamConverter.h"
#endif
extern "C" {
#include "libavutil/avutil.h"
} // extern "C"
#include <unistd.h>
#include <queue>
#include <vector>
#include <signal.h>
#include <semaphore.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
// amcodec include
extern "C" {
#include <amcodec/codec.h>
} // extern "C"
class PosixFile
{
public:
PosixFile() :
m_fd(-1)
{
}
PosixFile(int fd) :
m_fd(fd)
{
}
~PosixFile()
{
if (m_fd >= 0)
close(m_fd);
}
bool Open(const std::string &pathName, int flags)
{
m_fd = open(pathName.c_str(), flags);
return m_fd >= 0;
}
int GetDescriptor() const { return m_fd; }
int IOControl(unsigned long request, void *param)
{
return ioctl(m_fd, request, param);
}
private:
int m_fd;
};
typedef struct {
bool noblock;
int video_pid;
int video_type;
stream_type_t stream_type;
unsigned int format;
unsigned int width;
unsigned int height;
unsigned int rate;
unsigned int extra;
unsigned int status;
unsigned int ratio;
unsigned long long ratio64;
void *param;
} aml_generic_param;
class DllLibamCodecInterface
{
public:
virtual ~DllLibamCodecInterface() {};
virtual int codec_init(codec_para_t *pcodec)=0;
virtual int codec_close(codec_para_t *pcodec)=0;
virtual int codec_reset(codec_para_t *pcodec)=0;
virtual int codec_pause(codec_para_t *pcodec)=0;
virtual int codec_resume(codec_para_t *pcodec)=0;
virtual int codec_write(codec_para_t *pcodec, void *buffer, int len)=0;
virtual int codec_checkin_pts(codec_para_t *pcodec, unsigned long pts)=0;
virtual int codec_get_vbuf_state(codec_para_t *pcodec, struct buf_status *buf)=0;
virtual int codec_get_vdec_state(codec_para_t *pcodec, struct vdec_status *vdec)=0;
virtual int codec_init_cntl(codec_para_t *pcodec)=0;
virtual int codec_poll_cntl(codec_para_t *pcodec)=0;
virtual int codec_set_cntl_mode(codec_para_t *pcodec, unsigned int mode)=0;
virtual int codec_set_cntl_avthresh(codec_para_t *pcodec, unsigned int avthresh)=0;
virtual int codec_set_cntl_syncthresh(codec_para_t *pcodec, unsigned int syncthresh)=0;
};
class DllLibAmCodec : public DllDynamic, DllLibamCodecInterface
{
// libamcodec is static linked into libamplayer.so or libamcodec.so
DECLARE_DLL_WRAPPER(DllLibAmCodec, "libamplayer.so")
DEFINE_METHOD1(int, codec_init, (codec_para_t *p1))
DEFINE_METHOD1(int, codec_close, (codec_para_t *p1))
DEFINE_METHOD1(int, codec_reset, (codec_para_t *p1))
DEFINE_METHOD1(int, codec_pause, (codec_para_t *p1))
DEFINE_METHOD1(int, codec_resume, (codec_para_t *p1))
DEFINE_METHOD3(int, codec_write, (codec_para_t *p1, void *p2, int p3))
DEFINE_METHOD2(int, codec_checkin_pts, (codec_para_t *p1, unsigned long p2))
DEFINE_METHOD2(int, codec_get_vbuf_state, (codec_para_t *p1, struct buf_status * p2))
DEFINE_METHOD2(int, codec_get_vdec_state, (codec_para_t *p1, struct vdec_status * p2))
DEFINE_METHOD1(int, codec_init_cntl, (codec_para_t *p1))
DEFINE_METHOD1(int, codec_poll_cntl, (codec_para_t *p1))
DEFINE_METHOD2(int, codec_set_cntl_mode, (codec_para_t *p1, unsigned int p2))
DEFINE_METHOD2(int, codec_set_cntl_avthresh, (codec_para_t *p1, unsigned int p2))
DEFINE_METHOD2(int, codec_set_cntl_syncthresh,(codec_para_t *p1, unsigned int p2))
BEGIN_METHOD_RESOLVE()
RESOLVE_METHOD(codec_init)
RESOLVE_METHOD(codec_close)
RESOLVE_METHOD(codec_reset)
RESOLVE_METHOD(codec_pause)
RESOLVE_METHOD(codec_resume)
RESOLVE_METHOD(codec_write)
RESOLVE_METHOD(codec_checkin_pts)
RESOLVE_METHOD(codec_get_vbuf_state)
RESOLVE_METHOD(codec_get_vdec_state)
RESOLVE_METHOD(codec_init_cntl)
RESOLVE_METHOD(codec_poll_cntl)
RESOLVE_METHOD(codec_set_cntl_mode)
RESOLVE_METHOD(codec_set_cntl_avthresh)
RESOLVE_METHOD(codec_set_cntl_syncthresh)
END_METHOD_RESOLVE()
public:
void codec_init_para(aml_generic_param *p_in, codec_para_t *p_out)
{
memset(p_out, 0x00, sizeof(codec_para_t));
#ifdef TARGET_ANDROID
bits_writer_t bs = {0};
// we are always as large as codec_para_t from headers.
CBitstreamConverter::init_bits_writer(&bs, (uint8_t*)p_out, sizeof(codec_para_t), 1);
// order matters, so pay attention
// to codec_para_t in codec_types.h
CBitstreamConverter::write_bits(&bs, 32, -1); // CODEC_HANDLE handle, init to invalid
CBitstreamConverter::write_bits(&bs, 32, -1); // CODEC_HANDLE cntl_handle
CBitstreamConverter::write_bits(&bs, 32, -1); // CODEC_HANDLE sub_handle
CBitstreamConverter::write_bits(&bs, 32, -1); // CODEC_HANDLE audio_utils_handle
CBitstreamConverter::write_bits(&bs, 32, p_in->stream_type); // stream_type_t stream_type
// watch these, using bit fields (which is stupid)
CBitstreamConverter::write_bits(&bs, 1, 1); // unsigned int has_video:1
CBitstreamConverter::write_bits(&bs, 1, 0); // unsigned int has_audio:1
CBitstreamConverter::write_bits(&bs, 1, 0); // unsigned int has_sub:1
unsigned int value = p_in->noblock > 0 ? 1:0;
CBitstreamConverter::write_bits(&bs, 1, value); // unsigned int noblock:1
CBitstreamConverter::write_bits(&bs, 28, 0); // align back to next word boundary
CBitstreamConverter::write_bits(&bs, 32, p_in->video_type); // int video_type
CBitstreamConverter::write_bits(&bs, 32, 0); // int audio_type
CBitstreamConverter::write_bits(&bs, 32, 0); // int sub_type
CBitstreamConverter::write_bits(&bs, 32, p_in->video_pid); // int video_pid
CBitstreamConverter::write_bits(&bs, 32, 0); // int audio_pid
CBitstreamConverter::write_bits(&bs, 32, 0); // int sub_pid
CBitstreamConverter::write_bits(&bs, 32, 0); // int audio_channels
CBitstreamConverter::write_bits(&bs, 32, 0); // int audio_samplerate
CBitstreamConverter::write_bits(&bs, 32, 0); // int vbuf_size
CBitstreamConverter::write_bits(&bs, 32, 0); // int abuf_size
CBitstreamConverter::write_bits(&bs, 32, p_in->format); // am_sysinfo, unsigned int format
CBitstreamConverter::write_bits(&bs, 32, p_in->width); // am_sysinfo, unsigned int width
CBitstreamConverter::write_bits(&bs, 32, p_in->height); // am_sysinfo, unsigned int height
CBitstreamConverter::write_bits(&bs, 32, p_in->rate); // am_sysinfo, unsigned int rate
CBitstreamConverter::write_bits(&bs, 32, p_in->extra); // am_sysinfo, unsigned int extra
CBitstreamConverter::write_bits(&bs, 32, p_in->status); // am_sysinfo, unsigned int status
CBitstreamConverter::write_bits(&bs, 32, p_in->ratio); // am_sysinfo, unsigned int ratio
CBitstreamConverter::write_bits(&bs, 32, (unsigned int)p_in->param); // am_sysinfo, unsigned int param
unsigned int lo = 0x00000000ffffffff & p_in->ratio64;
unsigned int hi = p_in->ratio64 >> 32;
CBitstreamConverter::write_bits(&bs, 32, lo); // am_sysinfo, unsigned long long ratio64
CBitstreamConverter::write_bits(&bs, 32, hi); // am_sysinfo, unsigned long long ratio64
// we do not care about the rest, flush and go.
// FYI there are 4.0 to 4.1 differences here.
CBitstreamConverter::flush_bits(&bs);
//CLog::MemDump((char*)p_out, 0xFF);
#else
// direct struct usage, we do not know which flavor
// so just use what we get from headers and pray.
p_out->handle = -1; //init to invalid
p_out->cntl_handle = -1;
p_out->sub_handle = -1;
p_out->audio_utils_handle = -1;
p_out->has_video = 1;
p_out->noblock = p_in->noblock;
p_out->video_pid = p_in->video_pid;
p_out->video_type = p_in->video_type;
p_out->stream_type = p_in->stream_type;
p_out->am_sysinfo.format = p_in->format;
p_out->am_sysinfo.width = p_in->width;
p_out->am_sysinfo.height = p_in->height;
p_out->am_sysinfo.rate = p_in->rate;
p_out->am_sysinfo.extra = p_in->extra;
p_out->am_sysinfo.status = p_in->status;
p_out->am_sysinfo.ratio = p_in->ratio;
p_out->am_sysinfo.ratio64 = p_in->ratio64;
p_out->am_sysinfo.param = p_in->param;
#endif
}
};
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
// AppContext - Application state
#define MODE_3D_DISABLE 0x00000000
#define MODE_3D_LR 0x00000101
#define MODE_3D_LR_SWITCH 0x00000501
#define MODE_3D_BT 0x00000201
#define MODE_3D_BT_SWITCH 0x00000601
#define MODE_3D_TO_2D_L 0x00000102
#define MODE_3D_TO_2D_R 0x00000902
#define MODE_3D_TO_2D_T 0x00000202
#define MODE_3D_TO_2D_B 0x00000a02
#define PTS_FREQ 90000
#define UNIT_FREQ 96000
#define AV_SYNC_THRESH PTS_FREQ*30
#define TRICKMODE_NONE 0x00
#define TRICKMODE_I 0x01
#define TRICKMODE_FFFB 0x02
// same as AV_NOPTS_VALUE
#define INT64_0 INT64_C(0x8000000000000000)
#define EXTERNAL_PTS (1)
#define SYNC_OUTSIDE (2)
// missing tags
#define CODEC_TAG_VC_1 (0x312D4356)
#define CODEC_TAG_RV30 (0x30335652)
#define CODEC_TAG_RV40 (0x30345652)
#define CODEC_TAG_MJPEG (0x47504a4d)
#define CODEC_TAG_mjpeg (0x47504a4c)
#define CODEC_TAG_jpeg (0x6765706a)
#define CODEC_TAG_mjpa (0x61706a6d)
#define RW_WAIT_TIME (20 * 1000) // 20ms
#define P_PRE (0x02000000)
#define F_PRE (0x03000000)
#define PLAYER_SUCCESS (0)
#define PLAYER_FAILED (-(P_PRE|0x01))
#define PLAYER_NOMEM (-(P_PRE|0x02))
#define PLAYER_EMPTY_P (-(P_PRE|0x03))
#define PLAYER_WR_FAILED (-(P_PRE|0x21))
#define PLAYER_WR_EMPTYP (-(P_PRE|0x22))
#define PLAYER_WR_FINISH (P_PRE|0x1)
#define PLAYER_PTS_ERROR (-(P_PRE|0x31))
#define PLAYER_UNSUPPORT (-(P_PRE|0x35))
#define PLAYER_CHECK_CODEC_ERROR (-(P_PRE|0x39))
#define HDR_BUF_SIZE 1024
typedef struct hdr_buf {
char *data;
int size;
} hdr_buf_t;
typedef struct am_packet {
AVPacket avpkt;
int64_t avpts;
int64_t avdts;
int avduration;
int isvalid;
int newflag;
int64_t lastpts;
unsigned char *data;
unsigned char *buf;
int data_size;
int buf_size;
hdr_buf_t *hdr;
codec_para_t *codec;
} am_packet_t;
typedef enum {
AM_STREAM_UNKNOWN = 0,
AM_STREAM_TS,
AM_STREAM_PS,
AM_STREAM_ES,
AM_STREAM_RM,
AM_STREAM_AUDIO,
AM_STREAM_VIDEO,
} pstream_type;
typedef struct am_private_t
{
am_packet_t am_pkt;
aml_generic_param gcodec;
codec_para_t vcodec;
pstream_type stream_type;
int check_first_pts;
vformat_t video_format;
int video_pid;
unsigned int video_codec_id;
unsigned int video_codec_tag;
vdec_type_t video_codec_type;
unsigned int video_width;
unsigned int video_height;
unsigned int video_ratio;
unsigned int video_ratio64;
unsigned int video_rate;
unsigned int video_rotation_degree;
int extrasize;
uint8_t *extradata;
DllLibAmCodec *m_dll;
int dumpfile;
bool dumpdemux;
} am_private_t;
/*************************************************************************/
/*************************************************************************/
void dumpfile_open(am_private_t *para)
{
if (para->dumpdemux)
{
static int amcodec_dumpid = 0;
char dump_path[128] = {0};
sprintf(dump_path, "/temp/dump_amcodec-%d.dat", amcodec_dumpid++);
para->dumpfile = open(dump_path, O_CREAT | O_RDWR, 0666);
}
}
void dumpfile_close(am_private_t *para)
{
if (para->dumpdemux && para->dumpfile != -1)
close(para->dumpfile), para->dumpfile = -1;
}
void dumpfile_write(am_private_t *para, void* buf, int bufsiz)
{
if (!buf)
{
CLog::Log(LOGERROR, "dumpfile_write: wtf ? buf is null, bufsiz(%d)", bufsiz);
return;
}
if (para->dumpdemux && para->dumpfile != -1)
write(para->dumpfile, buf, bufsiz);
}
static int set_pts_pcrscr(int64_t value)
{
int fd = open("/sys/class/tsync/pts_pcrscr", O_WRONLY);
if (fd >= 0)
{
char pts_str[64];
unsigned long pts = (unsigned long)value;
sprintf(pts_str, "0x%lx", pts);
write(fd, pts_str, strlen(pts_str));
close(fd);
return 0;
}
CLog::Log(LOGERROR, "set_pts_pcrscr: open pts_pcrscr error");
return -1;
}
static vformat_t codecid_to_vformat(enum AVCodecID id)
{
vformat_t format;
switch (id)
{
case AV_CODEC_ID_MPEG1VIDEO:
case AV_CODEC_ID_MPEG2VIDEO:
case AV_CODEC_ID_MPEG2VIDEO_XVMC:
format = VFORMAT_MPEG12;
break;
case AV_CODEC_ID_H263:
case AV_CODEC_ID_MPEG4:
case AV_CODEC_ID_H263P:
case AV_CODEC_ID_H263I:
case AV_CODEC_ID_MSMPEG4V2:
case AV_CODEC_ID_MSMPEG4V3:
case AV_CODEC_ID_FLV1:
format = VFORMAT_MPEG4;
break;
case AV_CODEC_ID_RV10:
case AV_CODEC_ID_RV20:
case AV_CODEC_ID_RV30:
case AV_CODEC_ID_RV40:
format = VFORMAT_REAL;
break;
case AV_CODEC_ID_H264:
format = VFORMAT_H264;
break;
/*
case AV_CODEC_ID_H264MVC:
// H264 Multiview Video Coding (3d blurays)
format = VFORMAT_H264MVC;
break;
*/
case AV_CODEC_ID_MJPEG:
format = VFORMAT_MJPEG;
break;
case AV_CODEC_ID_VC1:
case AV_CODEC_ID_WMV3:
format = VFORMAT_VC1;
break;
case AV_CODEC_ID_AVS:
case AV_CODEC_ID_CAVS:
format = VFORMAT_AVS;
break;
case AV_CODEC_ID_HEVC:
format = VFORMAT_HEVC;
break;
default:
format = VFORMAT_UNSUPPORT;
break;
}
CLog::Log(LOGDEBUG, "codecid_to_vformat, id(%d) -> vformat(%d)", (int)id, format);
return format;
}
static vdec_type_t codec_tag_to_vdec_type(unsigned int codec_tag)
{
vdec_type_t dec_type;
switch (codec_tag)
{
case CODEC_TAG_MJPEG:
case CODEC_TAG_mjpeg:
case CODEC_TAG_jpeg:
case CODEC_TAG_mjpa:
// mjpeg
dec_type = VIDEO_DEC_FORMAT_MJPEG;
break;
case CODEC_TAG_XVID:
case CODEC_TAG_xvid:
case CODEC_TAG_XVIX:
// xvid
dec_type = VIDEO_DEC_FORMAT_MPEG4_5;
break;
case CODEC_TAG_COL1:
case CODEC_TAG_DIV3:
case CODEC_TAG_MP43:
// divx3.11
dec_type = VIDEO_DEC_FORMAT_MPEG4_3;
break;
case CODEC_TAG_DIV4:
case CODEC_TAG_DIVX:
// divx4
dec_type = VIDEO_DEC_FORMAT_MPEG4_4;
break;
case CODEC_TAG_DIV5:
case CODEC_TAG_DX50:
case CODEC_TAG_M4S2:
case CODEC_TAG_FMP4:
// divx5
dec_type = VIDEO_DEC_FORMAT_MPEG4_5;
break;
case CODEC_TAG_DIV6:
// divx6
dec_type = VIDEO_DEC_FORMAT_MPEG4_5;
break;
case CODEC_TAG_MP4V:
case CODEC_TAG_RMP4:
case CODEC_TAG_MPG4:
case CODEC_TAG_mp4v:
case AV_CODEC_ID_MPEG4:
// mp4
dec_type = VIDEO_DEC_FORMAT_MPEG4_5;
break;
case AV_CODEC_ID_H263:
case CODEC_TAG_H263:
case CODEC_TAG_h263:
case CODEC_TAG_s263:
case CODEC_TAG_F263:
// h263
dec_type = VIDEO_DEC_FORMAT_H263;
break;
case CODEC_TAG_AVC1:
case CODEC_TAG_avc1:
case CODEC_TAG_H264:
case CODEC_TAG_h264:
case AV_CODEC_ID_H264:
// h264
dec_type = VIDEO_DEC_FORMAT_H264;
break;
/*
case AV_CODEC_ID_H264MVC:
dec_type = VIDEO_DEC_FORMAT_H264;
break;
*/
case AV_CODEC_ID_RV30:
case CODEC_TAG_RV30:
// realmedia 3
dec_type = VIDEO_DEC_FORMAT_REAL_8;
break;
case AV_CODEC_ID_RV40:
case CODEC_TAG_RV40:
// realmedia 4
dec_type = VIDEO_DEC_FORMAT_REAL_9;
break;
case CODEC_TAG_WMV3:
// wmv3
dec_type = VIDEO_DEC_FORMAT_WMV3;
break;
case AV_CODEC_ID_VC1:
case CODEC_TAG_VC_1:
case CODEC_TAG_WVC1:
case CODEC_TAG_WMVA:
// vc1
dec_type = VIDEO_DEC_FORMAT_WVC1;
break;
case AV_CODEC_ID_VP6F:
// vp6
dec_type = VIDEO_DEC_FORMAT_SW;
break;
case AV_CODEC_ID_CAVS:
case AV_CODEC_ID_AVS:
// avs
dec_type = VIDEO_DEC_FORMAT_AVS;
break;
case AV_CODEC_ID_HEVC:
// h265
dec_type = VIDEO_DEC_FORMAT_HEVC;
break;
default:
dec_type = VIDEO_DEC_FORMAT_UNKNOW;
break;
}
CLog::Log(LOGDEBUG, "codec_tag_to_vdec_type, codec_tag(%d) -> vdec_type(%d)", codec_tag, dec_type);
return dec_type;
}
static void am_packet_init(am_packet_t *pkt)
{
memset(&pkt->avpkt, 0, sizeof(AVPacket));
pkt->avpts = 0;
pkt->avdts = 0;
pkt->avduration = 0;
pkt->isvalid = 0;
pkt->newflag = 0;
pkt->lastpts = 0;
pkt->data = NULL;
pkt->buf = NULL;
pkt->data_size = 0;
pkt->buf_size = 0;
pkt->hdr = NULL;
pkt->codec = NULL;
}
void am_packet_release(am_packet_t *pkt)
{
if (pkt->buf != NULL)
free(pkt->buf), pkt->buf= NULL;
if (pkt->hdr != NULL)
{
if (pkt->hdr->data != NULL)
free(pkt->hdr->data), pkt->hdr->data = NULL;
free(pkt->hdr), pkt->hdr = NULL;
}
pkt->codec = NULL;
}
int check_in_pts(am_private_t *para, am_packet_t *pkt)
{
int last_duration = 0;
static int last_v_duration = 0;
int64_t pts = 0;
last_duration = last_v_duration;
if (para->stream_type == AM_STREAM_ES) {
if ((int64_t)INT64_0 != pkt->avpts) {
pts = pkt->avpts;
if (para->m_dll->codec_checkin_pts(pkt->codec, pts) != 0) {
CLog::Log(LOGDEBUG, "ERROR check in pts error!");
return PLAYER_PTS_ERROR;
}
} else if ((int64_t)INT64_0 != pkt->avdts) {
pts = pkt->avdts * last_duration;
if (para->m_dll->codec_checkin_pts(pkt->codec, pts) != 0) {
CLog::Log(LOGDEBUG, "ERROR check in dts error!");
return PLAYER_PTS_ERROR;
}
last_v_duration = pkt->avduration ? pkt->avduration : 1;
} else {
if (!para->check_first_pts) {
if (para->m_dll->codec_checkin_pts(pkt->codec, 0) != 0) {
CLog::Log(LOGDEBUG, "ERROR check in 0 to video pts error!");
return PLAYER_PTS_ERROR;
}
}
}
if (!para->check_first_pts) {
para->check_first_pts = 1;
}
}
if (pts > 0)
pkt->lastpts = pts;
return PLAYER_SUCCESS;
}
static int write_header(am_private_t *para, am_packet_t *pkt)
{
int write_bytes = 0, len = 0;
if (pkt->hdr && pkt->hdr->size > 0) {
if ((NULL == pkt->codec) || (NULL == pkt->hdr->data)) {
CLog::Log(LOGDEBUG, "[write_header]codec null!");
return PLAYER_EMPTY_P;
}
//some wvc1 es data not need to add header
if (para->video_format == VFORMAT_VC1 && para->video_codec_type == VIDEO_DEC_FORMAT_WVC1) {
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)) {
return PLAYER_SUCCESS;
}
}
while (1) {
write_bytes = para->m_dll->codec_write(pkt->codec, pkt->hdr->data + len, pkt->hdr->size - len);
if (write_bytes < 0 || write_bytes > (pkt->hdr->size - len)) {
if (-errno != AVERROR(EAGAIN)) {
CLog::Log(LOGDEBUG, "ERROR:write header failed!");
return PLAYER_WR_FAILED;
} else {
continue;
}
} else {
dumpfile_write(para, pkt->hdr->data, write_bytes);
len += write_bytes;
if (len == pkt->hdr->size) {
break;
}
}
}
}
return PLAYER_SUCCESS;
}
int check_avbuffer_enough(am_private_t *para, am_packet_t *pkt)
{
return 1;
}
int write_av_packet(am_private_t *para, am_packet_t *pkt)
{
//CLog::Log(LOGDEBUG, "write_av_packet, pkt->isvalid(%d), pkt->data(%p), pkt->data_size(%d)",
// pkt->isvalid, pkt->data, pkt->data_size);
int write_bytes = 0, len = 0, ret;
unsigned char *buf;
int size;
// do we need to check in pts or write the header ?
if (pkt->newflag) {
if (pkt->isvalid) {
ret = check_in_pts(para, pkt);
if (ret != PLAYER_SUCCESS) {
CLog::Log(LOGDEBUG, "check in pts failed");
return PLAYER_WR_FAILED;
}
}
if (write_header(para, pkt) == PLAYER_WR_FAILED) {
CLog::Log(LOGDEBUG, "[%s]write header failed!", __FUNCTION__);
return PLAYER_WR_FAILED;
}
pkt->newflag = 0;
}
buf = pkt->data;
size = pkt->data_size ;
if (size == 0 && pkt->isvalid) {
pkt->isvalid = 0;
pkt->data_size = 0;
}
while (size > 0 && pkt->isvalid) {
write_bytes = para->m_dll->codec_write(pkt->codec, buf, size);
if (write_bytes < 0 || write_bytes > size) {
CLog::Log(LOGDEBUG, "write codec data failed, write_bytes(%d), errno(%d), size(%d)", write_bytes, errno, size);
if (-errno != AVERROR(EAGAIN)) {
CLog::Log(LOGDEBUG, "write codec data failed!");
return PLAYER_WR_FAILED;
} else {
// adjust for any data we already wrote into codec.
// we sleep a bit then exit as we will get called again
// with the same pkt because pkt->isvalid has not been cleared.
pkt->data += len;
pkt->data_size -= len;
usleep(RW_WAIT_TIME);
CLog::Log(LOGDEBUG, "usleep(RW_WAIT_TIME), len(%d)", len);
return PLAYER_SUCCESS;
}
} else {
dumpfile_write(para, buf, write_bytes);
// keep track of what we write into codec from this pkt
// in case we get hit with EAGAIN.
len += write_bytes;
if (len == pkt->data_size) {
pkt->isvalid = 0;
pkt->data_size = 0;
break;
} else if (len < pkt->data_size) {
buf += write_bytes;
size -= write_bytes;
} else {
// writing more that we should is a failure.
return PLAYER_WR_FAILED;
}
}
}
return PLAYER_SUCCESS;
}
/*************************************************************************/
static int m4s2_dx50_mp4v_add_header(unsigned char *buf, int size, am_packet_t *pkt)
{
if (size > pkt->hdr->size) {
free(pkt->hdr->data), pkt->hdr->data = NULL;
pkt->hdr->size = 0;
pkt->hdr->data = (char*)malloc(size);
if (!pkt->hdr->data) {
CLog::Log(LOGDEBUG, "[m4s2_dx50_add_header] NOMEM!");
return PLAYER_FAILED;
}
}
pkt->hdr->size = size;
memcpy(pkt->hdr->data, buf, size);
return PLAYER_SUCCESS;
}
static int m4s2_dx50_mp4v_write_header(am_private_t *para, am_packet_t *pkt)
{
CLog::Log(LOGDEBUG, "m4s2_dx50_mp4v_write_header");
int ret = m4s2_dx50_mp4v_add_header(para->extradata, para->extrasize, pkt);
if (ret == PLAYER_SUCCESS) {
if (1) {
pkt->codec = ¶->vcodec;
} else {
CLog::Log(LOGDEBUG, "[m4s2_dx50_mp4v_add_header]invalid video codec!");
return PLAYER_EMPTY_P;
}
pkt->newflag = 1;
ret = write_av_packet(para, pkt);
}
return ret;
}
static int mjpeg_data_prefeeding(am_packet_t *pkt)
{
const unsigned char mjpeg_addon_data[] = {
0xff, 0xd8, 0xff, 0xc4, 0x01, 0xa2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10,
0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00,
0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31,
0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1,
0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72,
0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29,
0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95,
0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4,
0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1,
0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02, 0x01,
0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51,
0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24,
0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a,
0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82,
0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
};
if (pkt->hdr->data) {
memcpy(pkt->hdr->data, &mjpeg_addon_data, sizeof(mjpeg_addon_data));
pkt->hdr->size = sizeof(mjpeg_addon_data);
} else {
CLog::Log(LOGDEBUG, "[mjpeg_data_prefeeding]No enough memory!");
return PLAYER_FAILED;
}
return PLAYER_SUCCESS;
}
static int mjpeg_write_header(am_private_t *para, am_packet_t *pkt)
{
mjpeg_data_prefeeding(pkt);
if (1) {
pkt->codec = ¶->vcodec;
} else {
CLog::Log(LOGDEBUG, "[mjpeg_write_header]invalid codec!");
return PLAYER_EMPTY_P;
}
pkt->newflag = 1;
write_av_packet(para, pkt);
return PLAYER_SUCCESS;
}
static int divx3_data_prefeeding(am_packet_t *pkt, unsigned w, unsigned h)
{
unsigned i = (w << 12) | (h & 0xfff);
unsigned char divx311_add[10] = {
0x00, 0x00, 0x00, 0x01,
0x20, 0x00, 0x00, 0x00,
0x00, 0x00
};
divx311_add[5] = (i >> 16) & 0xff;
divx311_add[6] = (i >> 8) & 0xff;
divx311_add[7] = i & 0xff;
if (pkt->hdr->data) {
memcpy(pkt->hdr->data, divx311_add, sizeof(divx311_add));
pkt->hdr->size = sizeof(divx311_add);
} else {
CLog::Log(LOGDEBUG, "[divx3_data_prefeeding]No enough memory!");
return PLAYER_FAILED;
}
return PLAYER_SUCCESS;
}
static int divx3_write_header(am_private_t *para, am_packet_t *pkt)
{
CLog::Log(LOGDEBUG, "divx3_write_header");
divx3_data_prefeeding(pkt, para->video_width, para->video_height);
if (1) {
pkt->codec = ¶->vcodec;
} else {
CLog::Log(LOGDEBUG, "[divx3_write_header]invalid codec!");
return PLAYER_EMPTY_P;
}
pkt->newflag = 1;
write_av_packet(para, pkt);
return PLAYER_SUCCESS;
}
static int h264_add_header(unsigned char *buf, int size, am_packet_t *pkt)
{
if (size > HDR_BUF_SIZE)
{
free(pkt->hdr->data);
pkt->hdr->data = (char *)malloc(size);
if (!pkt->hdr->data)
return PLAYER_NOMEM;
}
memcpy(pkt->hdr->data, buf, size);
pkt->hdr->size = size;
return PLAYER_SUCCESS;
}
static int h264_write_header(am_private_t *para, am_packet_t *pkt)
{
// CLog::Log(LOGDEBUG, "h264_write_header");
int ret = h264_add_header(para->extradata, para->extrasize, pkt);
if (ret == PLAYER_SUCCESS) {
//if (ctx->vcodec) {
if (1) {
pkt->codec = ¶->vcodec;
} else {
//CLog::Log(LOGDEBUG, "[pre_header_feeding]invalid video codec!");
return PLAYER_EMPTY_P;
}
pkt->newflag = 1;
ret = write_av_packet(para, pkt);
}
return ret;
}
static int hevc_add_header(unsigned char *buf, int size, am_packet_t *pkt)
{
if (size > HDR_BUF_SIZE)
{
free(pkt->hdr->data);
pkt->hdr->data = (char *)malloc(size);
if (!pkt->hdr->data)
return PLAYER_NOMEM;
}
memcpy(pkt->hdr->data, buf, size);
pkt->hdr->size = size;
return PLAYER_SUCCESS;
}
static int hevc_write_header(am_private_t *para, am_packet_t *pkt)
{
int ret = -1;
if (para->extradata) {
ret = hevc_add_header(para->extradata, para->extrasize, pkt);
}
if (ret == PLAYER_SUCCESS) {
pkt->codec = ¶->vcodec;
pkt->newflag = 1;
ret = write_av_packet(para, pkt);
}
return ret;
}
static int wmv3_write_header(am_private_t *para, am_packet_t *pkt)
{
CLog::Log(LOGDEBUG, "wmv3_write_header");
unsigned i, check_sum = 0;
unsigned data_len = para->extrasize + 4;
pkt->hdr->data[0] = 0;
pkt->hdr->data[1] = 0;
pkt->hdr->data[2] = 1;
pkt->hdr->data[3] = 0x10;
pkt->hdr->data[4] = 0;
pkt->hdr->data[5] = (data_len >> 16) & 0xff;
pkt->hdr->data[6] = 0x88;
pkt->hdr->data[7] = (data_len >> 8) & 0xff;
pkt->hdr->data[8] = data_len & 0xff;
pkt->hdr->data[9] = 0x88;
pkt->hdr->data[10] = 0xff;
pkt->hdr->data[11] = 0xff;
pkt->hdr->data[12] = 0x88;