Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / auxiliary / vl / vl_mpeg12_decoder.c
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 TUNGSTEN GRAPHICS 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_rect.h>
33 #include <util/u_video.h>
34
35 #include "vl_mpeg12_decoder.h"
36 #include "vl_defines.h"
37
38 #define SCALE_FACTOR_SNORM (32768.0f / 256.0f)
39 #define SCALE_FACTOR_SSCALED (1.0f / 256.0f)
40
41 static const enum pipe_format const_zscan_source_formats[] = {
42 PIPE_FORMAT_R16_SNORM,
43 PIPE_FORMAT_R16_SSCALED
44 };
45
46 static const unsigned num_zscan_source_formats =
47 sizeof(const_zscan_source_formats) / sizeof(enum pipe_format);
48
49 static const enum pipe_format const_idct_source_formats[] = {
50 PIPE_FORMAT_R16G16B16A16_SNORM,
51 PIPE_FORMAT_R16G16B16A16_SSCALED
52 };
53
54 static const unsigned num_idct_source_formats =
55 sizeof(const_idct_source_formats) / sizeof(enum pipe_format);
56
57 static const enum pipe_format const_idct_intermediate_formats[] = {
58 PIPE_FORMAT_R16G16B16A16_FLOAT,
59 PIPE_FORMAT_R16G16B16A16_SNORM,
60 PIPE_FORMAT_R16G16B16A16_SSCALED,
61 PIPE_FORMAT_R32G32B32A32_FLOAT
62 };
63
64 static const unsigned num_idct_intermediate_formats =
65 sizeof(const_idct_intermediate_formats) / sizeof(enum pipe_format);
66
67 static const enum pipe_format const_mc_source_formats[] = {
68 PIPE_FORMAT_R16_SNORM,
69 PIPE_FORMAT_R16_SSCALED
70 };
71
72 static const unsigned num_mc_source_formats =
73 sizeof(const_mc_source_formats) / sizeof(enum pipe_format);
74
75 static bool
76 init_zscan_buffer(struct vl_mpeg12_buffer *buffer)
77 {
78 enum pipe_format formats[3];
79
80 struct pipe_sampler_view **source;
81 struct pipe_surface **destination;
82
83 struct vl_mpeg12_decoder *dec;
84
85 unsigned i;
86
87 assert(buffer);
88
89 dec = (struct vl_mpeg12_decoder*)buffer->base.decoder;
90
91 formats[0] = formats[1] = formats[2] = dec->zscan_source_format;
92 buffer->zscan_source = vl_video_buffer_init(dec->base.context, dec->pipe,
93 dec->blocks_per_line * BLOCK_WIDTH * BLOCK_HEIGHT,
94 dec->max_blocks / dec->blocks_per_line,
95 1, PIPE_VIDEO_CHROMA_FORMAT_444,
96 formats, PIPE_USAGE_STATIC);
97 if (!buffer->zscan_source)
98 goto error_source;
99
100 source = buffer->zscan_source->get_sampler_views(buffer->zscan_source);
101 if (!source)
102 goto error_sampler;
103
104 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
105 destination = buffer->idct_source->get_surfaces(buffer->idct_source);
106 else
107 destination = buffer->mc_source->get_surfaces(buffer->mc_source);
108
109 if (!destination)
110 goto error_surface;
111
112 for (i = 0; i < VL_MAX_PLANES; ++i)
113 if (!vl_zscan_init_buffer(i == 0 ? &dec->zscan_y : &dec->zscan_c,
114 &buffer->zscan[i], source[i], destination[i]))
115 goto error_plane;
116
117 return true;
118
119 error_plane:
120 for (; i > 0; --i)
121 vl_zscan_cleanup_buffer(&buffer->zscan[i - 1]);
122
123 error_surface:
124 error_sampler:
125 buffer->zscan_source->destroy(buffer->zscan_source);
126
127 error_source:
128 return false;
129 }
130
131 static void
132 cleanup_zscan_buffer(struct vl_mpeg12_buffer *buffer)
133 {
134 unsigned i;
135
136 assert(buffer);
137
138 for (i = 0; i < VL_MAX_PLANES; ++i)
139 vl_zscan_cleanup_buffer(&buffer->zscan[i]);
140 buffer->zscan_source->destroy(buffer->zscan_source);
141 }
142
143 static bool
144 init_idct_buffer(struct vl_mpeg12_buffer *buffer)
145 {
146 enum pipe_format formats[3];
147
148 struct pipe_sampler_view **idct_source_sv, **idct_intermediate_sv;
149 struct pipe_surface **idct_surfaces;
150
151 struct vl_mpeg12_decoder *dec;
152
153 unsigned i;
154
155 assert(buffer);
156
157 dec = (struct vl_mpeg12_decoder*)buffer->base.decoder;
158
159 formats[0] = formats[1] = formats[2] = dec->idct_source_format;
160 buffer->idct_source = vl_video_buffer_init(dec->base.context, dec->pipe,
161 dec->base.width / 4, dec->base.height, 1,
162 dec->base.chroma_format,
163 formats, PIPE_USAGE_STATIC);
164 if (!buffer->idct_source)
165 goto error_source;
166
167 formats[0] = formats[1] = formats[2] = dec->idct_intermediate_format;
168 buffer->idct_intermediate = vl_video_buffer_init(dec->base.context, dec->pipe,
169 dec->base.width / dec->nr_of_idct_render_targets,
170 dec->base.height / 4, dec->nr_of_idct_render_targets,
171 dec->base.chroma_format,
172 formats, PIPE_USAGE_STATIC);
173
174 if (!buffer->idct_intermediate)
175 goto error_intermediate;
176
177 idct_source_sv = buffer->idct_source->get_sampler_views(buffer->idct_source);
178 if (!idct_source_sv)
179 goto error_source_sv;
180
181 idct_intermediate_sv = buffer->idct_intermediate->get_sampler_views(buffer->idct_intermediate);
182 if (!idct_intermediate_sv)
183 goto error_intermediate_sv;
184
185 idct_surfaces = buffer->mc_source->get_surfaces(buffer->mc_source);
186 if (!idct_surfaces)
187 goto error_surfaces;
188
189 for (i = 0; i < 3; ++i)
190 if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c,
191 &buffer->idct[i], idct_source_sv[i],
192 idct_intermediate_sv[i], idct_surfaces[i]))
193 goto error_plane;
194
195 return true;
196
197 error_plane:
198 for (; i > 0; --i)
199 vl_idct_cleanup_buffer(i == 1 ? &dec->idct_c : &dec->idct_y, &buffer->idct[i - 1]);
200
201 error_surfaces:
202 error_intermediate_sv:
203 error_source_sv:
204 buffer->idct_intermediate->destroy(buffer->idct_intermediate);
205
206 error_intermediate:
207 buffer->idct_source->destroy(buffer->idct_source);
208
209 error_source:
210 return false;
211 }
212
213 static void
214 cleanup_idct_buffer(struct vl_mpeg12_buffer *buf)
215 {
216 struct vl_mpeg12_decoder *dec;
217 assert(buf);
218
219 dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
220 assert(dec);
221
222 vl_idct_cleanup_buffer(&dec->idct_y, &buf->idct[0]);
223 vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[1]);
224 vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[2]);
225 buf->idct_source->destroy(buf->idct_source);
226 buf->idct_intermediate->destroy(buf->idct_intermediate);
227 }
228
229 static bool
230 init_mc_buffer(struct vl_mpeg12_buffer *buf)
231 {
232 struct vl_mpeg12_decoder *dec;
233 enum pipe_format formats[3];
234 struct pipe_sampler_view **mc_source_sv;
235
236 assert(buf);
237
238 dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
239 assert(dec);
240
241 formats[0] = formats[1] = formats[2] =dec->mc_source_format;
242 buf->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe,
243 dec->base.width, dec->base.height, 1,
244 dec->base.chroma_format,
245 formats, PIPE_USAGE_STATIC);
246
247 if (!buf->mc_source)
248 goto error_mc_source;
249
250 mc_source_sv = buf->mc_source->get_sampler_views(buf->mc_source);
251 if (!mc_source_sv)
252 goto error_mc_source_sv;
253
254 if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0], mc_source_sv[0]))
255 goto error_mc_y;
256
257 if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1], mc_source_sv[1]))
258 goto error_mc_cb;
259
260 if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2], mc_source_sv[2]))
261 goto error_mc_cr;
262
263 return true;
264
265 error_mc_cr:
266 vl_mc_cleanup_buffer(&buf->mc[1]);
267
268 error_mc_cb:
269 vl_mc_cleanup_buffer(&buf->mc[0]);
270
271 error_mc_y:
272 error_mc_source_sv:
273 buf->mc_source->destroy(buf->mc_source);
274
275 error_mc_source:
276 return false;
277 }
278
279 static void
280 cleanup_mc_buffer(struct vl_mpeg12_buffer *buf)
281 {
282 unsigned i;
283
284 assert(buf);
285
286 for (i = 0; i < VL_MAX_PLANES; ++i)
287 vl_mc_cleanup_buffer(&buf->mc[i]);
288
289 buf->mc_source->destroy(buf->mc_source);
290 }
291
292 static void
293 vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer *buffer)
294 {
295 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
296 struct vl_mpeg12_decoder *dec;
297
298 assert(buf);
299
300 dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
301 assert(dec);
302
303 cleanup_zscan_buffer(buf);
304
305 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
306 cleanup_idct_buffer(buf);
307
308 cleanup_mc_buffer(buf);
309
310 vl_vb_cleanup(&buf->vertex_stream);
311
312 FREE(buf);
313 }
314
315 static void
316 vl_mpeg12_buffer_map(struct pipe_video_decode_buffer *buffer)
317 {
318 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
319 struct vl_mpeg12_decoder *dec;
320
321 struct pipe_sampler_view **sampler_views;
322 unsigned i;
323
324 assert(buf);
325
326 dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
327 assert(dec);
328
329 vl_vb_map(&buf->vertex_stream, dec->pipe);
330
331 sampler_views = buf->zscan_source->get_sampler_views(buf->zscan_source);
332
333 assert(sampler_views);
334
335 for (i = 0; i < VL_MAX_PLANES; ++i) {
336 struct pipe_resource *tex = sampler_views[i]->texture;
337 struct pipe_box rect =
338 {
339 0, 0, 0,
340 tex->width0,
341 tex->height0,
342 1
343 };
344
345 buf->tex_transfer[i] = dec->pipe->get_transfer
346 (
347 dec->pipe, tex,
348 0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
349 &rect
350 );
351
352 buf->texels[i] = dec->pipe->transfer_map(dec->pipe, buf->tex_transfer[i]);
353 }
354 }
355
356 static struct pipe_ycbcr_block *
357 vl_mpeg12_buffer_get_ycbcr_stream(struct pipe_video_decode_buffer *buffer, int component)
358 {
359 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
360
361 assert(buf);
362
363 return vl_vb_get_ycbcr_stream(&buf->vertex_stream, component);
364 }
365
366 static short *
367 vl_mpeg12_buffer_get_ycbcr_buffer(struct pipe_video_decode_buffer *buffer, int component)
368 {
369 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
370
371 assert(buf);
372 assert(component < VL_MAX_PLANES);
373
374 return buf->texels[component];
375 }
376
377 static unsigned
378 vl_mpeg12_buffer_get_mv_stream_stride(struct pipe_video_decode_buffer *buffer)
379 {
380 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
381
382 assert(buf);
383
384 return vl_vb_get_mv_stream_stride(&buf->vertex_stream);
385 }
386
387 static struct pipe_motionvector *
388 vl_mpeg12_buffer_get_mv_stream(struct pipe_video_decode_buffer *buffer, int ref_frame)
389 {
390 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
391
392 assert(buf);
393
394 return vl_vb_get_mv_stream(&buf->vertex_stream, ref_frame);
395 }
396
397 static void
398 vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer *buffer)
399 {
400 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
401 struct vl_mpeg12_decoder *dec;
402 unsigned i;
403
404 assert(buf);
405
406 dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
407 assert(dec);
408
409 vl_vb_unmap(&buf->vertex_stream, dec->pipe);
410
411 for (i = 0; i < VL_MAX_PLANES; ++i) {
412 dec->pipe->transfer_unmap(dec->pipe, buf->tex_transfer[i]);
413 dec->pipe->transfer_destroy(dec->pipe, buf->tex_transfer[i]);
414 }
415 }
416
417 static void
418 vl_mpeg12_destroy(struct pipe_video_decoder *decoder)
419 {
420 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
421
422 assert(decoder);
423
424 /* Asserted in softpipe_delete_fs_state() for some reason */
425 dec->pipe->bind_vs_state(dec->pipe, NULL);
426 dec->pipe->bind_fs_state(dec->pipe, NULL);
427
428 dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa);
429
430 vl_mc_cleanup(&dec->mc_y);
431 vl_mc_cleanup(&dec->mc_c);
432
433 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
434 vl_idct_cleanup(&dec->idct_y);
435 vl_idct_cleanup(&dec->idct_c);
436 }
437
438 vl_zscan_cleanup(&dec->zscan_y);
439 vl_zscan_cleanup(&dec->zscan_c);
440
441 dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_ycbcr);
442 dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_mv);
443
444 pipe_resource_reference(&dec->quads.buffer, NULL);
445 pipe_resource_reference(&dec->pos.buffer, NULL);
446
447 FREE(dec);
448 }
449
450 static struct pipe_video_decode_buffer *
451 vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder)
452 {
453 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
454 struct vl_mpeg12_buffer *buffer;
455
456 assert(dec);
457
458 buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
459 if (buffer == NULL)
460 return NULL;
461
462 buffer->base.decoder = decoder;
463 buffer->base.destroy = vl_mpeg12_buffer_destroy;
464 buffer->base.map = vl_mpeg12_buffer_map;
465 buffer->base.get_ycbcr_stream = vl_mpeg12_buffer_get_ycbcr_stream;
466 buffer->base.get_ycbcr_buffer = vl_mpeg12_buffer_get_ycbcr_buffer;
467 buffer->base.get_mv_stream_stride = vl_mpeg12_buffer_get_mv_stream_stride;
468 buffer->base.get_mv_stream = vl_mpeg12_buffer_get_mv_stream;
469 buffer->base.unmap = vl_mpeg12_buffer_unmap;
470
471 if (!vl_vb_init(&buffer->vertex_stream, dec->pipe,
472 dec->base.width / MACROBLOCK_WIDTH,
473 dec->base.height / MACROBLOCK_HEIGHT))
474 goto error_vertex_buffer;
475
476 if (!init_mc_buffer(buffer))
477 goto error_mc;
478
479 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
480 if (!init_idct_buffer(buffer))
481 goto error_idct;
482
483 if (!init_zscan_buffer(buffer))
484 goto error_zscan;
485
486 return &buffer->base;
487
488 error_zscan:
489 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
490 cleanup_idct_buffer(buffer);
491
492 error_idct:
493 cleanup_mc_buffer(buffer);
494
495 error_mc:
496 vl_vb_cleanup(&buffer->vertex_stream);
497
498 error_vertex_buffer:
499 FREE(buffer);
500 return NULL;
501 }
502
503 static void
504 vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer,
505 unsigned num_ycbcr_blocks[3],
506 struct pipe_video_buffer *refs[2],
507 struct pipe_video_buffer *dst,
508 struct pipe_fence_handle **fence)
509 {
510 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
511 struct vl_mpeg12_decoder *dec;
512
513 struct pipe_sampler_view **sv[2];
514 struct pipe_surface **surfaces;
515
516 struct pipe_vertex_buffer vb[3];
517
518 unsigned i, j;
519
520 assert(buf);
521
522 dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
523 assert(dec);
524
525 for (i = 0; i < 2; ++i)
526 sv[i] = refs[i] ? refs[i]->get_sampler_views(refs[i]) : NULL;
527
528 surfaces = dst->get_surfaces(dst);
529
530 vb[0] = dec->quads;
531 vb[1] = dec->pos;
532
533 dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_mv);
534 for (i = 0; i < VL_MAX_PLANES; ++i) {
535 vl_mc_set_surface(&buf->mc[i], surfaces[i]);
536
537 for (j = 0; j < 2; ++j) {
538 if (sv[j] == NULL) continue;
539
540 vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);;
541 dec->pipe->set_vertex_buffers(dec->pipe, 3, vb);
542
543 vl_mc_render_ref(&buf->mc[i], sv[j][i]);
544 }
545 }
546
547 dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_ycbcr);
548 for (i = 0; i < VL_MAX_PLANES; ++i) {
549 if (num_ycbcr_blocks[i] == 0) continue;
550
551 vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);
552 dec->pipe->set_vertex_buffers(dec->pipe, 2, vb);
553
554 vl_zscan_render(&buf->zscan[i] , num_ycbcr_blocks[i]);
555
556 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
557 vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], num_ycbcr_blocks[i]);
558
559 vl_mc_render_ycbcr(&buf->mc[i], num_ycbcr_blocks[i]);
560 }
561
562 dec->pipe->flush(dec->pipe, fence);
563 }
564
565 static bool
566 init_pipe_state(struct vl_mpeg12_decoder *dec)
567 {
568 struct pipe_depth_stencil_alpha_state dsa;
569 unsigned i;
570
571 assert(dec);
572
573 memset(&dsa, 0, sizeof dsa);
574 dsa.depth.enabled = 0;
575 dsa.depth.writemask = 0;
576 dsa.depth.func = PIPE_FUNC_ALWAYS;
577 for (i = 0; i < 2; ++i) {
578 dsa.stencil[i].enabled = 0;
579 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
580 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
581 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
582 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
583 dsa.stencil[i].valuemask = 0;
584 dsa.stencil[i].writemask = 0;
585 }
586 dsa.alpha.enabled = 0;
587 dsa.alpha.func = PIPE_FUNC_ALWAYS;
588 dsa.alpha.ref_value = 0;
589 dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa);
590 dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa);
591
592 return true;
593 }
594
595 static enum pipe_format
596 find_first_supported_format(struct vl_mpeg12_decoder *dec,
597 const enum pipe_format formats[],
598 unsigned num_formats,
599 enum pipe_texture_target target)
600 {
601 struct pipe_screen *screen;
602 unsigned i;
603
604 assert(dec);
605
606 screen = dec->pipe->screen;
607
608 for (i = 0; i < num_formats; ++i)
609 if (screen->is_format_supported(dec->pipe->screen, formats[i], target, 1,
610 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
611 return formats[i];
612
613 return PIPE_FORMAT_NONE;
614 }
615
616 static bool
617 init_zscan(struct vl_mpeg12_decoder *dec)
618 {
619 struct pipe_sampler_view *layout;
620
621 unsigned num_channels;
622
623 assert(dec);
624
625 dec->blocks_per_line = 4;
626 dec->max_blocks =
627 (dec->base.width * dec->base.height) /
628 (BLOCK_WIDTH * BLOCK_HEIGHT);
629
630 dec->zscan_source_format = find_first_supported_format(dec, const_zscan_source_formats,
631 num_zscan_source_formats, PIPE_TEXTURE_2D);
632
633 if (dec->zscan_source_format == PIPE_FORMAT_NONE)
634 return false;
635
636 layout = vl_zscan_linear(dec->pipe, dec->blocks_per_line);
637
638 num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;
639
640 if (!vl_zscan_init(&dec->zscan_y, dec->pipe, dec->base.width, dec->base.height,
641 dec->blocks_per_line, dec->max_blocks, num_channels))
642 return false;
643
644 vl_zscan_set_layout(&dec->zscan_y, layout);
645
646 if (!vl_zscan_init(&dec->zscan_c, dec->pipe, dec->chroma_width, dec->chroma_height,
647 dec->blocks_per_line, dec->max_blocks, num_channels))
648 return false;
649
650 vl_zscan_set_layout(&dec->zscan_c, layout);
651
652 return true;
653 }
654
655 static bool
656 init_idct(struct vl_mpeg12_decoder *dec)
657 {
658 struct pipe_sampler_view *matrix, *transpose = NULL;
659 float matrix_scale, transpose_scale;
660
661 dec->nr_of_idct_render_targets = dec->pipe->screen->get_param(dec->pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS);
662
663 // more than 4 render targets usually doesn't makes any seens
664 dec->nr_of_idct_render_targets = MIN2(dec->nr_of_idct_render_targets, 4);
665
666 dec->idct_source_format = find_first_supported_format(dec, const_idct_source_formats,
667 num_idct_source_formats, PIPE_TEXTURE_2D);
668
669 if (dec->idct_source_format == PIPE_FORMAT_NONE)
670 return false;
671
672 dec->idct_intermediate_format = find_first_supported_format(dec, const_idct_intermediate_formats,
673 num_idct_intermediate_formats, PIPE_TEXTURE_3D);
674
675 if (dec->idct_intermediate_format == PIPE_FORMAT_NONE)
676 return false;
677
678 switch (dec->idct_source_format) {
679 case PIPE_FORMAT_R16G16B16A16_SSCALED:
680 matrix_scale = SCALE_FACTOR_SSCALED;
681 break;
682
683 case PIPE_FORMAT_R16G16B16A16_SNORM:
684 matrix_scale = SCALE_FACTOR_SNORM;
685 break;
686
687 default:
688 assert(0);
689 return false;
690 }
691
692 if (dec->idct_intermediate_format == PIPE_FORMAT_R16G16B16A16_FLOAT ||
693 dec->idct_intermediate_format == PIPE_FORMAT_R32G32B32A32_FLOAT)
694 transpose_scale = 1.0f;
695 else
696 transpose_scale = matrix_scale = sqrt(matrix_scale);
697
698 if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
699 transpose_scale /= SCALE_FACTOR_SSCALED;
700
701 if (!(matrix = vl_idct_upload_matrix(dec->pipe, matrix_scale)))
702 goto error_matrix;
703
704 if (matrix_scale != transpose_scale) {
705 if (!(transpose = vl_idct_upload_matrix(dec->pipe, transpose_scale)))
706 goto error_transpose;
707 } else
708 pipe_sampler_view_reference(&transpose, matrix);
709
710 if (!vl_idct_init(&dec->idct_y, dec->pipe, dec->base.width, dec->base.height,
711 dec->nr_of_idct_render_targets, matrix, transpose))
712 goto error_y;
713
714 if(!vl_idct_init(&dec->idct_c, dec->pipe, dec->chroma_width, dec->chroma_height,
715 dec->nr_of_idct_render_targets, matrix, transpose))
716 goto error_c;
717
718 pipe_sampler_view_reference(&matrix, NULL);
719 pipe_sampler_view_reference(&transpose, NULL);
720 return true;
721
722 error_c:
723 vl_idct_cleanup(&dec->idct_y);
724
725 error_y:
726 pipe_sampler_view_reference(&transpose, NULL);
727
728 error_transpose:
729 pipe_sampler_view_reference(&matrix, NULL);
730
731 error_matrix:
732 return false;
733 }
734
735 struct pipe_video_decoder *
736 vl_create_mpeg12_decoder(struct pipe_video_context *context,
737 struct pipe_context *pipe,
738 enum pipe_video_profile profile,
739 enum pipe_video_entrypoint entrypoint,
740 enum pipe_video_chroma_format chroma_format,
741 unsigned width, unsigned height)
742 {
743 struct vl_mpeg12_decoder *dec;
744 float mc_scale;
745
746 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
747
748 dec = CALLOC_STRUCT(vl_mpeg12_decoder);
749
750 if (!dec)
751 return NULL;
752
753 dec->base.context = context;
754 dec->base.profile = profile;
755 dec->base.entrypoint = entrypoint;
756 dec->base.chroma_format = chroma_format;
757 dec->base.width = width;
758 dec->base.height = height;
759
760 dec->base.destroy = vl_mpeg12_destroy;
761 dec->base.create_buffer = vl_mpeg12_create_buffer;
762 dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer;
763
764 dec->base.width = align(width, MACROBLOCK_WIDTH);
765 dec->base.height = align(height, MACROBLOCK_HEIGHT);
766
767 dec->pipe = pipe;
768
769 dec->quads = vl_vb_upload_quads(dec->pipe);
770 dec->pos = vl_vb_upload_pos(
771 dec->pipe,
772 dec->base.width / MACROBLOCK_WIDTH,
773 dec->base.height / MACROBLOCK_HEIGHT
774 );
775
776 dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->pipe);
777 dec->ves_mv = vl_vb_get_ves_mv(dec->pipe);
778
779 /* TODO: Implement 422, 444 */
780 assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
781
782 dec->mc_source_format = find_first_supported_format(dec, const_mc_source_formats,
783 num_mc_source_formats, PIPE_TEXTURE_3D);
784
785 if (dec->mc_source_format == PIPE_FORMAT_NONE)
786 return NULL;
787
788 if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
789 dec->chroma_width = dec->base.width / 2;
790 dec->chroma_height = dec->base.height / 2;
791 } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
792 dec->chroma_width = dec->base.width;
793 dec->chroma_height = dec->base.height / 2;
794 } else {
795 dec->chroma_width = dec->base.width;
796 dec->chroma_height = dec->base.height;
797 }
798
799 if (!init_zscan(dec))
800 goto error_zscan;
801
802 if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
803 if (!init_idct(dec))
804 goto error_idct;
805 if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
806 mc_scale = SCALE_FACTOR_SSCALED;
807 else
808 mc_scale = 1.0f;
809 } else {
810 switch (dec->mc_source_format) {
811 case PIPE_FORMAT_R16_SNORM:
812 mc_scale = SCALE_FACTOR_SNORM;
813 break;
814
815 case PIPE_FORMAT_R16_SSCALED:
816 mc_scale = SCALE_FACTOR_SSCALED;
817 break;
818
819 default:
820 assert(0);
821 return NULL;
822 }
823 }
824
825 if (!vl_mc_init(&dec->mc_y, dec->pipe, dec->base.width, dec->base.height, MACROBLOCK_HEIGHT, mc_scale))
826 goto error_mc_y;
827
828 // TODO
829 if (!vl_mc_init(&dec->mc_c, dec->pipe, dec->base.width, dec->base.height, BLOCK_HEIGHT, mc_scale))
830 goto error_mc_c;
831
832 if (!init_pipe_state(dec))
833 goto error_pipe_state;
834
835 return &dec->base;
836
837 error_pipe_state:
838 vl_mc_cleanup(&dec->mc_c);
839
840 error_mc_c:
841 vl_mc_cleanup(&dec->mc_y);
842
843 error_mc_y:
844 if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
845 vl_idct_cleanup(&dec->idct_y);
846 vl_idct_cleanup(&dec->idct_c);
847 }
848
849 error_idct:
850 vl_zscan_cleanup(&dec->zscan_y);
851 vl_zscan_cleanup(&dec->zscan_c);
852
853 error_zscan:
854 FREE(dec);
855 return NULL;
856 }