1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <math.h>
29 #include <assert.h>
30
31 #include "util/u_memory.h"
32 #include "util/u_sampler.h"
33 #include "util/u_surface.h"
34 #include "util/u_video.h"
35
36 #include "vl_mpeg12_decoder.h"
37 #include "vl_defines.h"
38
39 #define SCALE_FACTOR_SNORM (32768.0f / 256.0f)
40 #define SCALE_FACTOR_SSCALED (1.0f / 256.0f)
41
42 struct format_config {
43 enum pipe_format zscan_source_format;
44 enum pipe_format idct_source_format;
45 enum pipe_format mc_source_format;
46
47 float idct_scale;
48 float mc_scale;
49 };
50
51 static const struct format_config bitstream_format_config[] = {
52 // { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SSCALED },
53 // { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, 1.0f, SCALE_FACTOR_SSCALED },
54 { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SNORM },
55 { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, 1.0f, SCALE_FACTOR_SNORM }
56 };
57
58 static const unsigned num_bitstream_format_configs =
59 sizeof(bitstream_format_config) / sizeof(struct format_config);
60
61 static const struct format_config idct_format_config[] = {
62 // { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SSCALED },
63 // { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, 1.0f, SCALE_FACTOR_SSCALED },
64 { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SNORM },
65 { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, 1.0f, SCALE_FACTOR_SNORM }
66 };
67
68 static const unsigned num_idct_format_configs =
69 sizeof(idct_format_config) / sizeof(struct format_config);
70
71 static const struct format_config mc_format_config[] = {
72 //{ PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_NONE, PIPE_FORMAT_R16_SSCALED, 0.0f, SCALE_FACTOR_SSCALED },
73 { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_NONE, PIPE_FORMAT_R16_SNORM, 0.0f, SCALE_FACTOR_SNORM }
74 };
75
76 static const unsigned num_mc_format_configs =
77 sizeof(mc_format_config) / sizeof(struct format_config);
78
79 static const unsigned const_empty_block_mask_420[3][2][2] = {
80 { { 0x20, 0x10 }, { 0x08, 0x04 } },
81 { { 0x02, 0x02 }, { 0x02, 0x02 } },
82 { { 0x01, 0x01 }, { 0x01, 0x01 } }
83 };
84
85 struct video_buffer_private
86 {
87 struct list_head list;
88 struct pipe_video_buffer *video_buffer;
89
90 struct pipe_sampler_view *sampler_view_planes[VL_NUM_COMPONENTS];
91 struct pipe_surface *surfaces[VL_MAX_SURFACES];
92
93 struct vl_mpeg12_buffer *buffer;
94 };
95
96 static void
97 vl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer *buf);
98
99 static void
destroy_video_buffer_private(void * private)100 destroy_video_buffer_private(void *private)
101 {
102 struct video_buffer_private *priv = private;
103 unsigned i;
104
105 list_del(&priv->list);
106
107 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
108 pipe_sampler_view_reference(&priv->sampler_view_planes[i], NULL);
109
110 for (i = 0; i < VL_MAX_SURFACES; ++i)
111 pipe_surface_reference(&priv->surfaces[i], NULL);
112
113 if (priv->buffer)
114 vl_mpeg12_destroy_buffer(priv->buffer);
115
116 FREE(priv);
117 }
118
119 static struct video_buffer_private *
get_video_buffer_private(struct vl_mpeg12_decoder * dec,struct pipe_video_buffer * buf)120 get_video_buffer_private(struct vl_mpeg12_decoder *dec, struct pipe_video_buffer *buf)
121 {
122 struct pipe_context *pipe = dec->context;
123 struct video_buffer_private *priv;
124 struct pipe_sampler_view **sv;
125 struct pipe_surface **surf;
126 unsigned i;
127
128 priv = vl_video_buffer_get_associated_data(buf, &dec->base);
129 if (priv)
130 return priv;
131
132 priv = CALLOC_STRUCT(video_buffer_private);
133
134 list_add(&priv->list, &dec->buffer_privates);
135 priv->video_buffer = buf;
136
137 sv = buf->get_sampler_view_planes(buf);
138 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
139 if (sv[i])
140 priv->sampler_view_planes[i] = pipe->create_sampler_view(pipe, sv[i]->texture, sv[i]);
141
142 surf = buf->get_surfaces(buf);
143 for (i = 0; i < VL_MAX_SURFACES; ++i)
144 if (surf[i])
145 priv->surfaces[i] = pipe->create_surface(pipe, surf[i]->texture, surf[i]);
146
147 vl_video_buffer_set_associated_data(buf, &dec->base, priv, destroy_video_buffer_private);
148
149 return priv;
150 }
151
152 static void
free_video_buffer_privates(struct vl_mpeg12_decoder * dec)153 free_video_buffer_privates(struct vl_mpeg12_decoder *dec)
154 {
155 struct video_buffer_private *priv, *next;
156
157 LIST_FOR_EACH_ENTRY_SAFE(priv, next, &dec->buffer_privates, list) {
158 struct pipe_video_buffer *buf = priv->video_buffer;
159
160 vl_video_buffer_set_associated_data(buf, &dec->base, NULL, NULL);
161 }
162 }
163
164 static bool
init_zscan_buffer(struct vl_mpeg12_decoder * dec,struct vl_mpeg12_buffer * buffer)165 init_zscan_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)
166 {
167 struct pipe_resource *res, res_tmpl;
168 struct pipe_sampler_view sv_tmpl;
169 struct pipe_surface **destination;
170
171 unsigned i;
172
173 assert(dec && buffer);
174
175 memset(&res_tmpl, 0, sizeof(res_tmpl));
176 res_tmpl.target = PIPE_TEXTURE_2D;
177 res_tmpl.format = dec->zscan_source_format;
178 res_tmpl.width0 = dec->blocks_per_line * VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;
179 res_tmpl.height0 = align(dec->num_blocks, dec->blocks_per_line) / dec->blocks_per_line;
180 res_tmpl.depth0 = 1;
181 res_tmpl.array_size = 1;
182 res_tmpl.usage = PIPE_USAGE_STREAM;
183 res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW;
184
185 res = dec->context->screen->resource_create(dec->context->screen, &res_tmpl);
186 if (!res)
187 goto error_source;
188
189
190 memset(&sv_tmpl, 0, sizeof(sv_tmpl));
191 u_sampler_view_default_template(&sv_tmpl, res, res->format);
192 sv_tmpl.swizzle_r = sv_tmpl.swizzle_g = sv_tmpl.swizzle_b = sv_tmpl.swizzle_a = PIPE_SWIZZLE_X;
193 buffer->zscan_source = dec->context->create_sampler_view(dec->context, res, &sv_tmpl);
194 pipe_resource_reference(&res, NULL);
195 if (!buffer->zscan_source)
196 goto error_sampler;
197
198 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
199 destination = dec->idct_source->get_surfaces(dec->idct_source);
200 else
201 destination = dec->mc_source->get_surfaces(dec->mc_source);
202
203 if (!destination)
204 goto error_surface;
205
206 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
207 if (!vl_zscan_init_buffer(i == 0 ? &dec->zscan_y : &dec->zscan_c,
208 &buffer->zscan[i], buffer->zscan_source, destination[i]))
209 goto error_plane;
210
211 return true;
212
213 error_plane:
214 for (; i > 0; --i)
215 vl_zscan_cleanup_buffer(&buffer->zscan[i - 1]);
216
217 error_surface:
218 error_sampler:
219 pipe_sampler_view_reference(&buffer->zscan_source, NULL);
220
221 error_source:
222 return false;
223 }
224
225 static void
cleanup_zscan_buffer(struct vl_mpeg12_buffer * buffer)226 cleanup_zscan_buffer(struct vl_mpeg12_buffer *buffer)
227 {
228 unsigned i;
229
230 assert(buffer);
231
232 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
233 vl_zscan_cleanup_buffer(&buffer->zscan[i]);
234
235 pipe_sampler_view_reference(&buffer->zscan_source, NULL);
236 }
237
238 static bool
init_idct_buffer(struct vl_mpeg12_decoder * dec,struct vl_mpeg12_buffer * buffer)239 init_idct_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)
240 {
241 struct pipe_sampler_view **idct_source_sv, **mc_source_sv;
242
243 unsigned i;
244
245 assert(dec && buffer);
246
247 idct_source_sv = dec->idct_source->get_sampler_view_planes(dec->idct_source);
248 if (!idct_source_sv)
249 goto error_source_sv;
250
251 mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
252 if (!mc_source_sv)
253 goto error_mc_source_sv;
254
255 for (i = 0; i < 3; ++i)
256 if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c,
257 &buffer->idct[i], idct_source_sv[i],
258 mc_source_sv[i]))
259 goto error_plane;
260
261 return true;
262
263 error_plane:
264 for (; i > 0; --i)
265 vl_idct_cleanup_buffer(&buffer->idct[i - 1]);
266
267 error_mc_source_sv:
268 error_source_sv:
269 return false;
270 }
271
272 static void
cleanup_idct_buffer(struct vl_mpeg12_buffer * buf)273 cleanup_idct_buffer(struct vl_mpeg12_buffer *buf)
274 {
275 unsigned i;
276
277 assert(buf);
278
279 for (i = 0; i < 3; ++i)
280 vl_idct_cleanup_buffer(&buf->idct[i]);
281 }
282
283 static bool
init_mc_buffer(struct vl_mpeg12_decoder * dec,struct vl_mpeg12_buffer * buf)284 init_mc_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buf)
285 {
286 assert(dec && buf);
287
288 if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0]))
289 goto error_mc_y;
290
291 if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1]))
292 goto error_mc_cb;
293
294 if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2]))
295 goto error_mc_cr;
296
297 return true;
298
299 error_mc_cr:
300 vl_mc_cleanup_buffer(&buf->mc[1]);
301
302 error_mc_cb:
303 vl_mc_cleanup_buffer(&buf->mc[0]);
304
305 error_mc_y:
306 return false;
307 }
308
309 static void
cleanup_mc_buffer(struct vl_mpeg12_buffer * buf)310 cleanup_mc_buffer(struct vl_mpeg12_buffer *buf)
311 {
312 unsigned i;
313
314 assert(buf);
315
316 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
317 vl_mc_cleanup_buffer(&buf->mc[i]);
318 }
319
320 static inline void
MacroBlockTypeToPipeWeights(const struct pipe_mpeg12_macroblock * mb,unsigned weights[2])321 MacroBlockTypeToPipeWeights(const struct pipe_mpeg12_macroblock *mb, unsigned weights[2])
322 {
323 assert(mb);
324
325 switch (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {
326 case PIPE_MPEG12_MB_TYPE_MOTION_FORWARD:
327 weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;
328 weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
329 break;
330
331 case (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD):
332 weights[0] = PIPE_VIDEO_MV_WEIGHT_HALF;
333 weights[1] = PIPE_VIDEO_MV_WEIGHT_HALF;
334 break;
335
336 case PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD:
337 weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
338 weights[1] = PIPE_VIDEO_MV_WEIGHT_MAX;
339 break;
340
341 default:
342 if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) {
343 weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
344 weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
345 } else {
346 /* no motion vector, but also not intra mb ->
347 just copy the old frame content */
348 weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;
349 weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
350 }
351 break;
352 }
353 }
354
355 static inline struct vl_motionvector
MotionVectorToPipe(const struct pipe_mpeg12_macroblock * mb,unsigned vector,unsigned field_select_mask,unsigned weight)356 MotionVectorToPipe(const struct pipe_mpeg12_macroblock *mb, unsigned vector,
357 unsigned field_select_mask, unsigned weight)
358 {
359 struct vl_motionvector mv;
360
361 assert(mb);
362
363 if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {
364 switch (mb->macroblock_modes.bits.frame_motion_type) {
365 case PIPE_MPEG12_MO_TYPE_FRAME:
366 mv.top.x = mb->PMV[0][vector][0];
367 mv.top.y = mb->PMV[0][vector][1];
368 mv.top.field_select = PIPE_VIDEO_FRAME;
369 mv.top.weight = weight;
370
371 mv.bottom.x = mb->PMV[0][vector][0];
372 mv.bottom.y = mb->PMV[0][vector][1];
373 mv.bottom.weight = weight;
374 mv.bottom.field_select = PIPE_VIDEO_FRAME;
375 break;
376
377 case PIPE_MPEG12_MO_TYPE_FIELD:
378 mv.top.x = mb->PMV[0][vector][0];
379 mv.top.y = mb->PMV[0][vector][1];
380 mv.top.field_select = (mb->motion_vertical_field_select & field_select_mask) ?
381 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
382 mv.top.weight = weight;
383
384 mv.bottom.x = mb->PMV[1][vector][0];
385 mv.bottom.y = mb->PMV[1][vector][1];
386 mv.bottom.field_select = (mb->motion_vertical_field_select & (field_select_mask << 2)) ?
387 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
388 mv.bottom.weight = weight;
389 break;
390
391 default:
392 unreachable("TODO: Support DUALPRIME and 16x8");
393 }
394 } else {
395 mv.top.x = mv.top.y = 0;
396 mv.top.field_select = PIPE_VIDEO_FRAME;
397 mv.top.weight = weight;
398
399 mv.bottom.x = mv.bottom.y = 0;
400 mv.bottom.field_select = PIPE_VIDEO_FRAME;
401 mv.bottom.weight = weight;
402 }
403 return mv;
404 }
405
406 static inline void
UploadYcbcrBlocks(struct vl_mpeg12_decoder * dec,struct vl_mpeg12_buffer * buf,const struct pipe_mpeg12_macroblock * mb)407 UploadYcbcrBlocks(struct vl_mpeg12_decoder *dec,
408 struct vl_mpeg12_buffer *buf,
409 const struct pipe_mpeg12_macroblock *mb)
410 {
411 unsigned intra;
412 unsigned tb, x, y, num_blocks = 0;
413
414 assert(dec && buf);
415 assert(mb);
416
417 if (!mb->coded_block_pattern)
418 return;
419
420 intra = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA ? 1 : 0;
421
422 for (y = 0; y < 2; ++y) {
423 for (x = 0; x < 2; ++x) {
424 if (mb->coded_block_pattern & const_empty_block_mask_420[0][y][x]) {
425
426 struct vl_ycbcr_block *stream = buf->ycbcr_stream[0];
427 stream->x = mb->x * 2 + x;
428 stream->y = mb->y * 2 + y;
429 stream->intra = intra;
430 stream->coding = mb->macroblock_modes.bits.dct_type;
431 stream->block_num = buf->block_num++;
432
433 buf->num_ycbcr_blocks[0]++;
434 buf->ycbcr_stream[0]++;
435
436 num_blocks++;
437 }
438 }
439 }
440
441 /* TODO: Implement 422, 444 */
442 //assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
443
444 for (tb = 1; tb < 3; ++tb) {
445 if (mb->coded_block_pattern & const_empty_block_mask_420[tb][0][0]) {
446
447 struct vl_ycbcr_block *stream = buf->ycbcr_stream[tb];
448 stream->x = mb->x;
449 stream->y = mb->y;
450 stream->intra = intra;
451 stream->coding = 0;
452 stream->block_num = buf->block_num++;
453
454 buf->num_ycbcr_blocks[tb]++;
455 buf->ycbcr_stream[tb]++;
456
457 num_blocks++;
458 }
459 }
460
461 memcpy(buf->texels, mb->blocks, 64 * sizeof(short) * num_blocks);
462 buf->texels += 64 * num_blocks;
463 }
464
465 static void
vl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer * buf)466 vl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer *buf)
467 {
468
469 assert(buf);
470
471 cleanup_zscan_buffer(buf);
472 cleanup_idct_buffer(buf);
473 cleanup_mc_buffer(buf);
474 vl_vb_cleanup(&buf->vertex_stream);
475
476 FREE(buf);
477 }
478
479 static void
vl_mpeg12_destroy(struct pipe_video_codec * decoder)480 vl_mpeg12_destroy(struct pipe_video_codec *decoder)
481 {
482 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
483 unsigned i;
484
485 assert(decoder);
486
487 free_video_buffer_privates(dec);
488
489 /* Asserted in softpipe_delete_fs_state() for some reason */
490 dec->context->bind_vs_state(dec->context, NULL);
491 dec->context->bind_fs_state(dec->context, NULL);
492
493 dec->context->delete_depth_stencil_alpha_state(dec->context, dec->dsa);
494 dec->context->delete_sampler_state(dec->context, dec->sampler_ycbcr);
495
496 vl_mc_cleanup(&dec->mc_y);
497 vl_mc_cleanup(&dec->mc_c);
498 dec->mc_source->destroy(dec->mc_source);
499
500 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
501 vl_idct_cleanup(&dec->idct_y);
502 vl_idct_cleanup(&dec->idct_c);
503 dec->idct_source->destroy(dec->idct_source);
504 }
505
506 vl_zscan_cleanup(&dec->zscan_y);
507 vl_zscan_cleanup(&dec->zscan_c);
508
509 dec->context->delete_vertex_elements_state(dec->context, dec->ves_ycbcr);
510 dec->context->delete_vertex_elements_state(dec->context, dec->ves_mv);
511
512 pipe_resource_reference(&dec->quads.buffer.resource, NULL);
513 pipe_resource_reference(&dec->pos.buffer.resource, NULL);
514
515 pipe_sampler_view_reference(&dec->zscan_linear, NULL);
516 pipe_sampler_view_reference(&dec->zscan_normal, NULL);
517 pipe_sampler_view_reference(&dec->zscan_alternate, NULL);
518
519 for (i = 0; i < 4; ++i)
520 if (dec->dec_buffers[i])
521 vl_mpeg12_destroy_buffer(dec->dec_buffers[i]);
522
523 dec->context->destroy(dec->context);
524
525 FREE(dec);
526 }
527
528 static struct vl_mpeg12_buffer *
vl_mpeg12_get_decode_buffer(struct vl_mpeg12_decoder * dec,struct pipe_video_buffer * target)529 vl_mpeg12_get_decode_buffer(struct vl_mpeg12_decoder *dec, struct pipe_video_buffer *target)
530 {
531 struct video_buffer_private *priv;
532 struct vl_mpeg12_buffer *buffer;
533
534 assert(dec);
535
536 priv = get_video_buffer_private(dec, target);
537 if (priv->buffer)
538 return priv->buffer;
539
540 buffer = dec->dec_buffers[dec->current_buffer];
541 if (buffer)
542 return buffer;
543
544 buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
545 if (!buffer)
546 return NULL;
547
548 if (!vl_vb_init(&buffer->vertex_stream, dec->context,
549 dec->base.width / VL_MACROBLOCK_WIDTH,
550 dec->base.height / VL_MACROBLOCK_HEIGHT))
551 goto error_vertex_buffer;
552
553 if (!init_mc_buffer(dec, buffer))
554 goto error_mc;
555
556 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
557 if (!init_idct_buffer(dec, buffer))
558 goto error_idct;
559
560 if (!init_zscan_buffer(dec, buffer))
561 goto error_zscan;
562
563 if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
564 vl_mpg12_bs_init(&buffer->bs, &dec->base);
565
566 if (dec->base.expect_chunked_decode)
567 priv->buffer = buffer;
568 else
569 dec->dec_buffers[dec->current_buffer] = buffer;
570
571 return buffer;
572
573 error_zscan:
574 cleanup_idct_buffer(buffer);
575
576 error_idct:
577 cleanup_mc_buffer(buffer);
578
579 error_mc:
580 vl_vb_cleanup(&buffer->vertex_stream);
581
582 error_vertex_buffer:
583 FREE(buffer);
584 return NULL;
585 }
586
587 static void
vl_mpeg12_begin_frame(struct pipe_video_codec * decoder,struct pipe_video_buffer * target,struct pipe_picture_desc * picture)588 vl_mpeg12_begin_frame(struct pipe_video_codec *decoder,
589 struct pipe_video_buffer *target,
590 struct pipe_picture_desc *picture)
591 {
592 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
593 struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
594 struct vl_mpeg12_buffer *buf;
595
596 struct pipe_resource *tex;
597 struct pipe_box rect;
598 u_box_3d(0, 0, 0, 1, 1, 1, &rect);
599
600 uint8_t intra_matrix[64];
601 uint8_t non_intra_matrix[64];
602
603 unsigned i;
604
605 assert(dec && target && picture);
606
607 buf = vl_mpeg12_get_decode_buffer(dec, target);
608 assert(buf);
609
610 if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
611 memcpy(intra_matrix, desc->intra_matrix, sizeof(intra_matrix));
612 memcpy(non_intra_matrix, desc->non_intra_matrix, sizeof(non_intra_matrix));
613 intra_matrix[0] = 1 << (7 - desc->intra_dc_precision);
614 } else {
615 memset(intra_matrix, 0x10, sizeof(intra_matrix));
616 memset(non_intra_matrix, 0x10, sizeof(non_intra_matrix));
617 }
618
619 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
620 struct vl_zscan *zscan = i == 0 ? &dec->zscan_y : &dec->zscan_c;
621 vl_zscan_upload_quant(zscan, &buf->zscan[i], intra_matrix, true);
622 vl_zscan_upload_quant(zscan, &buf->zscan[i], non_intra_matrix, false);
623 }
624
625 vl_vb_map(&buf->vertex_stream, dec->context);
626
627 tex = buf->zscan_source->texture;
628 rect.width = tex->width0;
629 rect.height = tex->height0;
630
631 buf->texels =
632 dec->context->texture_map(dec->context, tex, 0,
633 PIPE_MAP_WRITE |
634 PIPE_MAP_DISCARD_RANGE,
635 &rect, &buf->tex_transfer);
636
637 buf->block_num = 0;
638
639 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
640 buf->ycbcr_stream[i] = vl_vb_get_ycbcr_stream(&buf->vertex_stream, i);
641 buf->num_ycbcr_blocks[i] = 0;
642 }
643
644 for (i = 0; i < VL_MAX_REF_FRAMES; ++i)
645 buf->mv_stream[i] = vl_vb_get_mv_stream(&buf->vertex_stream, i);
646
647 if (dec->base.entrypoint >= PIPE_VIDEO_ENTRYPOINT_IDCT) {
648 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
649 vl_zscan_set_layout(&buf->zscan[i], dec->zscan_linear);
650 }
651 }
652
653 static void
vl_mpeg12_decode_macroblock(struct pipe_video_codec * decoder,struct pipe_video_buffer * target,struct pipe_picture_desc * picture,const struct pipe_macroblock * macroblocks,unsigned num_macroblocks)654 vl_mpeg12_decode_macroblock(struct pipe_video_codec *decoder,
655 struct pipe_video_buffer *target,
656 struct pipe_picture_desc *picture,
657 const struct pipe_macroblock *macroblocks,
658 unsigned num_macroblocks)
659 {
660 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
661 const struct pipe_mpeg12_macroblock *mb = (const struct pipe_mpeg12_macroblock *)macroblocks;
662 struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
663 struct vl_mpeg12_buffer *buf;
664
665 unsigned i, j, mv_weights[2];
666
667 assert(dec && target && picture);
668 assert(macroblocks && macroblocks->codec == PIPE_VIDEO_FORMAT_MPEG12);
669
670 buf = vl_mpeg12_get_decode_buffer(dec, target);
671 assert(buf);
672
673 for (; num_macroblocks > 0; --num_macroblocks) {
674 unsigned mb_addr = mb->y * dec->width_in_macroblocks + mb->x;
675
676 if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_PATTERN | PIPE_MPEG12_MB_TYPE_INTRA))
677 UploadYcbcrBlocks(dec, buf, mb);
678
679 MacroBlockTypeToPipeWeights(mb, mv_weights);
680
681 for (i = 0; i < 2; ++i) {
682 if (!desc->ref[i]) continue;
683
684 buf->mv_stream[i][mb_addr] = MotionVectorToPipe
685 (
686 mb, i,
687 i ? PIPE_MPEG12_FS_FIRST_BACKWARD : PIPE_MPEG12_FS_FIRST_FORWARD,
688 mv_weights[i]
689 );
690 }
691
692 /* see section 7.6.6 of the spec */
693 if (mb->num_skipped_macroblocks > 0) {
694 struct vl_motionvector skipped_mv[2];
695
696 if (desc->ref[0] && !desc->ref[1]) {
697 skipped_mv[0].top.x = skipped_mv[0].top.y = 0;
698 skipped_mv[0].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
699 } else {
700 skipped_mv[0] = buf->mv_stream[0][mb_addr];
701 skipped_mv[1] = buf->mv_stream[1][mb_addr];
702 }
703 skipped_mv[0].top.field_select = PIPE_VIDEO_FRAME;
704 skipped_mv[1].top.field_select = PIPE_VIDEO_FRAME;
705
706 skipped_mv[0].bottom = skipped_mv[0].top;
707 skipped_mv[1].bottom = skipped_mv[1].top;
708
709 ++mb_addr;
710 for (i = 0; i < mb->num_skipped_macroblocks; ++i, ++mb_addr) {
711 for (j = 0; j < 2; ++j) {
712 if (!desc->ref[j]) continue;
713 buf->mv_stream[j][mb_addr] = skipped_mv[j];
714
715 }
716 }
717 }
718
719 ++mb;
720 }
721 }
722
723 static void
vl_mpeg12_decode_bitstream(struct pipe_video_codec * decoder,struct pipe_video_buffer * target,struct pipe_picture_desc * picture,unsigned num_buffers,const void * const * buffers,const unsigned * sizes)724 vl_mpeg12_decode_bitstream(struct pipe_video_codec *decoder,
725 struct pipe_video_buffer *target,
726 struct pipe_picture_desc *picture,
727 unsigned num_buffers,
728 const void * const *buffers,
729 const unsigned *sizes)
730 {
731 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
732 struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
733 struct vl_mpeg12_buffer *buf;
734
735 unsigned i;
736
737 assert(dec && target && picture);
738
739 buf = vl_mpeg12_get_decode_buffer(dec, target);
740 assert(buf);
741
742 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
743 vl_zscan_set_layout(&buf->zscan[i], desc->alternate_scan ?
744 dec->zscan_alternate : dec->zscan_normal);
745
746 vl_mpg12_bs_decode(&buf->bs, target, desc, num_buffers, buffers, sizes);
747 }
748
749 static int
vl_mpeg12_end_frame(struct pipe_video_codec * decoder,struct pipe_video_buffer * target,struct pipe_picture_desc * picture)750 vl_mpeg12_end_frame(struct pipe_video_codec *decoder,
751 struct pipe_video_buffer *target,
752 struct pipe_picture_desc *picture)
753 {
754 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
755 struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
756 struct pipe_sampler_view **ref_frames[2];
757 struct pipe_sampler_view **mc_source_sv;
758 struct pipe_surface **target_surfaces;
759 struct pipe_vertex_buffer vb[3];
760 struct vl_mpeg12_buffer *buf;
761
762 const unsigned *plane_order;
763 unsigned i, j, component;
764 unsigned nr_components;
765
766 assert(dec && target && picture);
767 assert(!target->interlaced);
768
769 buf = vl_mpeg12_get_decode_buffer(dec, target);
770
771 vl_vb_unmap(&buf->vertex_stream, dec->context);
772
773 if (buf->tex_transfer)
774 dec->context->texture_unmap(dec->context, buf->tex_transfer);
775
776 vb[0] = dec->quads;
777 vb[1] = dec->pos;
778
779 target_surfaces = get_video_buffer_private(dec, target)->surfaces;
780
781 for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
782 if (desc->ref[i])
783 ref_frames[i] = get_video_buffer_private(dec, desc->ref[i])->sampler_view_planes;
784 else
785 ref_frames[i] = NULL;
786 }
787
788 dec->context->bind_vertex_elements_state(dec->context, dec->ves_mv);
789 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
790 if (!target_surfaces[i]) continue;
791
792 vl_mc_set_surface(&buf->mc[i], target_surfaces[i]);
793
794 for (j = 0; j < VL_MAX_REF_FRAMES; ++j) {
795 if (!ref_frames[j] || !ref_frames[j][i]) continue;
796
797 vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);
798 util_set_vertex_buffers(dec->context, 3, false, vb);
799
800 vl_mc_render_ref(i ? &dec->mc_c : &dec->mc_y, &buf->mc[i], ref_frames[j][i]);
801 }
802 }
803
804 dec->context->bind_vertex_elements_state(dec->context, dec->ves_ycbcr);
805 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
806 if (!buf->num_ycbcr_blocks[i]) continue;
807
808 vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);
809 util_set_vertex_buffers(dec->context, 2, false, vb);
810
811 vl_zscan_render(i ? &dec->zscan_c : & dec->zscan_y, &buf->zscan[i] , buf->num_ycbcr_blocks[i]);
812
813 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
814 vl_idct_flush(i ? &dec->idct_c : &dec->idct_y, &buf->idct[i], buf->num_ycbcr_blocks[i]);
815 }
816
817 plane_order = vl_video_buffer_plane_order(target->buffer_format);
818 mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
819 for (i = 0, component = 0; component < VL_NUM_COMPONENTS; ++i) {
820 if (!target_surfaces[i]) continue;
821
822 nr_components = util_format_get_nr_components(target_surfaces[i]->texture->format);
823 for (j = 0; j < nr_components; ++j, ++component) {
824 unsigned plane = plane_order[component];
825 if (!buf->num_ycbcr_blocks[plane]) continue;
826
827 vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, plane);
828 util_set_vertex_buffers(dec->context, 2, false, vb);
829
830 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
831 vl_idct_prepare_stage2(i ? &dec->idct_c : &dec->idct_y, &buf->idct[plane]);
832 else {
833 dec->context->set_sampler_views(dec->context,
834 PIPE_SHADER_FRAGMENT, 0, 1, 0, false,
835 &mc_source_sv[plane]);
836 dec->context->bind_sampler_states(dec->context,
837 PIPE_SHADER_FRAGMENT,
838 0, 1, &dec->sampler_ycbcr);
839 }
840 vl_mc_render_ycbcr(i ? &dec->mc_c : &dec->mc_y, &buf->mc[i], j, buf->num_ycbcr_blocks[plane]);
841 }
842 }
843 dec->context->flush(dec->context, NULL, 0);
844 ++dec->current_buffer;
845 dec->current_buffer %= 4;
846 return 0;
847 }
848
849 static void
vl_mpeg12_flush(struct pipe_video_codec * decoder)850 vl_mpeg12_flush(struct pipe_video_codec *decoder)
851 {
852 assert(decoder);
853
854 //Noop, for shaders it is much faster to flush everything in end_frame
855 }
856
857 static bool
init_pipe_state(struct vl_mpeg12_decoder * dec)858 init_pipe_state(struct vl_mpeg12_decoder *dec)
859 {
860 struct pipe_depth_stencil_alpha_state dsa;
861 struct pipe_sampler_state sampler;
862 unsigned i;
863
864 assert(dec);
865
866 memset(&dsa, 0, sizeof dsa);
867 dsa.depth_enabled = 0;
868 dsa.depth_writemask = 0;
869 dsa.depth_func = PIPE_FUNC_ALWAYS;
870 for (i = 0; i < 2; ++i) {
871 dsa.stencil[i].enabled = 0;
872 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
873 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
874 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
875 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
876 dsa.stencil[i].valuemask = 0;
877 dsa.stencil[i].writemask = 0;
878 }
879 dsa.alpha_enabled = 0;
880 dsa.alpha_func = PIPE_FUNC_ALWAYS;
881 dsa.alpha_ref_value = 0;
882 dec->dsa = dec->context->create_depth_stencil_alpha_state(dec->context, &dsa);
883 dec->context->bind_depth_stencil_alpha_state(dec->context, dec->dsa);
884
885 memset(&sampler, 0, sizeof(sampler));
886 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
887 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
888 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
889 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
890 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
891 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
892 sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
893 sampler.compare_func = PIPE_FUNC_ALWAYS;
894 dec->sampler_ycbcr = dec->context->create_sampler_state(dec->context, &sampler);
895 if (!dec->sampler_ycbcr)
896 return false;
897
898 return true;
899 }
900
901 static const struct format_config*
find_format_config(struct vl_mpeg12_decoder * dec,const struct format_config configs[],unsigned num_configs)902 find_format_config(struct vl_mpeg12_decoder *dec, const struct format_config configs[], unsigned num_configs)
903 {
904 struct pipe_screen *screen;
905 unsigned i;
906
907 assert(dec);
908
909 screen = dec->context->screen;
910
911 for (i = 0; i < num_configs; ++i) {
912 if (!screen->is_format_supported(screen, configs[i].zscan_source_format, PIPE_TEXTURE_2D,
913 1, 1, PIPE_BIND_SAMPLER_VIEW))
914 continue;
915
916 if (configs[i].idct_source_format != PIPE_FORMAT_NONE) {
917 if (!screen->is_format_supported(screen, configs[i].idct_source_format, PIPE_TEXTURE_2D,
918 1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
919 continue;
920
921 if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_3D,
922 1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
923 continue;
924 } else {
925 if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_2D,
926 1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
927 continue;
928 }
929 return &configs[i];
930 }
931
932 return NULL;
933 }
934
935 static bool
init_zscan(struct vl_mpeg12_decoder * dec,const struct format_config * format_config)936 init_zscan(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
937 {
938 unsigned num_channels;
939
940 assert(dec);
941
942 dec->zscan_source_format = format_config->zscan_source_format;
943 dec->zscan_linear = vl_zscan_layout(dec->context, vl_zscan_linear, dec->blocks_per_line);
944 dec->zscan_normal = vl_zscan_layout(dec->context, vl_zscan_normal, dec->blocks_per_line);
945 dec->zscan_alternate = vl_zscan_layout(dec->context, vl_zscan_alternate, dec->blocks_per_line);
946
947 num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;
948
949 if (!vl_zscan_init(&dec->zscan_y, dec->context, dec->base.width, dec->base.height,
950 dec->blocks_per_line, dec->num_blocks, num_channels))
951 return false;
952
953 if (!vl_zscan_init(&dec->zscan_c, dec->context, dec->chroma_width, dec->chroma_height,
954 dec->blocks_per_line, dec->num_blocks, num_channels))
955 return false;
956
957 return true;
958 }
959
960 static bool
init_idct(struct vl_mpeg12_decoder * dec,const struct format_config * format_config)961 init_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
962 {
963 unsigned nr_of_idct_render_targets, max_inst;
964 enum pipe_format formats[3];
965 struct pipe_video_buffer templat;
966
967 struct pipe_sampler_view *matrix = NULL;
968
969 nr_of_idct_render_targets = dec->context->screen->get_param
970 (
971 dec->context->screen, PIPE_CAP_MAX_RENDER_TARGETS
972 );
973
974 max_inst = dec->context->screen->get_shader_param
975 (
976 dec->context->screen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_MAX_INSTRUCTIONS
977 );
978
979 // Just assume we need 32 inst per render target, not 100% true, but should work in most cases
980 if (nr_of_idct_render_targets >= 4 && max_inst >= 32*4)
981 // more than 4 render targets usually doesn't makes any seens
982 nr_of_idct_render_targets = 4;
983 else
984 nr_of_idct_render_targets = 1;
985
986 formats[0] = formats[1] = formats[2] = format_config->idct_source_format;
987 memset(&templat, 0, sizeof(templat));
988 templat.width = dec->base.width / 4;
989 templat.height = dec->base.height;
990 dec->idct_source = vl_video_buffer_create_ex
991 (
992 dec->context, &templat,
993 formats, 1, 1, PIPE_USAGE_DEFAULT,
994 PIPE_VIDEO_CHROMA_FORMAT_420
995 );
996
997 if (!dec->idct_source)
998 goto error_idct_source;
999
1000 formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
1001 memset(&templat, 0, sizeof(templat));
1002 templat.width = dec->base.width / nr_of_idct_render_targets;
1003 templat.height = dec->base.height / 4;
1004 dec->mc_source = vl_video_buffer_create_ex
1005 (
1006 dec->context, &templat,
1007 formats, nr_of_idct_render_targets, 1, PIPE_USAGE_DEFAULT,
1008 PIPE_VIDEO_CHROMA_FORMAT_420
1009 );
1010
1011 if (!dec->mc_source)
1012 goto error_mc_source;
1013
1014 if (!(matrix = vl_idct_upload_matrix(dec->context, format_config->idct_scale)))
1015 goto error_matrix;
1016
1017 if (!vl_idct_init(&dec->idct_y, dec->context, dec->base.width, dec->base.height,
1018 nr_of_idct_render_targets, matrix, matrix))
1019 goto error_y;
1020
1021 if(!vl_idct_init(&dec->idct_c, dec->context, dec->chroma_width, dec->chroma_height,
1022 nr_of_idct_render_targets, matrix, matrix))
1023 goto error_c;
1024
1025 pipe_sampler_view_reference(&matrix, NULL);
1026
1027 return true;
1028
1029 error_c:
1030 vl_idct_cleanup(&dec->idct_y);
1031
1032 error_y:
1033 pipe_sampler_view_reference(&matrix, NULL);
1034
1035 error_matrix:
1036 dec->mc_source->destroy(dec->mc_source);
1037
1038 error_mc_source:
1039 dec->idct_source->destroy(dec->idct_source);
1040
1041 error_idct_source:
1042 return false;
1043 }
1044
1045 static bool
init_mc_source_widthout_idct(struct vl_mpeg12_decoder * dec,const struct format_config * format_config)1046 init_mc_source_widthout_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
1047 {
1048 enum pipe_format formats[3];
1049 struct pipe_video_buffer templat;
1050
1051 formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
1052 assert(pipe_format_to_chroma_format(formats[0]) == dec->base.chroma_format);
1053 memset(&templat, 0, sizeof(templat));
1054 templat.width = dec->base.width;
1055 templat.height = dec->base.height;
1056 dec->mc_source = vl_video_buffer_create_ex
1057 (
1058 dec->context, &templat,
1059 formats, 1, 1, PIPE_USAGE_DEFAULT,
1060 PIPE_VIDEO_CHROMA_FORMAT_420
1061 );
1062
1063 return dec->mc_source != NULL;
1064 }
1065
1066 static void
mc_vert_shader_callback(void * priv,struct vl_mc * mc,struct ureg_program * shader,unsigned first_output,struct ureg_dst tex)1067 mc_vert_shader_callback(void *priv, struct vl_mc *mc,
1068 struct ureg_program *shader,
1069 unsigned first_output,
1070 struct ureg_dst tex)
1071 {
1072 struct vl_mpeg12_decoder *dec = priv;
1073 struct ureg_dst o_vtex;
1074
1075 assert(priv && mc);
1076 assert(shader);
1077
1078 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1079 struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
1080 vl_idct_stage2_vert_shader(idct, shader, first_output, tex);
1081 } else {
1082 o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output);
1083 ureg_MOV(shader, ureg_writemask(o_vtex, TGSI_WRITEMASK_XY), ureg_src(tex));
1084 }
1085 }
1086
1087 static void
mc_frag_shader_callback(void * priv,struct vl_mc * mc,struct ureg_program * shader,unsigned first_input,struct ureg_dst dst)1088 mc_frag_shader_callback(void *priv, struct vl_mc *mc,
1089 struct ureg_program *shader,
1090 unsigned first_input,
1091 struct ureg_dst dst)
1092 {
1093 struct vl_mpeg12_decoder *dec = priv;
1094 struct ureg_src src, sampler;
1095
1096 assert(priv && mc);
1097 assert(shader);
1098
1099 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1100 struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
1101 vl_idct_stage2_frag_shader(idct, shader, first_input, dst);
1102 } else {
1103 src = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, first_input, TGSI_INTERPOLATE_LINEAR);
1104 sampler = ureg_DECL_sampler(shader, 0);
1105 ureg_TEX(shader, dst, TGSI_TEXTURE_2D, src, sampler);
1106 }
1107 }
1108
1109 struct pipe_video_codec *
vl_create_mpeg12_decoder(struct pipe_context * context,const struct pipe_video_codec * templat)1110 vl_create_mpeg12_decoder(struct pipe_context *context,
1111 const struct pipe_video_codec *templat)
1112 {
1113 const unsigned block_size_pixels = VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;
1114 const struct format_config *format_config;
1115 struct vl_mpeg12_decoder *dec;
1116
1117 assert(u_reduce_video_profile(templat->profile) == PIPE_VIDEO_FORMAT_MPEG12);
1118
1119 dec = CALLOC_STRUCT(vl_mpeg12_decoder);
1120
1121 if (!dec)
1122 return NULL;
1123
1124 dec->base = *templat;
1125 dec->base.context = context;
1126 dec->context = pipe_create_multimedia_context(context->screen);
1127
1128 dec->base.destroy = vl_mpeg12_destroy;
1129 dec->base.begin_frame = vl_mpeg12_begin_frame;
1130 dec->base.decode_macroblock = vl_mpeg12_decode_macroblock;
1131 dec->base.decode_bitstream = vl_mpeg12_decode_bitstream;
1132 dec->base.end_frame = vl_mpeg12_end_frame;
1133 dec->base.flush = vl_mpeg12_flush;
1134
1135 dec->blocks_per_line = MAX2(util_next_power_of_two(dec->base.width) / block_size_pixels, 4);
1136 dec->num_blocks = (dec->base.width * dec->base.height) / block_size_pixels;
1137 dec->width_in_macroblocks = align(dec->base.width, VL_MACROBLOCK_WIDTH) / VL_MACROBLOCK_WIDTH;
1138
1139 /* TODO: Implement 422, 444 */
1140 assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
1141
1142 if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
1143 dec->chroma_width = dec->base.width / 2;
1144 dec->chroma_height = dec->base.height / 2;
1145 dec->num_blocks = dec->num_blocks * 2;
1146 } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
1147 dec->chroma_width = dec->base.width / 2;
1148 dec->chroma_height = dec->base.height;
1149 dec->num_blocks = dec->num_blocks * 2 + dec->num_blocks;
1150 } else {
1151 dec->chroma_width = dec->base.width;
1152 dec->chroma_height = dec->base.height;
1153 dec->num_blocks = dec->num_blocks * 3;
1154 }
1155
1156 dec->quads = vl_vb_upload_quads(dec->context);
1157 dec->pos = vl_vb_upload_pos(
1158 dec->context,
1159 dec->base.width / VL_MACROBLOCK_WIDTH,
1160 dec->base.height / VL_MACROBLOCK_HEIGHT
1161 );
1162
1163 dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->context);
1164 dec->ves_mv = vl_vb_get_ves_mv(dec->context);
1165
1166 switch (templat->entrypoint) {
1167 case PIPE_VIDEO_ENTRYPOINT_BITSTREAM:
1168 format_config = find_format_config(dec, bitstream_format_config, num_bitstream_format_configs);
1169 break;
1170
1171 case PIPE_VIDEO_ENTRYPOINT_IDCT:
1172 format_config = find_format_config(dec, idct_format_config, num_idct_format_configs);
1173 break;
1174
1175 case PIPE_VIDEO_ENTRYPOINT_MC:
1176 format_config = find_format_config(dec, mc_format_config, num_mc_format_configs);
1177 break;
1178
1179 default:
1180 assert(0);
1181 FREE(dec);
1182 return NULL;
1183 }
1184
1185 if (!format_config) {
1186 FREE(dec);
1187 return NULL;
1188 }
1189
1190 if (!init_zscan(dec, format_config))
1191 goto error_zscan;
1192
1193 if (templat->entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1194 if (!init_idct(dec, format_config))
1195 goto error_sources;
1196 } else {
1197 if (!init_mc_source_widthout_idct(dec, format_config))
1198 goto error_sources;
1199 }
1200
1201 if (!vl_mc_init(&dec->mc_y, dec->context, dec->base.width, dec->base.height,
1202 VL_MACROBLOCK_HEIGHT, format_config->mc_scale,
1203 mc_vert_shader_callback, mc_frag_shader_callback, dec))
1204 goto error_mc_y;
1205
1206 // TODO
1207 if (!vl_mc_init(&dec->mc_c, dec->context, dec->base.width, dec->base.height,
1208 VL_BLOCK_HEIGHT, format_config->mc_scale,
1209 mc_vert_shader_callback, mc_frag_shader_callback, dec))
1210 goto error_mc_c;
1211
1212 if (!init_pipe_state(dec))
1213 goto error_pipe_state;
1214
1215 list_inithead(&dec->buffer_privates);
1216
1217 return &dec->base;
1218
1219 error_pipe_state:
1220 vl_mc_cleanup(&dec->mc_c);
1221
1222 error_mc_c:
1223 vl_mc_cleanup(&dec->mc_y);
1224
1225 error_mc_y:
1226 if (templat->entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1227 vl_idct_cleanup(&dec->idct_y);
1228 vl_idct_cleanup(&dec->idct_c);
1229 dec->idct_source->destroy(dec->idct_source);
1230 }
1231 dec->mc_source->destroy(dec->mc_source);
1232
1233 error_sources:
1234 vl_zscan_cleanup(&dec->zscan_y);
1235 vl_zscan_cleanup(&dec->zscan_c);
1236
1237 error_zscan:
1238 FREE(dec);
1239 return NULL;
1240 }
1241