1 /***************************************************************************
2 \fn ADM_VideoEncoders
3 \brief Internal handling of video encoders
4 -------------------
5
6 copyright : (C) 2018 by mean
7 email : fixounet@free.fr
8 ***************************************************************************/
9 /* Derived from libva sample code */
10 /*
11 * Copyright (c) 2007-2013 Intel Corporation. All Rights Reserved.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sub license, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
28 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
30 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 */
33 /***************************************************************************
34 * *
35 * This program is free software; you can redistribute it and/or modify *
36 * it under the terms of the GNU General Public License as published by *
37 * the Free Software Foundation; either version 2 of the License, or *
38 * (at your option) any later version. *
39 * *
40 ***************************************************************************/
41 #include "ADM_default.h"
42
43 #include <getopt.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/mman.h>
48
49
50 #include "ADM_libvaEncoder.h"
51 #include "va/va.h"
52 #include "va/va_enc_h264.h"
53 #include "ADM_coreLibVA_buffer.h"
54 #include "vaDefines.h"
55 #include "ADM_coreLibVA_encodingContext.h"
56 #include "ADM_libVaEncodingContextH264.h"
57
58 /**
59 \fn ADM_libvaEncoder
60 */
ADM_libvaEncoder(ADM_coreVideoFilter * src,bool globalHeader)61 ADM_libvaEncoder::ADM_libvaEncoder(ADM_coreVideoFilter *src,bool globalHeader) : ADM_coreVideoEncoder(src)
62 {
63 ADM_info("[LibVAEncoder] Creating, globalHeader=%d.\n",globalHeader);
64 image=new ADMImageDefault(getWidth(),getHeight());
65 vaContext=NULL;
66 extraDataSize=0;
67 extraData=NULL;
68 this->globalHeader=globalHeader;
69 }
70 /**
71 *
72 * @return
73 */
setup(void)74 bool ADM_libvaEncoder::setup(void)
75 {
76 ADM_info("[LibVAEncoder] Setting up.\n");
77 int w=getWidth(),h=getHeight();
78 std::vector<ADM_vaSurface *>xNone;
79 #if 0
80 vaContext= ADM_vaEncodingContext::allocate(0,w,h,getFrameIncrement(),xNone);
81 if(!vaContext)
82 return false;
83 #else
84 // Allocate a new one
85 ADM_vaEncodingContextH264Base *ctx;
86 ctx=new ADM_vaEncodingContextH264AnnexB(globalHeader);
87 if(!ctx->setup(w, h, getFrameIncrement(), xNone))
88 {
89 delete ctx;
90 ctx=NULL;
91 return false;
92 }
93 vaContext=ctx;
94 #endif
95
96 vaContext->generateExtraData(&(this->extraDataSize),&(this->extraData));
97 return true;
98 }
99
100
101 /**
102 \fn ~ADM_libvaEncoder
103 */
~ADM_libvaEncoder()104 ADM_libvaEncoder::~ADM_libvaEncoder()
105 {
106 ADM_info("[LibVAEncoder] Destroying.\n");
107 if(vaContext)
108 {
109 delete vaContext;
110 vaContext=NULL;
111 }
112 if(extraData)
113 {
114 delete [] extraData;
115 extraData=NULL;
116 }
117
118 }
119
120
121 /**
122 \fn encode
123 */
encode(ADMBitstream * out)124 bool ADM_libvaEncoder::encode (ADMBitstream * out)
125 {
126 uint32_t fn;
127 aprintf("[LibVAEncoder] Encoding.\n");
128
129 if(source->getNextFrame(&fn,image)==false)
130 {
131 ADM_warning("[LIBVA] Cannot get next image\n");
132 return false;
133 }
134 bool r=vaContext->encode(image,out);
135 aprintf("Encoding frame %d, result = %d, size=%d\n",fn,r,out->len);
136 return r;
137 }
138
getExtraData(uint32_t * l,uint8_t ** d)139 bool ADM_libvaEncoder::getExtraData(uint32_t *l,uint8_t **d)
140 {
141 *l=extraDataSize;
142 *d=extraData;
143 return true;
144 }
145
146
147
148 // EOF
149