1 /*
2  INTEL CONFIDENTIAL
3  Copyright 2009 Intel Corporation All Rights Reserved.
4  The source code contained or described herein and all documents related to the source code ("Material") are owned by Intel Corporation or its suppliers or licensors. Title to the Material remains with Intel Corporation or its suppliers and licensors. The Material contains trade secrets and proprietary and confidential information of Intel or its suppliers and licensors. The Material is protected by worldwide copyright and trade secret laws and treaty provisions. No part of the Material may be used, copied, reproduced, modified, published, uploaded, posted, transmitted, distributed, or disclosed in any way without Intel’s prior express written permission.
5 
6  No license under any patent, copyright, trade secret or other intellectual property right is granted to or conferred upon you by disclosure or delivery of the Materials, either expressly, by implication, inducement, estoppel or otherwise. Any license under such intellectual property rights must be express and approved by Intel in writing.
7  */
8 
9 
10 #include <glib.h>
11 #include <dlfcn.h>
12 #include <string.h>
13 
14 #include "vc1.h"
15 #include "vbp_loader.h"
16 #include "vbp_utils.h"
17 #include "vbp_vc1_parser.h"
18 
19 /* maximum number of Macroblock divided by 2, see va.h */
20 #define MAX_BITPLANE_SIZE 16384
21 
22 /* Start code prefix is 001 which is 3 bytes. */
23 #define PREFIX_SIZE 3
24 
25 static uint32 b_fraction_table[][9] = {
26   /* num       0  1  2  3  4  5   6   7   8   den */
27   /* 0 */    { 0, 0, 0, 0, 0, 0,  0,  0,  0 },
28   /* 1 */    { 0, 0, 0, 1, 3, 5,  9, 11, 17 },
29   /* 2 */    { 0, 0, 0, 2, 0, 6,  0, 12,  0 },
30   /* 3 */    { 0, 0, 0, 0, 4, 7,  0, 13, 18 },
31   /* 4 */    { 0, 0, 0, 0, 0, 8,  0, 14,  0 },
32   /* 5 */    { 0, 0, 0, 0, 0, 0, 10, 15, 19 },
33   /* 6 */    { 0, 0, 0, 0, 0, 0,  0, 16,  0 },
34   /* 7 */    { 0, 0, 0, 0, 0, 0,  0,  0, 20 }
35 };
36 
37 
38 
39 /**
40  * set parser entry points
41  */
vbp_init_parser_entries_vc1(vbp_context * pcontext)42 uint32 vbp_init_parser_entries_vc1(vbp_context *pcontext)
43 {
44 	if (NULL == pcontext->parser_ops)
45 	{
46 		/* impossible, just sanity check */
47 		return VBP_PARM;
48 	}
49 
50 	pcontext->parser_ops->init = dlsym(pcontext->fd_parser, "viddec_vc1_init");
51 	if (NULL == pcontext->parser_ops->init)
52 	{
53 		ETRACE ("Failed to set entry point.");
54 		return VBP_LOAD;
55 	}
56 
57 	pcontext->parser_ops->parse_sc = viddec_parse_sc;
58 
59 	pcontext->parser_ops->parse_syntax = dlsym(pcontext->fd_parser, "viddec_vc1_parse");
60 	if (NULL == pcontext->parser_ops->parse_syntax)
61 	{
62 		ETRACE ("Failed to set entry point.");
63 		return VBP_LOAD;
64 	}
65 
66 	pcontext->parser_ops->get_cxt_size = dlsym(pcontext->fd_parser, "viddec_vc1_get_context_size");
67 	if (NULL == pcontext->parser_ops->get_cxt_size)
68 	{
69 		ETRACE ("Failed to set entry point.");
70 		return VBP_LOAD;
71 	}
72 
73 	pcontext->parser_ops->is_wkld_done = dlsym(pcontext->fd_parser, "viddec_vc1_wkld_done");
74 	if (NULL == pcontext->parser_ops->is_wkld_done)
75 	{
76 		ETRACE ("Failed to set entry point.");
77 		return VBP_LOAD;
78 	}
79 
80 	pcontext->parser_ops->is_frame_start = dlsym(pcontext->fd_parser, "viddec_vc1_is_start_frame");
81 	if (NULL == pcontext->parser_ops->is_frame_start)
82 	{
83 		ETRACE ("Failed to set entry point.");
84 		return VBP_LOAD;
85 	}
86 
87 	return VBP_OK;
88 }
89 
90 /**
91  * allocate query data structure
92  */
vbp_allocate_query_data_vc1(vbp_context * pcontext)93 uint32 vbp_allocate_query_data_vc1(vbp_context *pcontext)
94 {
95 	if (NULL != pcontext->query_data)
96 	{
97 		/* impossible, just sanity check */
98 		return VBP_PARM;
99 	}
100 
101 	pcontext->query_data = NULL;
102 
103 	vbp_data_vc1 *query_data = NULL;
104 	query_data = g_try_new0(vbp_data_vc1, 1);
105 	if (NULL == query_data)
106 	{
107 		return VBP_MEM;
108 	}
109 
110 	/* assign the pointer */
111 	pcontext->query_data = (void *)query_data;
112 
113 	query_data->se_data = g_try_new0(vbp_codec_data_vc1, 1);
114 	if (NULL == query_data->se_data)
115 	{
116 		goto cleanup;
117 	}
118 	query_data->pic_data = g_try_new0(vbp_picture_data_vc1, MAX_NUM_PICTURES);
119 	if (NULL == query_data->pic_data)
120 	{
121 		goto cleanup;
122 	}
123 
124 	int i;
125 	for (i = 0; i < MAX_NUM_PICTURES; i++)
126 	{
127 		query_data->pic_data[i].pic_parms = g_try_new0(VAPictureParameterBufferVC1, 1);
128 		if (NULL == query_data->pic_data[i].pic_parms)
129 		{
130 			goto cleanup;
131 		}
132 
133 		query_data->pic_data[i].packed_bitplanes = g_try_malloc0(MAX_BITPLANE_SIZE);
134 		if (NULL == query_data->pic_data[i].packed_bitplanes)
135 		{
136 			goto cleanup;
137 		}
138 
139 		query_data->pic_data[i].slc_data = g_try_malloc0(MAX_NUM_SLICES * sizeof(vbp_slice_data_vc1));
140 		if (NULL == query_data->pic_data[i].slc_data)
141 		{
142 			goto cleanup;
143 		}
144 	}
145 
146 	return VBP_OK;
147 
148 cleanup:
149 	vbp_free_query_data_vc1(pcontext);
150 
151 	return VBP_MEM;
152 }
153 
154 
155 /**
156  * free query data structure
157  */
vbp_free_query_data_vc1(vbp_context * pcontext)158 uint32 vbp_free_query_data_vc1(vbp_context *pcontext)
159 {
160 	vbp_data_vc1 *query_data = NULL;
161 
162 	if (NULL == pcontext->query_data)
163 	{
164 		return VBP_OK;
165 	}
166 
167 	query_data = (vbp_data_vc1 *)pcontext->query_data;
168 
169 	if (query_data->pic_data)
170 	{
171 		int i = 0;
172 		for (i = 0; i < MAX_NUM_PICTURES; i++)
173 		{
174 			g_free(query_data->pic_data[i].slc_data);
175 			g_free(query_data->pic_data[i].packed_bitplanes);
176 			g_free(query_data->pic_data[i].pic_parms);
177 		}
178 	}
179 
180 	g_free(query_data->pic_data);
181 
182 	g_free(query_data->se_data);
183 
184 	g_free(query_data);
185 
186 	pcontext->query_data = NULL;
187 
188 	return VBP_OK;
189 }
190 
191 
192 /**
193  * We want to create a list of buffer segments where each segment is a start
194  * code followed by all the data up to the next start code or to the end of
195  * the buffer.  In VC-1, it is common to get buffers with no start codes.  The
196  * parser proper, doesn't really handle the situation where there are no SCs.
197  * In this case, I will bypass the stripping of the SC code and assume a frame.
198  */
vbp_parse_start_code_helper_vc1(viddec_pm_cxt_t * cxt,viddec_parser_ops_t * ops,int init_data_flag)199 static uint32 vbp_parse_start_code_helper_vc1(
200 	viddec_pm_cxt_t *cxt,
201 	viddec_parser_ops_t *ops,
202 	int init_data_flag)
203 {
204 	uint32_t ret = VBP_OK;
205 	viddec_sc_parse_cubby_cxt_t cubby;
206 
207 	/* make copy of cubby */
208 	/* this doesn't copy the buffer, merely the structure that holds the buffer */
209 	/* pointer.  Below, where we call parse_sc() the code starts the search for */
210 	/* SCs at the beginning of the buffer pointed to by the cubby, so in our */
211 	/* cubby copy we increment the pointer as we move through the buffer.  If */
212 	/* you think of each start code followed either by another start code or the */
213 	/* end of the buffer, then parse_sc() is returning information relative to */
214 	/* current segment. */
215 
216 	cubby = cxt->parse_cubby;
217 
218 	cxt->list.num_items = 0;
219 	cxt->list.data[0].stpos = 0;
220   	cxt->getbits.is_emul_reqd = 1;
221 
222 	/* codec initialization data is always start code prefixed. (may not start at position 0)
223 	 * sample buffer for AP has three start code patterns here:
224 	 * pattern 0: no start code at all, the whole buffer is a single segment item
225 	 * pattern 1: start codes for all segment items
226 	 * pattern 2: no start code for the first segment item, start codes for the rest segment items
227 	 */
228 
229 	gboolean is_pattern_two = FALSE;
230 
231 	unsigned char start_code = 0;
232 
233 	while(1)
234 	{
235 		/* parse the created buffer for sc */
236 		ret = ops->parse_sc((void *)&cubby, (void *)&(cxt->codec_data[0]), &(cxt->sc_prefix_info));
237 		if(ret == 1)
238 		{
239 			cubby.phase = 0;
240 			start_code = *(unsigned char*)(cubby.buf + cubby.sc_end_pos);
241 #if 1
242 			if (0 == init_data_flag &&
243 				PREFIX_SIZE != cubby.sc_end_pos &&
244 				0 == cxt->list.num_items)
245 			{
246 				/* buffer does not have start code at the beginning */
247 				vc1_viddec_parser_t *parser = NULL;
248 				vc1_metadata_t *seqLayerHeader = NULL;
249 
250 				parser = (vc1_viddec_parser_t *)cxt->codec_data;
251 				seqLayerHeader = &(parser->info.metadata);
252 				if (1 == seqLayerHeader->INTERLACE)
253 				{
254 					/* this is a hack for interlaced field coding */
255 					/* handle field interlace coding. One sample contains two fields, where:
256 					 * the first field does not have start code prefix,
257 					 * the second field has start code prefix.
258 					 */
259 					cxt->list.num_items = 1;
260 					cxt->list.data[0].stpos = 0;
261 					is_pattern_two = TRUE;
262 				}
263 			}
264 #endif
265 			if (cxt->list.num_items == 0)  /* found first SC. */
266 			{
267 				/* sc_end_pos gets us to the SC type.  We need to back up to the first zero */
268 				cxt->list.data[0].stpos = cubby.sc_end_pos - PREFIX_SIZE;
269 			}
270 			else
271 			{
272 				/* First we set the end position of the last segment. */
273 				/* Since the SC parser searches from SC type to SC type and the */
274 				/* sc_end_pos is relative to this segment only, we merely add */
275 				/* sc_end_pos to the start to find the end. */
276 				cxt->list.data[cxt->list.num_items - 1].edpos =
277 					cubby.sc_end_pos + cxt->list.data[cxt->list.num_items - 1].stpos;
278 
279 				/* Then we set the start position of the current segment. */
280 				/* So I need to subtract 1 ??? */
281 				cxt->list.data[cxt->list.num_items].stpos =
282 					cxt->list.data[cxt->list.num_items - 1].edpos;
283 
284 				if (is_pattern_two)
285 				{
286 					cxt->list.data[cxt->list.num_items].stpos -= PREFIX_SIZE;
287 					/* restore to normal pattern */
288 					is_pattern_two = FALSE;
289 				}
290 			}
291 			/* We need to set up the cubby buffer for the next time through parse_sc(). */
292 			/* But even though we want the list to contain a segment as described */
293 			/* above, we want the cubby buffer to start just past the prefix, or it will */
294 			/* find the same SC again.  So I bump the cubby buffer past the prefix. */
295 			cubby.buf = cubby.buf +
296 				cxt->list.data[cxt->list.num_items].stpos +
297 				PREFIX_SIZE;
298 
299 			cubby.size = cxt->parse_cubby.size -
300 				cxt->list.data[cxt->list.num_items].stpos -
301 				PREFIX_SIZE;
302 
303 			if (start_code >= 0x0A && start_code <= 0x0F)
304 			{
305 				/* only put known start code to the list
306 				 * 0x0A: end of sequence
307 				 * 0x0B: slice header
308 				 * 0x0C: frame header
309 				 * 0x0D: field header
310 				 * 0x0E: entry point header
311 				 * 0x0F: sequence header
312 				 */
313 				cxt->list.num_items++;
314 			}
315 			else
316 			{
317 				ITRACE("skipping unknown start code :%d", start_code);
318 			}
319 
320 			if (cxt->list.num_items >= MAX_IBUFS_PER_SC)
321 			{
322 				WTRACE("Num items exceeds the limit!");
323 				/* not fatal, just stop parsing */
324 				break;
325 			}
326 		}
327 		else
328 		{
329 			/* we get here, if we reach the end of the buffer while looking or a SC. */
330 			/* If we never found a SC, then num_items will never get incremented. */
331 			if (cxt->list.num_items == 0)
332 			{
333 				/* If we don't find a SC we probably still have a frame of data. */
334 				/* So let's bump the num_items or else later we will not parse the */
335 				/* frame.   */
336 				cxt->list.num_items = 1;
337 			}
338 			/* now we can set the end position of the last segment. */
339 			cxt->list.data[cxt->list.num_items - 1].edpos = cxt->parse_cubby.size;
340 			break;
341 		}
342 	}
343 	return VBP_OK;
344 }
345 
346 /*
347 * parse initialization data (decoder configuration data)
348 * for VC1 advanced profile, data is sequence header and
349 * entry pointer header.
350 * for VC1 main/simple profile, data format
351 * is defined in VC1 spec: Annex J, (Decoder initialization metadata
352 * structure 1 and structure 3
353 */
vbp_parse_init_data_vc1(vbp_context * pcontext)354 uint32 vbp_parse_init_data_vc1(vbp_context *pcontext)
355 {
356 	/**
357 	* init data (aka decoder configuration data) must
358 	* be start-code prefixed
359 	*/
360 
361 	viddec_pm_cxt_t *cxt = pcontext->parser_cxt;
362 	viddec_parser_ops_t *ops = pcontext->parser_ops;
363 	return vbp_parse_start_code_helper_vc1(cxt, ops, 1);
364 }
365 
366 
367 
368 /**
369 * Parse start codes, VC1 main/simple profile does not have start code;
370 * VC1 advanced may not have start code either.
371 */
vbp_parse_start_code_vc1(vbp_context * pcontext)372 uint32_t vbp_parse_start_code_vc1(vbp_context *pcontext)
373 {
374 	viddec_pm_cxt_t *cxt = pcontext->parser_cxt;
375 	viddec_parser_ops_t *ops = pcontext->parser_ops;
376 
377 	vc1_viddec_parser_t *parser = NULL;
378 	vc1_metadata_t *seqLayerHeader = NULL;
379 
380 	vbp_data_vc1 *query_data = (vbp_data_vc1 *) pcontext->query_data;
381 
382 	/* Reset query data for the new sample buffer */
383 	int i = 0;
384 	for (i = 0; i < MAX_NUM_PICTURES; i++)
385 	{
386 		query_data->num_pictures = 0;
387 		query_data->pic_data[i].num_slices = 0;
388 		query_data->pic_data[i].picture_is_skipped = 0;
389 	}
390 
391 	parser = (vc1_viddec_parser_t *)cxt->codec_data;
392 	seqLayerHeader = &(parser->info.metadata);
393 
394 
395 	/* WMV codec data will have a start code, but the WMV picture data won't. */
396 	if (VC1_PROFILE_ADVANCED == seqLayerHeader->PROFILE)
397 	{
398 		return vbp_parse_start_code_helper_vc1(cxt, ops, 0);
399 	}
400 	else
401 	{
402 		/* WMV: vc1 simple or main profile. No start code present.
403 		*/
404 
405 		/* must set is_emul_reqd to 0! */
406 		cxt->getbits.is_emul_reqd = 0;
407 		cxt->list.num_items = 1;
408 		cxt->list.data[0].stpos = 0;
409 		cxt->list.data[0].edpos = cxt->parse_cubby.size;
410 	}
411 
412 	return VBP_OK;
413 }
414 
415 
416 /**
417  *
418  */
vbp_get_bit_vc1(uint32 * data,uint32 * current_word,uint32 * current_bit)419 static inline uint8 vbp_get_bit_vc1(uint32 *data, uint32 *current_word, uint32 *current_bit)
420 {
421 	uint8 value;
422 
423 	value = (data[*current_word] >> *current_bit) & 1;
424 
425 	/* Fix up bit/byte offsets.  endianess?? */
426 	if (*current_bit < 31)
427 	{
428 		++(*current_bit);
429 	}
430 	else
431 	{
432 		++(*current_word);
433 		*current_bit = 0;
434 	}
435 
436 	return value;
437 }
438 
439 
440 /**
441  *
442  */
vbp_pack_bitplane_vc1(uint32 * from_plane,uint8 * to_plane,uint32 width,uint32 height,uint32 nibble_shift)443 static uint32 vbp_pack_bitplane_vc1(
444 	uint32 *from_plane,
445 	uint8 *to_plane,
446 	uint32 width,
447 	uint32 height,
448 	uint32 nibble_shift)
449 {
450 	uint32 error = VBP_OK;
451 	uint32 current_word = 0;
452 	uint32 current_bit = 0;  /* must agree with number in vbp_get_bit_vc1 */
453 	uint32 i, j, n;
454 	uint8 value;
455 	uint32 stride = 0;
456 
457 	stride = 32 * ((width + 31) / 32);
458 
459 	for (i = 0, n = 0; i < height; i++)
460 	{
461 		for (j = 0; j < stride; j++)
462 		{
463 			if (j < width)
464 			{
465 				value = vbp_get_bit_vc1(
466 					from_plane,
467 					&current_word,
468 					&current_bit);
469 
470 				to_plane[n / 2] |= value << (nibble_shift + ((n % 2) ? 0 : 4));
471 				n++;
472 			}
473 			else
474 			{
475 				break;
476 			}
477 		}
478 		if (stride > width)
479 		{
480 			current_word++;
481 			current_bit = 0;
482 		}
483 	}
484 
485 	return error;
486 }
487 
488 
489 /**
490  *
491  */
vbp_map_bfraction(uint32 numerator,uint32 denominator)492 static inline uint32 vbp_map_bfraction(uint32 numerator, uint32 denominator)
493 {
494 	uint32 b_fraction = 0;
495 
496 	if ((numerator < 8) && (denominator < 9))
497 	{
498 		b_fraction = b_fraction_table[numerator][denominator];
499 	}
500 
501 	return b_fraction;
502 }
503 
504 /**
505  *
506  */
vbp_pack_bitplanes_vc1(vbp_context * pcontext,int index,vbp_picture_data_vc1 * pic_data)507 static uint32 vbp_pack_bitplanes_vc1(
508 	vbp_context *pcontext,
509 	int index,
510 	vbp_picture_data_vc1* pic_data)
511 {
512 	uint32 error = VBP_OK;
513 	if (0 == pic_data->pic_parms->bitplane_present.value)
514 	{
515 		/* return if bitplane is not present */
516 		pic_data->size_bitplanes = 0;
517 		memset(pic_data->packed_bitplanes, 0, MAX_BITPLANE_SIZE);
518 		return error;
519 	}
520 
521 	vc1_viddec_parser_t *parser = (vc1_viddec_parser_t *)pcontext->parser_cxt->codec_data;
522 	vc1_metadata_t *seqLayerHeader = &(parser->info.metadata);
523 	vc1_PictureLayerHeader *picLayerHeader = &(parser->info.picLayerHeader);
524 
525 
526 	/* set bit plane size */
527 	pic_data->size_bitplanes = ((seqLayerHeader->widthMB * seqLayerHeader->heightMB) + 1) / 2;
528 
529 
530 	memset(pic_data->packed_bitplanes, 0, pic_data->size_bitplanes);
531 
532 	/* see libva library va.h for nibble bit */
533 	switch (picLayerHeader->PTYPE)
534 	{
535 		case VC1_I_FRAME:
536 		case VC1_BI_FRAME:
537 		if (picLayerHeader->OVERFLAGS.imode)
538 		{
539 			vbp_pack_bitplane_vc1(
540 				picLayerHeader->OVERFLAGS.databits,
541 				pic_data->packed_bitplanes,
542 				seqLayerHeader->widthMB,
543 				seqLayerHeader->heightMB,
544 				2);
545 		}
546 		if (picLayerHeader->ACPRED.imode)
547 		{
548 			vbp_pack_bitplane_vc1(
549 				picLayerHeader->ACPRED.databits,
550 				pic_data->packed_bitplanes,
551 				seqLayerHeader->widthMB,
552 				seqLayerHeader->heightMB,
553 				1);
554 		}
555 		if (picLayerHeader->FIELDTX.imode)
556 		{
557 			vbp_pack_bitplane_vc1(
558 				picLayerHeader->FIELDTX.databits,
559 				pic_data->packed_bitplanes,
560 				seqLayerHeader->widthMB,
561 				seqLayerHeader->heightMB,
562 				0);
563 		}
564 		/* sanity check */
565 		if (picLayerHeader->MVTYPEMB.imode ||
566 			picLayerHeader->DIRECTMB.imode ||
567 			picLayerHeader->SKIPMB.imode ||
568 			picLayerHeader->FORWARDMB.imode)
569 		{
570 			ETRACE("Unexpected bit-plane type.");
571 			error = VBP_TYPE;
572 		}
573 		break;
574 
575 		case VC1_P_FRAME:
576 		if (picLayerHeader->MVTYPEMB.imode)
577 		{
578 			vbp_pack_bitplane_vc1(
579 				picLayerHeader->MVTYPEMB.databits,
580 				pic_data->packed_bitplanes,
581 				seqLayerHeader->widthMB,
582 				seqLayerHeader->heightMB,
583 				2);
584 		}
585 		if (picLayerHeader->SKIPMB.imode)
586 		{
587 			vbp_pack_bitplane_vc1(
588 				picLayerHeader->SKIPMB.databits,
589 				pic_data->packed_bitplanes,
590 				seqLayerHeader->widthMB,
591 				seqLayerHeader->heightMB,
592 				1);
593 		}
594 		if (picLayerHeader->DIRECTMB.imode)
595 		{
596 			vbp_pack_bitplane_vc1(
597 				picLayerHeader->DIRECTMB.databits,
598 				pic_data->packed_bitplanes,
599 				seqLayerHeader->widthMB,
600 				seqLayerHeader->heightMB,
601 				0);
602 		}
603 		/* sanity check */
604 		if (picLayerHeader->FIELDTX.imode ||
605 			picLayerHeader->FORWARDMB.imode ||
606 			picLayerHeader->ACPRED.imode ||
607 			picLayerHeader->OVERFLAGS.imode )
608 		{
609 			ETRACE("Unexpected bit-plane type.");
610 			error = VBP_TYPE;
611 		}
612 		break;
613 
614 		case VC1_B_FRAME:
615 		if (picLayerHeader->FORWARDMB.imode)
616 		{
617 			vbp_pack_bitplane_vc1(
618 				picLayerHeader->FORWARDMB.databits,
619 				pic_data->packed_bitplanes,
620 				seqLayerHeader->widthMB,
621 				seqLayerHeader->heightMB,
622 				2);
623 		}
624 		if (picLayerHeader->SKIPMB.imode)
625 		{
626 			vbp_pack_bitplane_vc1(
627 				picLayerHeader->SKIPMB.databits,
628 				pic_data->packed_bitplanes,
629 				seqLayerHeader->widthMB,
630 				seqLayerHeader->heightMB,
631 				1);
632 		}
633 		if (picLayerHeader->DIRECTMB.imode)
634 		{
635 			vbp_pack_bitplane_vc1(
636 				picLayerHeader->DIRECTMB.databits,
637 				pic_data->packed_bitplanes,
638 				seqLayerHeader->widthMB,
639 				seqLayerHeader->heightMB,
640 				0);
641 		}
642 		/* sanity check */
643 		if (picLayerHeader->MVTYPEMB.imode ||
644 			picLayerHeader->FIELDTX.imode ||
645 			picLayerHeader->ACPRED.imode ||
646 			picLayerHeader->OVERFLAGS.imode)
647 		{
648 			ETRACE("Unexpected bit-plane type.");
649 			error = VBP_TYPE;
650 		}
651 		break;
652 	}
653 	return error;
654 }
655 
656 
657 /**
658  * fill the query data structure after sequence header, entry point header
659  * or a complete frame is parsed.
660  * NOTE: currently partial frame is not handled properly
661  */
vbp_populate_query_data_vc1(vbp_context * pcontext)662 uint32 vbp_populate_query_data_vc1(vbp_context *pcontext)
663 {
664 	uint32 error = VBP_OK;
665 
666 	vc1_viddec_parser_t *parser = (vc1_viddec_parser_t *)pcontext->parser_cxt->codec_data;
667 	vc1_metadata_t *seqLayerHeader = &(parser->info.metadata);
668 
669 	vbp_data_vc1 *query_data = (vbp_data_vc1 *)pcontext->query_data;
670 
671 	/* first we get the SH/EP data.  Can we cut down on this? */
672 	vbp_codec_data_vc1 *se_data = query_data->se_data;
673 	se_data->PROFILE = seqLayerHeader->PROFILE;
674 	se_data->LEVEL = seqLayerHeader->LEVEL;
675 	se_data->POSTPROCFLAG = seqLayerHeader->POSTPROCFLAG;
676 	se_data->PULLDOWN = seqLayerHeader->PULLDOWN;
677 	se_data->INTERLACE = seqLayerHeader->INTERLACE;
678 	se_data->TFCNTRFLAG = seqLayerHeader->TFCNTRFLAG;
679 	se_data->FINTERPFLAG = seqLayerHeader->FINTERPFLAG;
680 	se_data->PSF = seqLayerHeader->PSF;
681 	se_data->BROKEN_LINK = seqLayerHeader->BROKEN_LINK;
682 	se_data->CLOSED_ENTRY = seqLayerHeader->CLOSED_ENTRY;
683 	se_data->PANSCAN_FLAG = seqLayerHeader->PANSCAN_FLAG;
684 	se_data->REFDIST_FLAG = seqLayerHeader->REFDIST_FLAG;
685 	se_data->LOOPFILTER = seqLayerHeader->LOOPFILTER;
686 	se_data->FASTUVMC = seqLayerHeader->FASTUVMC;
687 	se_data->EXTENDED_MV = seqLayerHeader->EXTENDED_MV;
688 	se_data->DQUANT = seqLayerHeader->DQUANT;
689 	se_data->VSTRANSFORM = seqLayerHeader->VSTRANSFORM;
690 	se_data->OVERLAP = seqLayerHeader->OVERLAP;
691 	se_data->QUANTIZER = seqLayerHeader->QUANTIZER;
692 	se_data->CODED_WIDTH = (seqLayerHeader->width + 1) << 1;
693 	se_data->CODED_HEIGHT = (seqLayerHeader->height + 1) << 1;
694 	se_data->EXTENDED_DMV = seqLayerHeader->EXTENDED_DMV;
695 	se_data->RANGE_MAPY_FLAG = seqLayerHeader->RANGE_MAPY_FLAG;
696 	se_data->RANGE_MAPY = seqLayerHeader->RANGE_MAPY;
697 	se_data->RANGE_MAPUV_FLAG = seqLayerHeader->RANGE_MAPUV_FLAG;
698 	se_data->RANGE_MAPUV = seqLayerHeader->RANGE_MAPUV;
699 	se_data->RANGERED = seqLayerHeader->RANGERED;
700 	se_data->MAXBFRAMES = seqLayerHeader->MAXBFRAMES;
701 	se_data->MULTIRES = seqLayerHeader->MULTIRES;
702 	se_data->SYNCMARKER = seqLayerHeader->SYNCMARKER;
703 	se_data->RNDCTRL = seqLayerHeader->RNDCTRL;
704 	se_data->REFDIST = seqLayerHeader->REFDIST;
705 	se_data->widthMB = seqLayerHeader->widthMB;
706 	se_data->heightMB = seqLayerHeader->heightMB;
707 	se_data->INTCOMPFIELD = seqLayerHeader->INTCOMPFIELD;
708 	se_data->LUMSCALE2 = seqLayerHeader->LUMSCALE2;
709 	se_data->LUMSHIFT2 = seqLayerHeader->LUMSHIFT2;
710 
711 	/* update buffer number */
712 	query_data->buf_number = buffer_counter;
713 
714 	if (query_data->num_pictures > 2)
715 	{
716 		WTRACE("sampe buffer contains %d pictures", query_data->num_pictures);
717 	}
718 	return error;
719 }
720 
721 
722 
vbp_pack_picture_params_vc1(vbp_context * pcontext,int index,vbp_picture_data_vc1 * pic_data)723 static void vbp_pack_picture_params_vc1(
724 	vbp_context *pcontext,
725 	int index,
726 	vbp_picture_data_vc1* pic_data)
727 {
728 	viddec_pm_cxt_t *cxt = pcontext->parser_cxt;
729 	vc1_viddec_parser_t *parser = (vc1_viddec_parser_t *)cxt->codec_data;
730 	vc1_metadata_t *seqLayerHeader = &(parser->info.metadata);
731 	vc1_PictureLayerHeader *picLayerHeader = &(parser->info.picLayerHeader);
732 
733 
734 	VAPictureParameterBufferVC1 *pic_parms = pic_data->pic_parms;
735 
736 	/* Then we get the picture header data.  Picture type need translation. */
737 	pic_parms->forward_reference_picture = VA_INVALID_SURFACE;
738 	pic_parms->backward_reference_picture = VA_INVALID_SURFACE;
739 	pic_parms->inloop_decoded_picture = VA_INVALID_SURFACE;
740 
741 	pic_parms->sequence_fields.value = 0;
742 	pic_parms->sequence_fields.bits.interlace = seqLayerHeader->INTERLACE;
743 	pic_parms->sequence_fields.bits.syncmarker = seqLayerHeader->SYNCMARKER;
744 	pic_parms->sequence_fields.bits.overlap = seqLayerHeader->OVERLAP;
745 
746 	pic_parms->coded_width = (seqLayerHeader->width + 1) << 1;
747 	pic_parms->coded_height = (seqLayerHeader->height + 1) << 1;
748 
749 	pic_parms->entrypoint_fields.value = 0;
750 	pic_parms->entrypoint_fields.bits.closed_entry = seqLayerHeader->CLOSED_ENTRY;
751 	pic_parms->entrypoint_fields.bits.broken_link = seqLayerHeader->BROKEN_LINK;
752 	pic_parms->entrypoint_fields.bits.loopfilter = seqLayerHeader->LOOPFILTER;
753 
754 	pic_parms->conditional_overlap_flag = picLayerHeader->CONDOVER;
755 	pic_parms->fast_uvmc_flag = seqLayerHeader->FASTUVMC;
756 
757 	pic_parms->range_mapping_fields.value = 0;
758 	pic_parms->range_mapping_fields.bits.luma_flag = seqLayerHeader->RANGE_MAPY_FLAG;
759 	pic_parms->range_mapping_fields.bits.luma = seqLayerHeader->RANGE_MAPY;
760 	pic_parms->range_mapping_fields.bits.chroma_flag = seqLayerHeader->RANGE_MAPUV_FLAG;
761 	pic_parms->range_mapping_fields.bits.chroma = seqLayerHeader->RANGE_MAPUV;
762 
763 	pic_parms->b_picture_fraction =
764 		vbp_map_bfraction(picLayerHeader->BFRACTION_NUM, picLayerHeader->BFRACTION_DEN);
765 
766 	pic_parms->cbp_table = picLayerHeader->CBPTAB;
767 	pic_parms->mb_mode_table = picLayerHeader->MBMODETAB;
768 	pic_parms->range_reduction_frame = picLayerHeader->RANGEREDFRM;
769 	pic_parms->rounding_control = picLayerHeader->RNDCTRL;
770 	pic_parms->post_processing = picLayerHeader->POSTPROC;
771 	/* fix this.  Add RESPIC to parser.  */
772 	pic_parms->picture_resolution_index = 0;
773 	pic_parms->luma_scale = picLayerHeader->LUMSCALE;
774 	pic_parms->luma_shift = picLayerHeader->LUMSHIFT;
775 
776 	pic_parms->picture_fields.value = 0;
777 	switch (picLayerHeader->PTYPE)
778 	{
779 		case VC1_I_FRAME:
780 		pic_parms->picture_fields.bits.picture_type = VC1_PTYPE_I;
781 		break;
782 
783 		case VC1_P_FRAME:
784 		pic_parms->picture_fields.bits.picture_type = VC1_PTYPE_P;
785 		break;
786 
787 		case VC1_B_FRAME:
788 		pic_parms->picture_fields.bits.picture_type = VC1_PTYPE_B;
789 		break;
790 
791 		case VC1_BI_FRAME:
792 		pic_parms->picture_fields.bits.picture_type = VC1_PTYPE_BI;
793 		break;
794 
795 		case VC1_SKIPPED_FRAME:
796 		pic_data->picture_is_skipped = VC1_PTYPE_SKIPPED;
797 		break;
798 
799 		default:
800 		/* to do: handle this case */
801 		break;
802 	}
803 	pic_parms->picture_fields.bits.frame_coding_mode = picLayerHeader->FCM;
804 	if (0 == seqLayerHeader->PROFILE || 1 == seqLayerHeader->PROFILE)
805 	{
806 		/* simple or main profile, top field flag is not present, default to 1.*/
807 		pic_parms->picture_fields.bits.top_field_first = 1;
808 	}
809 	else
810 	{
811 		pic_parms->picture_fields.bits.top_field_first = picLayerHeader->TFF;
812 	}
813 
814 	pic_parms->picture_fields.bits.is_first_field = !(picLayerHeader->CurrField);
815 	/* This seems to be set based on the MVMODE and MVMODE2 syntax. */
816 	/* This is a hack.  Probably will need refining. */
817 	if ((VC1_MVMODE_INTENSCOMP == picLayerHeader->MVMODE) ||
818 		(VC1_MVMODE_INTENSCOMP == picLayerHeader->MVMODE2))
819 	{
820 		pic_parms->picture_fields.bits.intensity_compensation = 1;
821 	}
822 	else
823 	{
824 		pic_parms->picture_fields.bits.intensity_compensation = picLayerHeader->INTCOMP;
825 	}
826 
827 	/* Lets store the raw-mode BP bits. */
828 	pic_parms->raw_coding.value = 0;
829 	pic_parms->raw_coding.flags.mv_type_mb = picLayerHeader->raw_MVTYPEMB;
830 	pic_parms->raw_coding.flags.direct_mb = picLayerHeader->raw_DIRECTMB;
831 	pic_parms->raw_coding.flags.skip_mb = picLayerHeader->raw_SKIPMB;
832 	pic_parms->raw_coding.flags.field_tx = picLayerHeader->raw_FIELDTX;
833 	pic_parms->raw_coding.flags.forward_mb = picLayerHeader->raw_FORWARDMB;
834 	pic_parms->raw_coding.flags.ac_pred = picLayerHeader->raw_ACPRED;
835 	pic_parms->raw_coding.flags.overflags = picLayerHeader->raw_OVERFLAGS;
836 
837 	/* imode 1/0 indicates bitmap presence in Pic Hdr. */
838 	pic_parms->bitplane_present.value = 0;
839 
840 	pic_parms->bitplane_present.flags.bp_mv_type_mb =
841 		pic_parms->raw_coding.flags.mv_type_mb ? 1 :
842 		(picLayerHeader->MVTYPEMB.imode ? 1: 0);
843 
844 	pic_parms->bitplane_present.flags.bp_direct_mb =
845 		pic_parms->raw_coding.flags.direct_mb ? 1 :
846 		(picLayerHeader->DIRECTMB.imode ? 1: 0);
847 
848 	pic_parms->bitplane_present.flags.bp_skip_mb =
849 		pic_parms->raw_coding.flags.skip_mb ? 1 :
850 		(picLayerHeader->SKIPMB.imode ? 1: 0);
851 
852 	pic_parms->bitplane_present.flags.bp_field_tx =
853 		pic_parms->raw_coding.flags.field_tx ? 1 :
854 		(picLayerHeader->FIELDTX.imode ? 1: 0);
855 
856 	pic_parms->bitplane_present.flags.bp_forward_mb =
857 		pic_parms->raw_coding.flags.forward_mb ? 1 :
858 		(picLayerHeader->FORWARDMB.imode ? 1: 0);
859 
860 	pic_parms->bitplane_present.flags.bp_ac_pred =
861 		pic_parms->raw_coding.flags.ac_pred ? 1 :
862 		(picLayerHeader->ACPRED.imode ? 1: 0);
863 
864 	pic_parms->bitplane_present.flags.bp_overflags =
865 		pic_parms->raw_coding.flags.overflags ? 1 :
866 		(picLayerHeader->OVERFLAGS.imode ? 1: 0);
867 
868 	pic_parms->reference_fields.value = 0;
869 	pic_parms->reference_fields.bits.reference_distance_flag =
870 		seqLayerHeader->REFDIST_FLAG;
871 
872 	pic_parms->reference_fields.bits.reference_distance =
873 		seqLayerHeader->REFDIST;
874 
875 	pic_parms->reference_fields.bits.num_reference_pictures =
876 		picLayerHeader->NUMREF;
877 
878 	pic_parms->reference_fields.bits.reference_field_pic_indicator =
879 		picLayerHeader->REFFIELD;
880 
881 	pic_parms->mv_fields.value = 0;
882 	pic_parms->mv_fields.bits.mv_mode = picLayerHeader->MVMODE;
883 	pic_parms->mv_fields.bits.mv_mode2 = picLayerHeader->MVMODE2;
884 
885 	pic_parms->mv_fields.bits.mv_table = picLayerHeader->MVTAB;
886 	pic_parms->mv_fields.bits.two_mv_block_pattern_table = picLayerHeader->MV2BPTAB;
887 	pic_parms->mv_fields.bits.four_mv_switch = picLayerHeader->MV4SWITCH;
888 	pic_parms->mv_fields.bits.four_mv_block_pattern_table = picLayerHeader->MV4BPTAB;
889 	pic_parms->mv_fields.bits.extended_mv_flag = seqLayerHeader->EXTENDED_MV;
890 	pic_parms->mv_fields.bits.extended_mv_range = picLayerHeader->MVRANGE;
891 	pic_parms->mv_fields.bits.extended_dmv_flag = seqLayerHeader->EXTENDED_DMV;
892 	pic_parms->mv_fields.bits.extended_dmv_range = picLayerHeader->DMVRANGE;
893 
894 	pic_parms->pic_quantizer_fields.value = 0;
895 	pic_parms->pic_quantizer_fields.bits.dquant = seqLayerHeader->DQUANT;
896 	pic_parms->pic_quantizer_fields.bits.quantizer = seqLayerHeader->QUANTIZER;
897 	pic_parms->pic_quantizer_fields.bits.half_qp = picLayerHeader->HALFQP;
898 	pic_parms->pic_quantizer_fields.bits.pic_quantizer_scale = picLayerHeader->PQUANT;
899 	pic_parms->pic_quantizer_fields.bits.pic_quantizer_type = picLayerHeader->UniformQuant;
900 	pic_parms->pic_quantizer_fields.bits.dq_frame = picLayerHeader->DQUANTFRM;
901 	pic_parms->pic_quantizer_fields.bits.dq_profile = picLayerHeader->DQPROFILE;
902 	pic_parms->pic_quantizer_fields.bits.dq_sb_edge = picLayerHeader->DQSBEDGE;
903 	pic_parms->pic_quantizer_fields.bits.dq_db_edge = picLayerHeader->DQDBEDGE;
904 	pic_parms->pic_quantizer_fields.bits.dq_binary_level = picLayerHeader->DQBILEVEL;
905 	pic_parms->pic_quantizer_fields.bits.alt_pic_quantizer = picLayerHeader->ALTPQUANT;
906 
907 	pic_parms->transform_fields.value = 0;
908 	pic_parms->transform_fields.bits.variable_sized_transform_flag =
909 		seqLayerHeader->VSTRANSFORM;
910 
911 	pic_parms->transform_fields.bits.mb_level_transform_type_flag = picLayerHeader->TTMBF;
912 	pic_parms->transform_fields.bits.frame_level_transform_type = picLayerHeader->TTFRM;
913 
914 	pic_parms->transform_fields.bits.transform_ac_codingset_idx1 =
915 		(picLayerHeader->TRANSACFRM > 0) ? picLayerHeader->TRANSACFRM - 1 : 0;
916 
917 	pic_parms->transform_fields.bits.transform_ac_codingset_idx2 =
918 		(picLayerHeader->TRANSACFRM2 > 0) ? picLayerHeader->TRANSACFRM2 - 1 : 0;
919 
920 	pic_parms->transform_fields.bits.intra_transform_dc_table = picLayerHeader->TRANSDCTAB;
921 }
922 
923 
vbp_pack_slice_data_vc1(vbp_context * pcontext,int index,vbp_picture_data_vc1 * pic_data)924 static void vbp_pack_slice_data_vc1(
925 	vbp_context *pcontext,
926 	int index,
927 	vbp_picture_data_vc1* pic_data)
928 {
929 	viddec_pm_cxt_t *cxt = pcontext->parser_cxt;
930 	uint32 slice_size = cxt->list.data[index].edpos - cxt->list.data[index].stpos;
931 	uint32 bit;
932 	uint32 byte;
933 	uint8 is_emul;
934 	viddec_pm_get_au_pos(cxt, &bit, &byte, &is_emul);
935 
936 	vbp_slice_data_vc1 *slc_data = &(pic_data->slc_data[pic_data->num_slices]);
937 	VASliceParameterBufferVC1 *slc_parms = &(slc_data->slc_parms);
938 
939 	/*uint32 data_offset = byte - cxt->list.data[index].stpos;*/
940 
941 	slc_data->buffer_addr = cxt->parse_cubby.buf + cxt->list.data[index].stpos;
942 	slc_data->slice_size = slice_size - byte;
943 	slc_data->slice_offset = byte;
944 
945 	slc_parms->slice_data_size = slc_data->slice_size;
946 	slc_parms->slice_data_offset = 0;
947 
948 	/* fix this.  we need to be able to handle partial slices. */
949 	slc_parms->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
950 
951 	slc_parms->macroblock_offset = bit;
952 
953 	/* fix this.  we need o get the slice_vertical_position from the code */
954 	slc_parms->slice_vertical_position = pic_data->num_slices;
955 
956 	pic_data->num_slices++;
957 }
958 
959 /**
960  * process parsing result
961  */
vbp_process_parsing_result_vc1(vbp_context * pcontext,int index)962 uint32_t vbp_process_parsing_result_vc1(vbp_context *pcontext, int index)
963 {
964 	viddec_pm_cxt_t *cxt = pcontext->parser_cxt;
965 	uint32 error = VBP_OK;
966 
967 	vc1_viddec_parser_t *parser = (vc1_viddec_parser_t *)cxt->codec_data;
968 	if (parser->start_code != VC1_SC_FRM && parser->start_code != VC1_SC_FLD &&
969 		parser->start_code != VC1_SC_SLC)
970 	{
971 		/* only handle frame data, field data and slice data here
972 		 */
973 		return VBP_OK;
974 	}
975 	vbp_data_vc1 *query_data = (vbp_data_vc1 *)pcontext->query_data;
976 
977 	if (parser->start_code == VC1_SC_FRM || parser->start_code == VC1_SC_FLD)
978 	{
979 		query_data->num_pictures++;
980 	}
981 
982 	if (query_data->num_pictures > MAX_NUM_PICTURES)
983 	{
984 		ETRACE("Num of pictures per sample buffer exceeds the limit (%d).", MAX_NUM_PICTURES);
985 		return VBP_DATA;
986 	}
987 
988 	if (query_data->num_pictures == 0)
989 	{
990 		ETRACE("Unexpected num of pictures.");
991 		return VBP_DATA;
992 	}
993 
994 	/* start packing data */
995 	int picture_index = query_data->num_pictures - 1;
996 	vbp_picture_data_vc1* pic_data = &(query_data->pic_data[picture_index]);
997 
998 	if (parser->start_code == VC1_SC_FRM || parser->start_code == VC1_SC_FLD)
999 	{
1000 		/* setup picture parameter first*/
1001 		vbp_pack_picture_params_vc1(pcontext, index, pic_data);
1002 
1003 		/* setup bitplane after setting up picture parameter (so that bitplane_present is updated) */
1004 		error = vbp_pack_bitplanes_vc1(pcontext, index, pic_data);
1005 		if (VBP_OK != error)
1006 		{
1007 			ETRACE("Failed to pack bitplane.");
1008 			return error;
1009 		}
1010 
1011 	}
1012 
1013 	/* Always pack slice parameter. The first macroblock in the picture CANNOT
1014 	 * be preceeded by a slice header, so we will have first slice parsed always.
1015 	 *
1016 	 */
1017 
1018 	if (pic_data->num_slices >= MAX_NUM_SLICES)
1019 	{
1020 		ETRACE("Num of slices exceeds the limit (%d).", MAX_NUM_SLICES);
1021 		return VBP_DATA;
1022 	}
1023 
1024 	/* set up slice parameter */
1025 	vbp_pack_slice_data_vc1(pcontext, index, pic_data);
1026 
1027 
1028 	return VBP_OK;
1029 }
1030