@@ -78,7 +78,7 @@ struct state_video_compress_oapv {
7878 oapve_stat_t stat{}; // encoding status (output)
7979
8080 oapv_frms_t input_frm{}; // frame for input
81- oapv_imgb_t imgb{} ; // planar pixel data of input frame
81+ oapv_imgb_t * imgb = nullptr ; // planar pixel data of input frame
8282
8383 video_desc saved_desc{}; // last configured video description
8484
@@ -89,75 +89,10 @@ struct state_video_compress_oapv {
8989};
9090
9191state_video_compress_oapv::~state_video_compress_oapv () {
92- for (int i = 0 ; i < OAPV_MAX_CC ; i++) {
93- free (imgb.baddr [i]);
94- }
95-
92+ ug_oapv_imgb_free (imgb);
9693 free (bitb.addr );
9794}
9895
99- bool input_buffer_setup (const oapve_cdesc_t *cdsc, oapv_imgb_t *imgb, int cs) {
100- for (int i = 0 ; i < OAPV_MAX_CC ; i++) {
101- // baddr and a are same for input buffer
102- free (imgb->baddr [i]);
103- imgb->baddr [i] = nullptr ;
104- imgb->a [i] = nullptr ;
105- }
106- memset (imgb, 0 , sizeof (*imgb));
107-
108- int bd = OAPV_CS_GET_BYTE_DEPTH (cs);
109-
110- imgb->w [0 ] = cdsc->param [0 ].w ;
111- imgb->h [0 ] = cdsc->param [0 ].h ;
112-
113- switch (cs) {
114- case OAPV_CS_YCBCR4444_10LE :
115- case OAPV_CS_YCBCR4444_12LE :
116- imgb->w [1 ] = imgb->w [2 ] = imgb->w [3 ] = cdsc->param [0 ].w ;
117- imgb->h [1 ] = imgb->h [2 ] = imgb->h [3 ] = cdsc->param [0 ].h ;
118- imgb->np = 4 ;
119- break ;
120- case OAPV_CS_YCBCR422_10LE :
121- imgb->w [1 ] = imgb->w [2 ] = (cdsc->param [0 ].w + 1 ) >> 1 ;
122- imgb->h [1 ] = imgb->h [2 ] = cdsc->param [0 ].h ;
123- imgb->np = 3 ;
124- break ;
125- case OAPV_CS_YCBCR444_10LE :
126- case OAPV_CS_YCBCR444_12LE :
127- imgb->w [1 ] = imgb->w [2 ] = cdsc->param [0 ].w ;
128- imgb->h [1 ] = imgb->h [2 ] = cdsc->param [0 ].h ;
129- imgb->np = 3 ;
130- break ;
131- default :
132- log_msg (LOG_LEVEL_ERROR , MOD_NAME " Unsupported color format for input buffer: %d\n " , OAPV_CS_GET_FORMAT (cs));
133- return false ;
134- }
135-
136- for (int i = 0 ; i < imgb->np ; i++) {
137- // align width and height to macroblock size, and calculate buffer size
138- imgb->aw [i] = ((imgb->w [i] + OAPV_MB_W - 1 ) / OAPV_MB_W ) * OAPV_MB_W ;
139- imgb->ah [i] = ((imgb->h [i] + OAPV_MB_H - 1 ) / OAPV_MB_H ) * OAPV_MB_H ;
140- imgb->s [i] = imgb->aw [i] * bd;
141- imgb->e [i] = imgb->ah [i];
142-
143- imgb->bsize [i] = imgb->s [i] * imgb->e [i];
144- imgb->a [i] = imgb->baddr [i] = malloc (imgb->bsize [i]);
145- if (imgb->baddr [i] == nullptr ) {
146- log_msg (LOG_LEVEL_ERROR , MOD_NAME " Failed to allocate plane %d for input buffer (%zu bytes).\n " , i, (size_t ) imgb->bsize [i]);
147- for (int j = 0 ; j < i; j++) {
148- free (imgb->baddr [j]);
149- imgb->baddr [j] = nullptr ;
150- imgb->a [j] = nullptr ;
151- }
152- return false ;
153- }
154- memset (imgb->a [i], 0 , imgb->bsize [i]);
155- }
156- imgb->cs = cs;
157-
158- return imgb;
159- }
160-
16196int map_color_spaces_to_profiles (int cs) {
16297 switch (cs) {
16398 case OAPV_CS_YCBCR4444_10LE :
@@ -345,7 +280,7 @@ bool state_video_compress_oapv::parse_fmt(char *fmt)
345280 return true ;
346281}
347282
348- bool configure_with (state_video_compress_oapv *s, video_desc desc) {
283+ bool configure_with (state_video_compress_oapv *s, video_desc desc){
349284 const uv_to_openapv_conversion* conv_struct = get_uv_to_openapv_conversion (desc.color_spec );
350285 if (!conv_struct || conv_struct->convert == nullptr ) {
351286 log_msg (LOG_LEVEL_ERROR , MOD_NAME " unsupported codec\n " );
@@ -361,14 +296,16 @@ bool configure_with(state_video_compress_oapv *s, video_desc desc) {
361296
362297 s->cdsc .param [FRM_INDEX ].profile_idc = map_color_spaces_to_profiles (conv_struct->dst_color_format );
363298
364- if (!input_buffer_setup (&s->cdsc , &s->imgb , conv_struct->dst_color_format )) {
299+ s->imgb = create_oapv_imgb (desc.width , desc.height , conv_struct->dst_color_format );
300+ if (!s->imgb ){
365301 log_msg (LOG_LEVEL_ERROR , MOD_NAME " Failed to set up input buffer\n " );
366302 return false ;
367303 }
304+ s->input_frm .frm [FRM_INDEX ].imgb = s->imgb ;
368305
369306 int raw_bytes = 0 ;
370- for (int i = 0 ; i < s->imgb . np ; i++) {
371- raw_bytes += s->imgb . bsize [i];
307+ for (int i = 0 ; i < s->imgb -> np ; i++) {
308+ raw_bytes += s->imgb -> bsize [i];
372309 }
373310 // allocate bitstream buffer with size based on raw input size * 2 for safe upper bound
374311 const int new_buf_size = raw_bytes * 2 ;
@@ -428,7 +365,7 @@ shared_ptr<video_frame> openapv_compress_tile(void *state, shared_ptr<video_fram
428365 }
429366
430367 struct tile *in_tile = vf_get_tile (tile.get (), 0 );
431- s->convert_to_planar ((const uint8_t *) in_tile->data , desc.width , desc.height , & s->imgb );
368+ s->convert_to_planar ((const uint8_t *) in_tile->data , desc.width , desc.height , s->imgb );
432369
433370 s->bitb .ssize = 0 ;
434371 int ret = oapve_encode (s->enc_h .get (), &s->input_frm , nullptr , &s->bitb , &s->stat , nullptr );
@@ -480,7 +417,7 @@ void* openapv_compress_init(module */*parent*/, const char *opts) {
480417 s->input_frm.num_frms = MAX_NUM_FRMS ;
481418 s->input_frm.frm[FRM_INDEX ].pbu_type = OAPV_PBU_TYPE_PRIMARY_FRAME ;
482419 s->input_frm.frm[FRM_INDEX ].group_id = 1 ;
483- s->input_frm.frm[FRM_INDEX ].imgb = & s->imgb;
420+ s->input_frm.frm[FRM_INDEX ].imgb = s->imgb;
484421
485422 if (opts && opts[0 ] != ' \0 ' ) {
486423 char *fmt = strdup (opts);
0 commit comments