-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.bak.c
executable file
·1869 lines (1607 loc) · 51.6 KB
/
enc.bak.c
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 2004-2013 Freescale Semiconductor, Inc.
*
* Copyright (c) 2006, Chips & Media. All rights reserved.
*/
/*
* The code contained herein is licensed under the GNU General Public
* License. You may obtain a copy of the GNU General Public License
* Version 2 or later at the following locations:
*
* http://www.opensource.org/licenses/gpl-license.html
* http://www.gnu.org/copyleft/gpl.html
*/
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "vpu_test.h"
#include "vpu_jpegtable.h"
#include "g2d.h"
/* V4L2 capture buffers are obtained from here */
/*----move to strcut instance_priv----------------
extern struct capture_testbuffer cap_buffers[];
extern struct g2d_buf *g2d_buffers[];
-------------------------------------------*/
extern struct instance_priv ins_priv[];
/* When app need to exit */
extern int quitflag;
static int sem_post_cnt = 0;
#define FN_ENC_QP_DATA "enc_qp.log"
#define FN_ENC_SLICE_BND_DATA "enc_slice_bnd.log"
#define FN_ENC_MV_DATA "enc_mv.log"
#define FN_ENC_SLICE_DATA "enc_slice.log"
static FILE *fpEncSliceBndInfo = NULL;
static FILE *fpEncQpInfo = NULL;
static FILE *fpEncMvInfo = NULL;
static FILE *fpEncSliceInfo = NULL;
/*---------------for muxing--------------*/
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#define STREAM_FRAME_RATE 30
#define STREAM_PIX_FMT PIX_FMT_YUV420P /* AV_PIX_FMT_YUV420P */ /* default pix_fmt */
static int
h264buff_write(char *buf, int n, int instns);
/*------------------------muxing---------------------------------*/
/*-----------------
< 0 = error
0 = I-Frame
1 = P-Frame
2 = B-Frame
3 = S-Frame
--------------------*/
static int getVopType( const void *p, int len )
{
if ( !p || 6 >= len )
return -1;
unsigned char *b = (unsigned char*)p;
/* Verify NAL marker */
if (b[0] ||b[1] || 0x01 != b[2]){
b++;
if (b[0] || b[1] || 0x01 != b[2])
return -1;
}
b += 3;
/* Verify VOP id */
if (0xb6 == *b){
b++;
return (*b & 0xc0) >> 6;
}
switch(*b){
case 0x65 : return 0;
case 0x61 : return 1;
case 0x01 : return 2;
}
return -1;
}
static int get_nal_type( void *p, int len )
{
if ( !p || 5 >= len )
return -1;
unsigned char *b = (unsigned char*)p;
/* Verify NAL marker */
if ( b[0] || b[1] || 0x01 != b[2] ){
b++;
if ( b[0] || b[1] || 0x01 != b[2] )
return -1;
}
b += 3;
return (*b & 0x1f);
}
/* Add an output stream */
static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec, struct mux_priv *mux_p, void *p, int len)
{
AVCodecContext *pcc;
AVStream *pst;
AVBitStreamFilterContext * avFilter;
enum CodecID codec_id = CODEC_ID_H264;
pst = av_new_stream(oc, 0);
if (!pst){
printf("could not allocate stream \n");
return NULL;
}
pst->id = oc->nb_streams-1;
pcc = pst->codec;
mux_p->vi = pst->index;
//avcodec_get_context_defaults2(pcc, NULL);
pcc->codec_id = codec_id;
pcc->codec_type = CODEC_TYPE_VIDEO;
pcc->bit_rate = 3000000;
pcc->width = 720;
pcc->height = 480;
pcc->time_base.den = 30;
pcc->time_base.num = 1;
pcc->frame_number = 1;
//pcc->gop_size = 12;
pcc->pix_fmt = STREAM_PIX_FMT;
avFilter = av_bitstream_filter_init("h264_mp4toannexb");
pcc->extradata_size = len;
pcc->extradata = (uint8_t*)p; /*set the PPS SPS of H264 */
printf("\n\nzorro, pcc->codec_id = %d. \n",(int)pcc->codec_id);
if (oc->oformat->flags & AVFMT_GLOBALHEADER){
pcc->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
return pst;
}
static int
CreateMp4(AVFormatContext **pm_pOc, char *pszFileName, struct mux_priv *mux_p, void *p, int len)
{
int ret; // 成功返回0,失败返回1
//const char* pszFileName = "output002.mp4";
AVOutputFormat *fmt;
AVCodec *video_codec;
AVStream *m_pVideoSt;
AVFormatContext *m_pOc;
/*0x67 or 0x27*/
if (0x07 != get_nal_type(p, len)){
printf("can not detect nal type");
return -1;
}
av_register_all();
/* auto detect the output format from the name. default is
mpeg. */
fmt = guess_format(NULL, pszFileName, NULL);
if (!fmt) {
printf("Could not deduce output format from file extension: using MPEG.\n");
fmt = guess_format("mpeg", pszFileName, NULL);
}
if (!fmt) {
fprintf(stderr, "Could not find suitable output format\n");
return -1;//
}
/* allocate the output media context */
m_pOc = av_alloc_format_context();
*pm_pOc = m_pOc;
if (!m_pOc) {
fprintf(stderr, "Memory error\n");
return -1;
}
m_pOc->oformat = fmt;
snprintf(m_pOc->filename, sizeof(m_pOc->filename), "%s", pszFileName);
if (fmt->video_codec != CODEC_ID_NONE){
if (m_pVideoSt = add_stream(m_pOc, &video_codec, mux_p, p, len) == NULL){
return -1;
}
}
/* set the output parameters (must be done even if no
parameters). */
if (av_set_parameters(m_pOc, NULL) < 0) {
printf("Invalid output format parameters\n");
return -1;
}
dump_format(m_pOc, 0, pszFileName, 1);
/* open the output file, if needed */
if (!(fmt->flags & AVFMT_NOFILE)) {
if (url_fopen(&m_pOc->pb, pszFileName, URL_WRONLY) < 0) {
printf("Could not open '%s'\n", pszFileName);
return -1;
}
}
/* Write the stream header, if any */
ret = av_write_header(m_pOc);
if (ret < 0){
printf("Error occurred when opening output file");
return -1;
}
return 0;
}
/* write h264 data to mp4 file */
static int
WriteVideo(AVFormatContext *m_pOc, struct mux_priv *mux_p, void* data, int nLen)
{
int ret;
if ( 0 > mux_p->vi ){
printf("vi less than 0");
return -1;
}
AVStream *pst = m_pOc->streams[mux_p->vi];
// Init packet
AVPacket pkt;
// 我的添加,为了计算pts
AVCodecContext *c = pst->codec;
av_init_packet( &pkt );
pkt.flags |= ( 0 >= getVopType( data, nLen ) ) ? PKT_FLAG_KEY : 0;
pkt.stream_index = pst->index;
pkt.data = (uint8_t*)data;
pkt.size = nLen;
// Wait for key frame
if (mux_p->waitkey)
if ( 0 == ( pkt.flags & PKT_FLAG_KEY ) )
return 0;
else
mux_p->waitkey = 0;
pkt.dts = AV_NOPTS_VALUE;
pkt.pts = AV_NOPTS_VALUE;
//pkt.pts = (ptsInc++) * (180000/STREAM_FRAME_RATE);
//pkt.dts = (ptsInc++) * (90000/STREAM_FRAME_RATE);
ret = av_interleaved_write_frame( m_pOc, &pkt );
if (ret < 0){
printf("cannot write frame");
}
av_free_packet(&pkt);
return 0;
}
static void
CloseMp4(AVFormatContext *m_pOc)
{
/* no need zorro
waitkey = -1;
vi = -1; */
if (m_pOc)
av_write_trailer(m_pOc);
if (m_pOc && !(m_pOc->oformat->flags & AVFMT_NOFILE))
url_fclose(m_pOc->pb);/* for new : avio_close(m_pOc->pb); */
if (m_pOc){
av_free(m_pOc);
m_pOc = NULL;
}
}
/*==============get_H264NAL================
*Function: get a NAL from H264buff,when we find the next NAL,
return 0, or return -1,
*Author: zorro
*Date :6/3/2015
*======================================*/
static int
get_H264NAL(void *pbuff, int plen, int *NAL_len, int *NAL_type)
{
unsigned char *H264Data = (unsigned char*)pbuff;
int H264Len = plen;
int next_type;
if (plen <4){
*NAL_len = plen;
return -1;
}
// check this NAL type
if (H264Data[1] || H264Data[2] || 0x01 != H264Data[3]){
*NAL_type = 0;
}else{
*NAL_type = H264Data[4] & 0x1f;
H264Data+=4;
H264Len-=4;
}
while(H264Len){
if(H264Data[0] == 0x00 && H264Data[1] == 0x00 && H264Data[2] == 0x01){
next_type = H264Data[3] & 0x1f;
if(next_type == 0x01 ||next_type == 0x05){
if(H264Data[-1] == 0x00) H264Data--;
*NAL_len = H264Data - (unsigned char *)pbuff;
return 0;
}
}
H264Data+=1;
H264Len-=1;
}
*NAL_len = H264Data - (unsigned char *)pbuff;
return -1;
}
/*==============mux_thread====================
*Function: read H264Data from h264 file, and packet it to MP4 file
*Author: zorro
*Date :6/3/2015
*======================================*/
void
mux_thread(void *arg)
{
struct encode *enc = (struct encode *)arg;
int instns = enc->cmdl->instns;
struct mux_priv mux_p = {0, -1, 1};
char *H264head;
int H264len;
unsigned char NALbuf[512 * 1024];
int NAL_Len;
int read_len;
int NAL_type;
int rs;
int sem_wait_cnt = 0;
int do_while_cnt = 0;
AVFormatContext * pFormatCtx = NULL;
waitenc:
sem_wait(&ins_priv[instns].sem_f);
sem_wait_cnt++;
if(ins_priv[instns].H264buff_flag == 1){
H264head = ins_priv[instns].H264BUFA;
H264len = ins_priv[instns].A_size;
ins_priv[instns].A_size = 0;
}else{
H264head = ins_priv[instns].H264BUFB;
H264len = ins_priv[instns].B_size;
ins_priv[instns].B_size = 0;
}
sync();
do{
do_while_cnt++;
/* reach the next NAL or H264buf end*/
rs = get_H264NAL(H264head, H264len, &read_len, &NAL_type);
if ((H264len - read_len) < 0){
printf("\n\n\nzorro, get_H264NAL err.\n\n");
}
/* no data to read, break out*/
if (read_len <= 0) break;
/* new NAL*/
if (NAL_type > 0){
memcpy(NALbuf, H264head, read_len);
NAL_Len = read_len;
if (NAL_type == 0x05) mux_p.waitkey = 1; /*check key frame*/
}else{ /* last NAL*/
memcpy(&NALbuf[NAL_Len], H264head, read_len);
NAL_Len += read_len;
}
H264head += read_len;
H264len -= read_len;
/* reach the H264buf end */
if (rs < 0){
if (! ins_priv[instns].enc_end)
goto waitenc;
printf("reach the H264 buff end, H264len = %d, NAL_Len = %d, NAL_type = %d\n", H264len, NAL_Len, NAL_type);
}
if (0 < NAL_Len)
{
memset(&NALbuf[NAL_Len], 0, FF_INPUT_BUFFER_PADDING_SIZE);
if (!pFormatCtx){
if (CreateMp4(&pFormatCtx, enc->cmdl->output, &mux_p, NALbuf, NAL_Len) < 0){
break;
}
}
if (pFormatCtx)
if (WriteVideo(pFormatCtx, &mux_p, NALbuf, NAL_Len) < 0)
break;
} /* end if (0 < NAL_Len) */
if (quitflag)
break;
}while(0 < NAL_Len);
CloseMp4(pFormatCtx);
ins_priv[instns].A_size = 0;
ins_priv[instns].B_size = 0;
printf("\nmuxing end\n");
printf("zorro, sem_wait_cnt = %d, do_while_cnt = %d\n",sem_wait_cnt,do_while_cnt);
}
/*==============h264buff_write================
*Function: copy the encoded data frome vpu to H264buff,
*Author: zorro
*Date :6/3/2015
*======================================*/
static int
h264buff_write(char *buf, int n, int instns)
{
if (! ins_priv[instns].H264buff_flag){
if ( ins_priv[instns].A_size >= 5000000){
printf("zorro,memory err ,out of H264BUFA range..\n" );
return -1;
}
memcpy((void *)(& ins_priv[instns].H264BUFA[ ins_priv[instns].A_size]), (void *)buf, n);
ins_priv[instns].A_size+=n;
if ( ins_priv[instns].A_size >= 5000000){
ins_priv[instns].H264buff_flag = 1;
sem_post(& ins_priv[instns].sem_f);
sem_post_cnt++;
}
}else{
if (ins_priv[instns].B_size >= 5000000){
printf("zorro,memory err ,out of H264BUFB range..\n" );
return -1;
}
memcpy((void *)(& ins_priv[instns].H264BUFB[ ins_priv[instns].B_size]), (void *)buf, n);
ins_priv[instns].B_size+=n;
if ( ins_priv[instns].B_size >= 5000000){
ins_priv[instns].H264buff_flag = 0;
sem_post(& ins_priv[instns].sem_f);
sem_post_cnt++;
}
}
return 0;
}
/*------------------------muxing---------------------------------*/
void SaveEncMbInfo(u8 *mbParaBuf, int size, int MbNumX, int EncNum)
{
int i;
if(!fpEncQpInfo)
fpEncQpInfo = fopen(FN_ENC_QP_DATA, "w+");
if(!fpEncQpInfo)
return;
if(!fpEncSliceBndInfo)
fpEncSliceBndInfo = fopen(FN_ENC_SLICE_BND_DATA, "w+");
if(!fpEncSliceBndInfo)
return;
fprintf(fpEncQpInfo, "FRAME [%1d]\n", EncNum);
fprintf(fpEncSliceBndInfo, "FRAME [%1d]\n", EncNum);
for(i=0; i < size; i++) {
fprintf(fpEncQpInfo, "MbAddr[%4d]: MbQs[%2d]\n", i, mbParaBuf[i] & 63);
fprintf(fpEncSliceBndInfo, "MbAddr[%4d]: Slice Boundary Flag[%1d]\n",
i, (mbParaBuf[i] >> 6) & 1);
}
fprintf(fpEncQpInfo, "\n");
fprintf(fpEncSliceBndInfo, "\n");
fflush(fpEncQpInfo);
fflush(fpEncSliceBndInfo);
}
void SaveEncMvInfo(u8 *mvParaBuf, int size, int MbNumX, int EncNum)
{
int i;
if(!fpEncMvInfo)
fpEncMvInfo = fopen(FN_ENC_MV_DATA, "w+");
if(!fpEncMvInfo)
return;
fprintf(fpEncMvInfo, "FRAME [%1d]\n", EncNum);
for(i=0; i<size/4; i++) {
u16 mvX = (mvParaBuf[0] << 8) | (mvParaBuf[1]);
u16 mvY = (mvParaBuf[2] << 8) | (mvParaBuf[3]);
if(mvX & 0x8000) {
fprintf(fpEncMvInfo, "MbAddr[%4d:For ]: Avail[0] Mv[%5d:%5d]\n", i, 0, 0);
} else {
mvX = (mvX & 0x7FFF) | ((mvX << 1) & 0x8000);
fprintf(fpEncMvInfo, "MbAddr[%4d:For ]: Avail[1] Mv[%5d:%5d]\n", i, mvX, mvY);
}
mvParaBuf += 4;
}
fprintf(fpEncMvInfo, "\n");
fflush(fpEncMvInfo);
}
void SaveEncSliceInfo(u8 *SliceParaBuf, int size, int EncNum)
{
int i, nMbAddr, nSliceBits;
if(!fpEncSliceInfo)
fpEncSliceInfo = fopen(FN_ENC_SLICE_DATA, "w+");
if(!fpEncSliceInfo)
return;
fprintf(fpEncSliceInfo, "EncFrmNum[%3d]\n", EncNum);
for(i=0; i<size / 8; i++) {
nMbAddr = (SliceParaBuf[2] << 8) | SliceParaBuf[3];
nSliceBits = (int)(SliceParaBuf[4] << 24)|(SliceParaBuf[5] << 16)|
(SliceParaBuf[6] << 8)|(SliceParaBuf[7]);
fprintf(fpEncSliceInfo, "[%2d] mbIndex.%3d, Bits.%d\n", i, nMbAddr, nSliceBits);
SliceParaBuf += 8;
}
fprintf(fpEncSliceInfo, "\n");
fflush(fpEncSliceInfo);
}
void jpgGetHuffTable(EncMjpgParam *param)
{
/* Rearrange and insert pre-defined Huffman table to deticated variable. */
memcpy(param->huffBits[DC_TABLE_INDEX0], lumaDcBits, 16); /* Luma DC BitLength */
memcpy(param->huffVal[DC_TABLE_INDEX0], lumaDcValue, 16); /* Luma DC HuffValue */
memcpy(param->huffBits[AC_TABLE_INDEX0], lumaAcBits, 16); /* Luma DC BitLength */
memcpy(param->huffVal[AC_TABLE_INDEX0], lumaAcValue, 162); /* Luma DC HuffValue */
memcpy(param->huffBits[DC_TABLE_INDEX1], chromaDcBits, 16); /* Chroma DC BitLength */
memcpy(param->huffVal[DC_TABLE_INDEX1], chromaDcValue, 16); /* Chroma DC HuffValue */
memcpy(param->huffBits[AC_TABLE_INDEX1], chromaAcBits, 16); /* Chroma AC BitLength */
memcpy(param->huffVal[AC_TABLE_INDEX1], chromaAcValue, 162); /* Chorma AC HuffValue */
}
void jpgGetQMatrix(EncMjpgParam *param)
{
/* Rearrange and insert pre-defined Q-matrix to deticated variable. */
memcpy(param->qMatTab[DC_TABLE_INDEX0], lumaQ2, 64);
memcpy(param->qMatTab[AC_TABLE_INDEX0], chromaBQ2, 64);
memcpy(param->qMatTab[DC_TABLE_INDEX1], param->qMatTab[DC_TABLE_INDEX0], 64);
memcpy(param->qMatTab[AC_TABLE_INDEX1], param->qMatTab[AC_TABLE_INDEX0], 64);
}
void jpgGetCInfoTable(EncMjpgParam *param)
{
int format = param->mjpg_sourceFormat;
memcpy(param->cInfoTab, cInfoTable[format], 6 * 4);
}
static int
enc_readbs_reset_buffer(struct encode *enc, PhysicalAddress paBsBufAddr, int bsBufsize)
{
u32 vbuf;
vbuf = enc->virt_bsbuf_addr + paBsBufAddr - enc->phy_bsbuf_addr;
//return vpu_write(enc->cmdl, (void *)vbuf, bsBufsize);
return h264buff_write( (void *)vbuf, bsBufsize, enc->cmdl->instns);
}
static int
enc_readbs_ring_buffer(EncHandle handle, struct cmd_line *cmd,
u32 bs_va_startaddr, u32 bs_va_endaddr, u32 bs_pa_startaddr,
int defaultsize)
{
RetCode ret;
int space = 0, room;
PhysicalAddress pa_read_ptr, pa_write_ptr;
u32 target_addr, size;
ret = vpu_EncGetBitstreamBuffer(handle, &pa_read_ptr, &pa_write_ptr,
(Uint32 *)&size);
if (ret != RETCODE_SUCCESS) {
err_msg("EncGetBitstreamBuffer failed\n");
return -1;
}
/* No space in ring buffer */
if (size <= 0)
return 0;
if (defaultsize > 0) {
if (size < defaultsize)
return 0;
space = defaultsize;
} else {
space = size;
}
if (space > 0) {
target_addr = bs_va_startaddr + (pa_read_ptr - bs_pa_startaddr);
if ( (target_addr + space) > bs_va_endaddr) {
room = bs_va_endaddr - target_addr;
vpu_write(cmd, (void *)target_addr, room);
vpu_write(cmd, (void *)bs_va_startaddr,(space - room));
} else {
vpu_write(cmd, (void *)target_addr, space);
}
ret = vpu_EncUpdateBitstreamBuffer(handle, space);
if (ret != RETCODE_SUCCESS) {
err_msg("EncUpdateBitstreamBuffer failed\n");
return -1;
}
}
return space;
}
static int
encoder_fill_headers(struct encode *enc)
{
EncHeaderParam enchdr_param = {0};
EncHandle handle = enc->handle;
RetCode ret;
int mbPicNum;
/* Must put encode header before encoding */
if (enc->cmdl->format == STD_MPEG4) {
enchdr_param.headerType = VOS_HEADER;
if (cpu_is_mx6x())
goto put_mp4header;
/*
* Please set userProfileLevelEnable to 0 if you need to generate
* user profile and level automaticaly by resolution, here is one
* sample of how to work when userProfileLevelEnable is 1.
*/
enchdr_param.userProfileLevelEnable = 1;
mbPicNum = ((enc->enc_picwidth + 15) / 16) *((enc->enc_picheight + 15) / 16);
if (enc->enc_picwidth <= 176 && enc->enc_picheight <= 144 &&
mbPicNum * enc->cmdl->fps <= 1485)
enchdr_param.userProfileLevelIndication = 8; /* L1 */
/* Please set userProfileLevelIndication to 8 if L0 is needed */
else if (enc->enc_picwidth <= 352 && enc->enc_picheight <= 288 &&
mbPicNum * enc->cmdl->fps <= 5940)
enchdr_param.userProfileLevelIndication = 2; /* L2 */
else if (enc->enc_picwidth <= 352 && enc->enc_picheight <= 288 &&
mbPicNum * enc->cmdl->fps <= 11880)
enchdr_param.userProfileLevelIndication = 3; /* L3 */
else if (enc->enc_picwidth <= 640 && enc->enc_picheight <= 480 &&
mbPicNum * enc->cmdl->fps <= 36000)
enchdr_param.userProfileLevelIndication = 4; /* L4a */
else if (enc->enc_picwidth <= 720 && enc->enc_picheight <= 576 &&
mbPicNum * enc->cmdl->fps <= 40500)
enchdr_param.userProfileLevelIndication = 5; /* L5 */
else
enchdr_param.userProfileLevelIndication = 6; /* L6 */
put_mp4header:
vpu_EncGiveCommand(handle, ENC_PUT_MP4_HEADER, &enchdr_param);
if (enc->ringBufferEnable == 0 ) {
ret = enc_readbs_reset_buffer(enc, enchdr_param.buf, enchdr_param.size);
if (ret < 0)
return -1;
}
enchdr_param.headerType = VIS_HEADER;
vpu_EncGiveCommand(handle, ENC_PUT_MP4_HEADER, &enchdr_param);
if (enc->ringBufferEnable == 0 ) {
ret = enc_readbs_reset_buffer(enc, enchdr_param.buf, enchdr_param.size);
if (ret < 0)
return -1;
}
enchdr_param.headerType = VOL_HEADER;
vpu_EncGiveCommand(handle, ENC_PUT_MP4_HEADER, &enchdr_param);
if (enc->ringBufferEnable == 0 ) {
ret = enc_readbs_reset_buffer(enc, enchdr_param.buf, enchdr_param.size);
if (ret < 0)
return -1;
}
} else if (enc->cmdl->format == STD_AVC) {
if (!enc->mvc_extension || !enc->mvc_paraset_refresh_en) {
enchdr_param.headerType = SPS_RBSP;
vpu_EncGiveCommand(handle, ENC_PUT_AVC_HEADER, &enchdr_param);
if (enc->ringBufferEnable == 0 ) {
ret = enc_readbs_reset_buffer(enc, enchdr_param.buf, enchdr_param.size);
if (ret < 0)
return -1;
}
}
if (enc->mvc_extension) {
enchdr_param.headerType = SPS_RBSP_MVC;
vpu_EncGiveCommand(handle, ENC_PUT_AVC_HEADER, &enchdr_param);
if (enc->ringBufferEnable == 0 ) {
ret = enc_readbs_reset_buffer(enc, enchdr_param.buf, enchdr_param.size);
if (ret < 0)
return -1;
}
}
enchdr_param.headerType = PPS_RBSP;
vpu_EncGiveCommand(handle, ENC_PUT_AVC_HEADER, &enchdr_param);
if (enc->ringBufferEnable == 0 ) {
ret = enc_readbs_reset_buffer(enc, enchdr_param.buf, enchdr_param.size);
if (ret < 0)
return -1;
}
if (enc->mvc_extension) { /* MVC */
enchdr_param.headerType = PPS_RBSP_MVC;
vpu_EncGiveCommand(handle, ENC_PUT_AVC_HEADER, &enchdr_param);
if (enc->ringBufferEnable == 0 ) {
ret = enc_readbs_reset_buffer(enc, enchdr_param.buf, enchdr_param.size);
if (ret < 0)
return -1;
}
}
} else if (enc->cmdl->format == STD_MJPG) {
if (enc->huffTable)
free(enc->huffTable);
if (enc->qMatTable)
free(enc->qMatTable);
if (cpu_is_mx6x()) {
EncParamSet enchdr_param = {0};
enchdr_param.size = STREAM_BUF_SIZE;
enchdr_param.pParaSet = malloc(STREAM_BUF_SIZE);
if (enchdr_param.pParaSet) {
vpu_EncGiveCommand(handle,ENC_GET_JPEG_HEADER, &enchdr_param);
vpu_write(enc->cmdl, (void *)enchdr_param.pParaSet, enchdr_param.size);
free(enchdr_param.pParaSet);
} else {
err_msg("memory allocate failure\n");
return -1;
}
}
}
return 0;
}
void
encoder_free_framebuffer(struct encode *enc)
{
int i;
for (i = 0; i < enc->totalfb; i++) {
framebuf_free(enc->pfbpool[i]);
}
free(enc->fb);
free(enc->pfbpool);
printf("zorro, encoder_free_framebuffer end.\n");
}
int
encoder_allocate_framebuffer(struct encode *enc)
{
EncHandle handle = enc->handle;
int i, enc_stride, src_stride, src_fbid;
int totalfb, minfbcount, srcfbcount, extrafbcount;
RetCode ret;
FrameBuffer *fb;
PhysicalAddress subSampBaseA = 0, subSampBaseB = 0;
struct frame_buf **pfbpool;
EncExtBufInfo extbufinfo = {0};
int enc_fbwidth, enc_fbheight, src_fbwidth, src_fbheight;
minfbcount = enc->minFrameBufferCount;
dprintf(4, "minfb %d\n", minfbcount);
srcfbcount = 1;
enc_fbwidth = (enc->enc_picwidth + 15) & ~15;
enc_fbheight = (enc->enc_picheight + 15) & ~15;
src_fbwidth = (enc->src_picwidth + 15) & ~15;
src_fbheight = (enc->src_picheight + 15) & ~15;
if (cpu_is_mx6x()) {
if (enc->cmdl->format == STD_AVC && enc->mvc_extension) /* MVC */
extrafbcount = 2 + 2; /* Subsamp [2] + Subsamp MVC [2] */
else if (enc->cmdl->format == STD_MJPG)
extrafbcount = 0;
else
extrafbcount = 2; /* Subsamp buffer [2] */
} else
extrafbcount = 0;
enc->totalfb = totalfb = minfbcount + extrafbcount + srcfbcount;
/* last framebuffer is used as src frame in the test */
enc->src_fbid = src_fbid = totalfb - 1;
fb = enc->fb = calloc(totalfb, sizeof(FrameBuffer));
if (fb == NULL) {
err_msg("Failed to allocate enc->fb\n");
return -1;
}
pfbpool = enc->pfbpool = calloc(totalfb,
sizeof(struct frame_buf *));
if (pfbpool == NULL) {
err_msg("Failed to allocate enc->pfbpool\n");
free(fb);
return -1;
}
if (enc->cmdl->mapType == LINEAR_FRAME_MAP) {
/* All buffers are linear */
for (i = 0; i < minfbcount + extrafbcount; i++) {
pfbpool[i] = framebuf_alloc(enc->cmdl->format, enc->mjpg_fmt,
enc_fbwidth, enc_fbheight, 0);
if (pfbpool[i] == NULL) {
goto err1;
}
}
} else {
/* Encoded buffers are tiled */
for (i = 0; i < minfbcount; i++) {
pfbpool[i] = tiled_framebuf_alloc(enc->cmdl->format, enc->mjpg_fmt,
enc_fbwidth, enc_fbheight, 0, enc->cmdl->mapType);
if (pfbpool[i] == NULL)
goto err1;
}
/* sub frames are linear */
for (i = minfbcount; i < minfbcount + extrafbcount; i++) {
pfbpool[i] = framebuf_alloc(enc->cmdl->format, enc->mjpg_fmt,
enc_fbwidth, enc_fbheight, 0);
if (pfbpool[i] == NULL)
goto err1;
}
}
for (i = 0; i < minfbcount + extrafbcount; i++) {
fb[i].myIndex = i;
fb[i].bufY = pfbpool[i]->addrY;
fb[i].bufCb = pfbpool[i]->addrCb;
fb[i].bufCr = pfbpool[i]->addrCr;
fb[i].strideY = pfbpool[i]->strideY;
fb[i].strideC = pfbpool[i]->strideC;
}
if (cpu_is_mx6x() && (enc->cmdl->format != STD_MJPG)) {
subSampBaseA = fb[minfbcount].bufY;
subSampBaseB = fb[minfbcount + 1].bufY;
if (enc->cmdl->format == STD_AVC && enc->mvc_extension) { /* MVC */
extbufinfo.subSampBaseAMvc = fb[minfbcount + 2].bufY;
extbufinfo.subSampBaseBMvc = fb[minfbcount + 3].bufY;
}
}
/* Must be a multiple of 16 */
if (enc->cmdl->rot_angle == 90 || enc->cmdl->rot_angle == 270)
enc_stride = (enc->enc_picheight + 15 ) & ~15;
else
enc_stride = (enc->enc_picwidth + 15) & ~15;
src_stride = (enc->src_picwidth + 15 ) & ~15;
extbufinfo.scratchBuf = enc->scratchBuf;
ret = vpu_EncRegisterFrameBuffer(handle, fb, minfbcount, enc_stride, src_stride,
subSampBaseA, subSampBaseB, &extbufinfo);
if (ret != RETCODE_SUCCESS) {
err_msg("Register frame buffer failed\n");
goto err1;
}
if (enc->cmdl->src_scheme == PATH_V4L2) {
ret = v4l_capture_setup(enc, enc->src_picwidth, enc->src_picheight, enc->cmdl->fps);
if (ret < 0) {
goto err1;
}
} else {
/* Allocate a single frame buffer for source frame */
pfbpool[src_fbid] = framebuf_alloc(enc->cmdl->format, enc->mjpg_fmt,
src_fbwidth, src_fbheight, 0);
if (pfbpool[src_fbid] == NULL) {
err_msg("failed to allocate single framebuf\n");
goto err1;
}
fb[src_fbid].myIndex = enc->src_fbid;
fb[src_fbid].bufY = pfbpool[src_fbid]->addrY;
fb[src_fbid].bufCb = pfbpool[src_fbid]->addrCb;
fb[src_fbid].bufCr = pfbpool[src_fbid]->addrCr;
fb[src_fbid].strideY = pfbpool[src_fbid]->strideY;
fb[src_fbid].strideC = pfbpool[src_fbid]->strideC;
}
return 0;
err1:
for (i = 0; i < totalfb; i++) {
framebuf_free(pfbpool[i]);
}
free(fb);
free(pfbpool);
return -1;
}
static int
read_from_file(struct encode *enc)
{
u32 y_addr, u_addr, v_addr;
struct frame_buf *pfb = enc->pfbpool[enc->src_fbid];
int divX, divY;
int src_fd = enc->cmdl->src_fd;
int format = enc->mjpg_fmt;
int chromaInterleave = enc->cmdl->chromaInterleave;
int img_size, y_size, c_size;
int ret = 0;
if (enc->src_picwidth != pfb->strideY) {
err_msg("Make sure src pic width is a multiple of 16\n");
return -1;
}
divX = (format == MODE420 || format == MODE422) ? 2 : 1;
divY = (format == MODE420 || format == MODE224) ? 2 : 1;
y_size = enc->src_picwidth * enc->src_picheight;
c_size = y_size / divX / divY;
img_size = y_size + c_size * 2;
y_addr = pfb->addrY + pfb->desc.virt_uaddr - pfb->desc.phy_addr;
u_addr = pfb->addrCb + pfb->desc.virt_uaddr - pfb->desc.phy_addr;
v_addr = pfb->addrCr + pfb->desc.virt_uaddr - pfb->desc.phy_addr;
if (img_size == pfb->desc.size) {
ret = freadn(src_fd, (void *)y_addr, img_size);
} else {
ret = freadn(src_fd, (void *)y_addr, y_size);
if (chromaInterleave == 0) {
ret = freadn(src_fd, (void *)u_addr, c_size);
ret = freadn(src_fd, (void *)v_addr, c_size);
} else {
ret = freadn(src_fd, (void *)u_addr, c_size * 2);
}
}
return ret;
}
static inline void wait_queue(int instns)
{
pthread_mutex_lock(&ins_priv[instns].vpu_mutex);
ins_priv[instns].vpu_waiting = 1;
pthread_cond_wait(&ins_priv[instns].vpu_cond, &ins_priv[instns].vpu_mutex);
pthread_mutex_unlock(&ins_priv[instns].vpu_mutex);
}
static inline void wakeup_queue(int instns)
{
pthread_cond_signal(&ins_priv[instns].vpu_cond);