1
/**************************************************************************
3
* Copyright 2009 Younes Manton.
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:
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
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.
26
**************************************************************************/
31
#include "util/u_memory.h"
32
#include "util/u_sampler.h"
33
#include "util/u_surface.h"
34
#include "util/u_video.h"
36
#include "vl_mpeg12_decoder.h"
37
#include "vl_defines.h"
39
#define SCALE_FACTOR_SNORM (32768.0f / 256.0f)
40
#define SCALE_FACTOR_SSCALED (1.0f / 256.0f)
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;
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 }
58
static const unsigned num_bitstream_format_configs =
59
sizeof(bitstream_format_config) / sizeof(struct format_config);
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 }
68
static const unsigned num_idct_format_configs =
69
sizeof(idct_format_config) / sizeof(struct format_config);
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 }
76
static const unsigned num_mc_format_configs =
77
sizeof(mc_format_config) / sizeof(struct format_config);
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 } }
85
struct video_buffer_private
87
struct list_head list;
88
struct pipe_video_buffer *video_buffer;
90
struct pipe_sampler_view *sampler_view_planes[VL_NUM_COMPONENTS];
91
struct pipe_surface *surfaces[VL_MAX_SURFACES];
93
struct vl_mpeg12_buffer *buffer;
97
vl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer *buf);
100
destroy_video_buffer_private(void *private)
102
struct video_buffer_private *priv = private;
105
list_del(&priv->list);
107
for (i = 0; i < VL_NUM_COMPONENTS; ++i)
108
pipe_sampler_view_reference(&priv->sampler_view_planes[i], NULL);
110
for (i = 0; i < VL_MAX_SURFACES; ++i)
111
pipe_surface_reference(&priv->surfaces[i], NULL);
114
vl_mpeg12_destroy_buffer(priv->buffer);
119
static struct video_buffer_private *
120
get_video_buffer_private(struct vl_mpeg12_decoder *dec, struct pipe_video_buffer *buf)
122
struct pipe_context *pipe = dec->context;
123
struct video_buffer_private *priv;
124
struct pipe_sampler_view **sv;
125
struct pipe_surface **surf;
128
priv = vl_video_buffer_get_associated_data(buf, &dec->base);
132
priv = CALLOC_STRUCT(video_buffer_private);
134
list_add(&priv->list, &dec->buffer_privates);
135
priv->video_buffer = buf;
137
sv = buf->get_sampler_view_planes(buf);
138
for (i = 0; i < VL_NUM_COMPONENTS; ++i)
140
priv->sampler_view_planes[i] = pipe->create_sampler_view(pipe, sv[i]->texture, sv[i]);
142
surf = buf->get_surfaces(buf);
143
for (i = 0; i < VL_MAX_SURFACES; ++i)
145
priv->surfaces[i] = pipe->create_surface(pipe, surf[i]->texture, surf[i]);
147
vl_video_buffer_set_associated_data(buf, &dec->base, priv, destroy_video_buffer_private);
153
free_video_buffer_privates(struct vl_mpeg12_decoder *dec)
155
struct video_buffer_private *priv, *next;
157
LIST_FOR_EACH_ENTRY_SAFE(priv, next, &dec->buffer_privates, list) {
158
struct pipe_video_buffer *buf = priv->video_buffer;
160
vl_video_buffer_set_associated_data(buf, &dec->base, NULL, NULL);
165
init_zscan_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)
167
struct pipe_resource *res, res_tmpl;
168
struct pipe_sampler_view sv_tmpl;
169
struct pipe_surface **destination;
173
assert(dec && buffer);
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;
181
res_tmpl.array_size = 1;
182
res_tmpl.usage = PIPE_USAGE_STREAM;
183
res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW;
185
res = dec->context->screen->resource_create(dec->context->screen, &res_tmpl);
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)
198
if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
199
destination = dec->idct_source->get_surfaces(dec->idct_source);
201
destination = dec->mc_source->get_surfaces(dec->mc_source);
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]))
215
vl_zscan_cleanup_buffer(&buffer->zscan[i - 1]);
219
pipe_sampler_view_reference(&buffer->zscan_source, NULL);
226
cleanup_zscan_buffer(struct vl_mpeg12_buffer *buffer)
232
for (i = 0; i < VL_NUM_COMPONENTS; ++i)
233
vl_zscan_cleanup_buffer(&buffer->zscan[i]);
235
pipe_sampler_view_reference(&buffer->zscan_source, NULL);
239
init_idct_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)
241
struct pipe_sampler_view **idct_source_sv, **mc_source_sv;
245
assert(dec && buffer);
247
idct_source_sv = dec->idct_source->get_sampler_view_planes(dec->idct_source);
249
goto error_source_sv;
251
mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
253
goto error_mc_source_sv;
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],
265
vl_idct_cleanup_buffer(&buffer->idct[i - 1]);
273
cleanup_idct_buffer(struct vl_mpeg12_buffer *buf)
279
for (i = 0; i < 3; ++i)
280
vl_idct_cleanup_buffer(&buf->idct[i]);
284
init_mc_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buf)
288
if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0]))
291
if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1]))
294
if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2]))
300
vl_mc_cleanup_buffer(&buf->mc[1]);
303
vl_mc_cleanup_buffer(&buf->mc[0]);
310
cleanup_mc_buffer(struct vl_mpeg12_buffer *buf)
316
for (i = 0; i < VL_NUM_COMPONENTS; ++i)
317
vl_mc_cleanup_buffer(&buf->mc[i]);
321
MacroBlockTypeToPipeWeights(const struct pipe_mpeg12_macroblock *mb, unsigned weights[2])
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;
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;
336
case PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD:
337
weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
338
weights[1] = PIPE_VIDEO_MV_WEIGHT_MAX;
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;
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;
355
static inline struct vl_motionvector
356
MotionVectorToPipe(const struct pipe_mpeg12_macroblock *mb, unsigned vector,
357
unsigned field_select_mask, unsigned weight)
359
struct vl_motionvector mv;
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;
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;
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;
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;
392
unreachable("TODO: Support DUALPRIME and 16x8");
395
mv.top.x = mv.top.y = 0;
396
mv.top.field_select = PIPE_VIDEO_FRAME;
397
mv.top.weight = weight;
399
mv.bottom.x = mv.bottom.y = 0;
400
mv.bottom.field_select = PIPE_VIDEO_FRAME;
401
mv.bottom.weight = weight;
407
UploadYcbcrBlocks(struct vl_mpeg12_decoder *dec,
408
struct vl_mpeg12_buffer *buf,
409
const struct pipe_mpeg12_macroblock *mb)
412
unsigned tb, x, y, num_blocks = 0;
417
if (!mb->coded_block_pattern)
420
intra = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA ? 1 : 0;
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]) {
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++;
433
buf->num_ycbcr_blocks[0]++;
434
buf->ycbcr_stream[0]++;
441
/* TODO: Implement 422, 444 */
442
//assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
444
for (tb = 1; tb < 3; ++tb) {
445
if (mb->coded_block_pattern & const_empty_block_mask_420[tb][0][0]) {
447
struct vl_ycbcr_block *stream = buf->ycbcr_stream[tb];
450
stream->intra = intra;
452
stream->block_num = buf->block_num++;
454
buf->num_ycbcr_blocks[tb]++;
455
buf->ycbcr_stream[tb]++;
461
memcpy(buf->texels, mb->blocks, 64 * sizeof(short) * num_blocks);
462
buf->texels += 64 * num_blocks;
466
vl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer *buf)
471
cleanup_zscan_buffer(buf);
472
cleanup_idct_buffer(buf);
473
cleanup_mc_buffer(buf);
474
vl_vb_cleanup(&buf->vertex_stream);
480
vl_mpeg12_destroy(struct pipe_video_codec *decoder)
482
struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
487
free_video_buffer_privates(dec);
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);
493
dec->context->delete_depth_stencil_alpha_state(dec->context, dec->dsa);
494
dec->context->delete_sampler_state(dec->context, dec->sampler_ycbcr);
496
vl_mc_cleanup(&dec->mc_y);
497
vl_mc_cleanup(&dec->mc_c);
498
dec->mc_source->destroy(dec->mc_source);
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);
506
vl_zscan_cleanup(&dec->zscan_y);
507
vl_zscan_cleanup(&dec->zscan_c);
509
dec->context->delete_vertex_elements_state(dec->context, dec->ves_ycbcr);
510
dec->context->delete_vertex_elements_state(dec->context, dec->ves_mv);
512
pipe_resource_reference(&dec->quads.buffer.resource, NULL);
513
pipe_resource_reference(&dec->pos.buffer.resource, NULL);
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);
519
for (i = 0; i < 4; ++i)
520
if (dec->dec_buffers[i])
521
vl_mpeg12_destroy_buffer(dec->dec_buffers[i]);
523
dec->context->destroy(dec->context);
528
static struct vl_mpeg12_buffer *
529
vl_mpeg12_get_decode_buffer(struct vl_mpeg12_decoder *dec, struct pipe_video_buffer *target)
531
struct video_buffer_private *priv;
532
struct vl_mpeg12_buffer *buffer;
536
priv = get_video_buffer_private(dec, target);
540
buffer = dec->dec_buffers[dec->current_buffer];
544
buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
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;
553
if (!init_mc_buffer(dec, buffer))
556
if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
557
if (!init_idct_buffer(dec, buffer))
560
if (!init_zscan_buffer(dec, buffer))
563
if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
564
vl_mpg12_bs_init(&buffer->bs, &dec->base);
566
if (dec->base.expect_chunked_decode)
567
priv->buffer = buffer;
569
dec->dec_buffers[dec->current_buffer] = buffer;
574
cleanup_idct_buffer(buffer);
577
cleanup_mc_buffer(buffer);
580
vl_vb_cleanup(&buffer->vertex_stream);
588
vl_mpeg12_begin_frame(struct pipe_video_codec *decoder,
589
struct pipe_video_buffer *target,
590
struct pipe_picture_desc *picture)
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;
596
struct pipe_resource *tex;
597
struct pipe_box rect = { 0, 0, 0, 1, 1, 1 };
599
uint8_t intra_matrix[64];
600
uint8_t non_intra_matrix[64];
604
assert(dec && target && picture);
606
buf = vl_mpeg12_get_decode_buffer(dec, target);
609
if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
610
memcpy(intra_matrix, desc->intra_matrix, sizeof(intra_matrix));
611
memcpy(non_intra_matrix, desc->non_intra_matrix, sizeof(non_intra_matrix));
612
intra_matrix[0] = 1 << (7 - desc->intra_dc_precision);
614
memset(intra_matrix, 0x10, sizeof(intra_matrix));
615
memset(non_intra_matrix, 0x10, sizeof(non_intra_matrix));
618
for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
619
struct vl_zscan *zscan = i == 0 ? &dec->zscan_y : &dec->zscan_c;
620
vl_zscan_upload_quant(zscan, &buf->zscan[i], intra_matrix, true);
621
vl_zscan_upload_quant(zscan, &buf->zscan[i], non_intra_matrix, false);
624
vl_vb_map(&buf->vertex_stream, dec->context);
626
tex = buf->zscan_source->texture;
627
rect.width = tex->width0;
628
rect.height = tex->height0;
631
dec->context->texture_map(dec->context, tex, 0,
633
PIPE_MAP_DISCARD_RANGE,
634
&rect, &buf->tex_transfer);
638
for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
639
buf->ycbcr_stream[i] = vl_vb_get_ycbcr_stream(&buf->vertex_stream, i);
640
buf->num_ycbcr_blocks[i] = 0;
643
for (i = 0; i < VL_MAX_REF_FRAMES; ++i)
644
buf->mv_stream[i] = vl_vb_get_mv_stream(&buf->vertex_stream, i);
646
if (dec->base.entrypoint >= PIPE_VIDEO_ENTRYPOINT_IDCT) {
647
for (i = 0; i < VL_NUM_COMPONENTS; ++i)
648
vl_zscan_set_layout(&buf->zscan[i], dec->zscan_linear);
653
vl_mpeg12_decode_macroblock(struct pipe_video_codec *decoder,
654
struct pipe_video_buffer *target,
655
struct pipe_picture_desc *picture,
656
const struct pipe_macroblock *macroblocks,
657
unsigned num_macroblocks)
659
struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
660
const struct pipe_mpeg12_macroblock *mb = (const struct pipe_mpeg12_macroblock *)macroblocks;
661
struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
662
struct vl_mpeg12_buffer *buf;
664
unsigned i, j, mv_weights[2];
666
assert(dec && target && picture);
667
assert(macroblocks && macroblocks->codec == PIPE_VIDEO_FORMAT_MPEG12);
669
buf = vl_mpeg12_get_decode_buffer(dec, target);
672
for (; num_macroblocks > 0; --num_macroblocks) {
673
unsigned mb_addr = mb->y * dec->width_in_macroblocks + mb->x;
675
if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_PATTERN | PIPE_MPEG12_MB_TYPE_INTRA))
676
UploadYcbcrBlocks(dec, buf, mb);
678
MacroBlockTypeToPipeWeights(mb, mv_weights);
680
for (i = 0; i < 2; ++i) {
681
if (!desc->ref[i]) continue;
683
buf->mv_stream[i][mb_addr] = MotionVectorToPipe
686
i ? PIPE_MPEG12_FS_FIRST_BACKWARD : PIPE_MPEG12_FS_FIRST_FORWARD,
691
/* see section 7.6.6 of the spec */
692
if (mb->num_skipped_macroblocks > 0) {
693
struct vl_motionvector skipped_mv[2];
695
if (desc->ref[0] && !desc->ref[1]) {
696
skipped_mv[0].top.x = skipped_mv[0].top.y = 0;
697
skipped_mv[0].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
699
skipped_mv[0] = buf->mv_stream[0][mb_addr];
700
skipped_mv[1] = buf->mv_stream[1][mb_addr];
702
skipped_mv[0].top.field_select = PIPE_VIDEO_FRAME;
703
skipped_mv[1].top.field_select = PIPE_VIDEO_FRAME;
705
skipped_mv[0].bottom = skipped_mv[0].top;
706
skipped_mv[1].bottom = skipped_mv[1].top;
709
for (i = 0; i < mb->num_skipped_macroblocks; ++i, ++mb_addr) {
710
for (j = 0; j < 2; ++j) {
711
if (!desc->ref[j]) continue;
712
buf->mv_stream[j][mb_addr] = skipped_mv[j];
723
vl_mpeg12_decode_bitstream(struct pipe_video_codec *decoder,
724
struct pipe_video_buffer *target,
725
struct pipe_picture_desc *picture,
726
unsigned num_buffers,
727
const void * const *buffers,
728
const unsigned *sizes)
730
struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
731
struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
732
struct vl_mpeg12_buffer *buf;
736
assert(dec && target && picture);
738
buf = vl_mpeg12_get_decode_buffer(dec, target);
741
for (i = 0; i < VL_NUM_COMPONENTS; ++i)
742
vl_zscan_set_layout(&buf->zscan[i], desc->alternate_scan ?
743
dec->zscan_alternate : dec->zscan_normal);
745
vl_mpg12_bs_decode(&buf->bs, target, desc, num_buffers, buffers, sizes);
749
vl_mpeg12_end_frame(struct pipe_video_codec *decoder,
750
struct pipe_video_buffer *target,
751
struct pipe_picture_desc *picture)
753
struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
754
struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
755
struct pipe_sampler_view **ref_frames[2];
756
struct pipe_sampler_view **mc_source_sv;
757
struct pipe_surface **target_surfaces;
758
struct pipe_vertex_buffer vb[3];
759
struct vl_mpeg12_buffer *buf;
761
const unsigned *plane_order;
762
unsigned i, j, component;
763
unsigned nr_components;
765
assert(dec && target && picture);
766
assert(!target->interlaced);
768
buf = vl_mpeg12_get_decode_buffer(dec, target);
770
vl_vb_unmap(&buf->vertex_stream, dec->context);
772
if (buf->tex_transfer)
773
dec->context->texture_unmap(dec->context, buf->tex_transfer);
778
target_surfaces = get_video_buffer_private(dec, target)->surfaces;
780
for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
782
ref_frames[i] = get_video_buffer_private(dec, desc->ref[i])->sampler_view_planes;
784
ref_frames[i] = NULL;
787
dec->context->bind_vertex_elements_state(dec->context, dec->ves_mv);
788
for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
789
if (!target_surfaces[i]) continue;
791
vl_mc_set_surface(&buf->mc[i], target_surfaces[i]);
793
for (j = 0; j < VL_MAX_REF_FRAMES; ++j) {
794
if (!ref_frames[j] || !ref_frames[j][i]) continue;
796
vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);
797
dec->context->set_vertex_buffers(dec->context, 0, 3, 0, false, vb);
799
vl_mc_render_ref(i ? &dec->mc_c : &dec->mc_y, &buf->mc[i], ref_frames[j][i]);
803
dec->context->bind_vertex_elements_state(dec->context, dec->ves_ycbcr);
804
for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
805
if (!buf->num_ycbcr_blocks[i]) continue;
807
vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);
808
dec->context->set_vertex_buffers(dec->context, 0, 2, 0, false, vb);
810
vl_zscan_render(i ? &dec->zscan_c : & dec->zscan_y, &buf->zscan[i] , buf->num_ycbcr_blocks[i]);
812
if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
813
vl_idct_flush(i ? &dec->idct_c : &dec->idct_y, &buf->idct[i], buf->num_ycbcr_blocks[i]);
816
plane_order = vl_video_buffer_plane_order(target->buffer_format);
817
mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
818
for (i = 0, component = 0; component < VL_NUM_COMPONENTS; ++i) {
819
if (!target_surfaces[i]) continue;
821
nr_components = util_format_get_nr_components(target_surfaces[i]->texture->format);
822
for (j = 0; j < nr_components; ++j, ++component) {
823
unsigned plane = plane_order[component];
824
if (!buf->num_ycbcr_blocks[plane]) continue;
826
vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, plane);
827
dec->context->set_vertex_buffers(dec->context, 0, 2, 0, false, vb);
829
if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
830
vl_idct_prepare_stage2(i ? &dec->idct_c : &dec->idct_y, &buf->idct[plane]);
832
dec->context->set_sampler_views(dec->context,
833
PIPE_SHADER_FRAGMENT, 0, 1, 0, false,
834
&mc_source_sv[plane]);
835
dec->context->bind_sampler_states(dec->context,
836
PIPE_SHADER_FRAGMENT,
837
0, 1, &dec->sampler_ycbcr);
839
vl_mc_render_ycbcr(i ? &dec->mc_c : &dec->mc_y, &buf->mc[i], j, buf->num_ycbcr_blocks[plane]);
842
dec->context->flush(dec->context, NULL, 0);
843
++dec->current_buffer;
844
dec->current_buffer %= 4;
848
vl_mpeg12_flush(struct pipe_video_codec *decoder)
852
//Noop, for shaders it is much faster to flush everything in end_frame
856
init_pipe_state(struct vl_mpeg12_decoder *dec)
858
struct pipe_depth_stencil_alpha_state dsa;
859
struct pipe_sampler_state sampler;
864
memset(&dsa, 0, sizeof dsa);
865
dsa.depth_enabled = 0;
866
dsa.depth_writemask = 0;
867
dsa.depth_func = PIPE_FUNC_ALWAYS;
868
for (i = 0; i < 2; ++i) {
869
dsa.stencil[i].enabled = 0;
870
dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
871
dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
872
dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
873
dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
874
dsa.stencil[i].valuemask = 0;
875
dsa.stencil[i].writemask = 0;
877
dsa.alpha_enabled = 0;
878
dsa.alpha_func = PIPE_FUNC_ALWAYS;
879
dsa.alpha_ref_value = 0;
880
dec->dsa = dec->context->create_depth_stencil_alpha_state(dec->context, &dsa);
881
dec->context->bind_depth_stencil_alpha_state(dec->context, dec->dsa);
883
memset(&sampler, 0, sizeof(sampler));
884
sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
885
sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
886
sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
887
sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
888
sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
889
sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
890
sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
891
sampler.compare_func = PIPE_FUNC_ALWAYS;
892
sampler.normalized_coords = 1;
893
dec->sampler_ycbcr = dec->context->create_sampler_state(dec->context, &sampler);
894
if (!dec->sampler_ycbcr)
900
static const struct format_config*
901
find_format_config(struct vl_mpeg12_decoder *dec, const struct format_config configs[], unsigned num_configs)
903
struct pipe_screen *screen;
908
screen = dec->context->screen;
910
for (i = 0; i < num_configs; ++i) {
911
if (!screen->is_format_supported(screen, configs[i].zscan_source_format, PIPE_TEXTURE_2D,
912
1, 1, PIPE_BIND_SAMPLER_VIEW))
915
if (configs[i].idct_source_format != PIPE_FORMAT_NONE) {
916
if (!screen->is_format_supported(screen, configs[i].idct_source_format, PIPE_TEXTURE_2D,
917
1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
920
if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_3D,
921
1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
924
if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_2D,
925
1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
935
init_zscan(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
937
unsigned num_channels;
941
dec->zscan_source_format = format_config->zscan_source_format;
942
dec->zscan_linear = vl_zscan_layout(dec->context, vl_zscan_linear, dec->blocks_per_line);
943
dec->zscan_normal = vl_zscan_layout(dec->context, vl_zscan_normal, dec->blocks_per_line);
944
dec->zscan_alternate = vl_zscan_layout(dec->context, vl_zscan_alternate, dec->blocks_per_line);
946
num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;
948
if (!vl_zscan_init(&dec->zscan_y, dec->context, dec->base.width, dec->base.height,
949
dec->blocks_per_line, dec->num_blocks, num_channels))
952
if (!vl_zscan_init(&dec->zscan_c, dec->context, dec->chroma_width, dec->chroma_height,
953
dec->blocks_per_line, dec->num_blocks, num_channels))
960
init_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
962
unsigned nr_of_idct_render_targets, max_inst;
963
enum pipe_format formats[3];
964
struct pipe_video_buffer templat;
966
struct pipe_sampler_view *matrix = NULL;
968
nr_of_idct_render_targets = dec->context->screen->get_param
970
dec->context->screen, PIPE_CAP_MAX_RENDER_TARGETS
973
max_inst = dec->context->screen->get_shader_param
975
dec->context->screen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_MAX_INSTRUCTIONS
978
// Just assume we need 32 inst per render target, not 100% true, but should work in most cases
979
if (nr_of_idct_render_targets >= 4 && max_inst >= 32*4)
980
// more than 4 render targets usually doesn't makes any seens
981
nr_of_idct_render_targets = 4;
983
nr_of_idct_render_targets = 1;
985
formats[0] = formats[1] = formats[2] = format_config->idct_source_format;
986
memset(&templat, 0, sizeof(templat));
987
templat.width = dec->base.width / 4;
988
templat.height = dec->base.height;
989
dec->idct_source = vl_video_buffer_create_ex
991
dec->context, &templat,
992
formats, 1, 1, PIPE_USAGE_DEFAULT,
993
PIPE_VIDEO_CHROMA_FORMAT_420
996
if (!dec->idct_source)
997
goto error_idct_source;
999
formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
1000
memset(&templat, 0, sizeof(templat));
1001
templat.width = dec->base.width / nr_of_idct_render_targets;
1002
templat.height = dec->base.height / 4;
1003
dec->mc_source = vl_video_buffer_create_ex
1005
dec->context, &templat,
1006
formats, nr_of_idct_render_targets, 1, PIPE_USAGE_DEFAULT,
1007
PIPE_VIDEO_CHROMA_FORMAT_420
1010
if (!dec->mc_source)
1011
goto error_mc_source;
1013
if (!(matrix = vl_idct_upload_matrix(dec->context, format_config->idct_scale)))
1016
if (!vl_idct_init(&dec->idct_y, dec->context, dec->base.width, dec->base.height,
1017
nr_of_idct_render_targets, matrix, matrix))
1020
if(!vl_idct_init(&dec->idct_c, dec->context, dec->chroma_width, dec->chroma_height,
1021
nr_of_idct_render_targets, matrix, matrix))
1024
pipe_sampler_view_reference(&matrix, NULL);
1029
vl_idct_cleanup(&dec->idct_y);
1032
pipe_sampler_view_reference(&matrix, NULL);
1035
dec->mc_source->destroy(dec->mc_source);
1038
dec->idct_source->destroy(dec->idct_source);
1045
init_mc_source_widthout_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
1047
enum pipe_format formats[3];
1048
struct pipe_video_buffer templat;
1050
formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
1051
assert(pipe_format_to_chroma_format(formats[0]) == dec->base.chroma_format);
1052
memset(&templat, 0, sizeof(templat));
1053
templat.width = dec->base.width;
1054
templat.height = dec->base.height;
1055
dec->mc_source = vl_video_buffer_create_ex
1057
dec->context, &templat,
1058
formats, 1, 1, PIPE_USAGE_DEFAULT,
1059
PIPE_VIDEO_CHROMA_FORMAT_420
1062
return dec->mc_source != NULL;
1066
mc_vert_shader_callback(void *priv, struct vl_mc *mc,
1067
struct ureg_program *shader,
1068
unsigned first_output,
1069
struct ureg_dst tex)
1071
struct vl_mpeg12_decoder *dec = priv;
1072
struct ureg_dst o_vtex;
1077
if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1078
struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
1079
vl_idct_stage2_vert_shader(idct, shader, first_output, tex);
1081
o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output);
1082
ureg_MOV(shader, ureg_writemask(o_vtex, TGSI_WRITEMASK_XY), ureg_src(tex));
1087
mc_frag_shader_callback(void *priv, struct vl_mc *mc,
1088
struct ureg_program *shader,
1089
unsigned first_input,
1090
struct ureg_dst dst)
1092
struct vl_mpeg12_decoder *dec = priv;
1093
struct ureg_src src, sampler;
1098
if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1099
struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
1100
vl_idct_stage2_frag_shader(idct, shader, first_input, dst);
1102
src = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, first_input, TGSI_INTERPOLATE_LINEAR);
1103
sampler = ureg_DECL_sampler(shader, 0);
1104
ureg_TEX(shader, dst, TGSI_TEXTURE_2D, src, sampler);
1108
struct pipe_video_codec *
1109
vl_create_mpeg12_decoder(struct pipe_context *context,
1110
const struct pipe_video_codec *templat)
1112
const unsigned block_size_pixels = VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;
1113
const struct format_config *format_config;
1114
struct vl_mpeg12_decoder *dec;
1116
assert(u_reduce_video_profile(templat->profile) == PIPE_VIDEO_FORMAT_MPEG12);
1118
dec = CALLOC_STRUCT(vl_mpeg12_decoder);
1123
dec->base = *templat;
1124
dec->base.context = context;
1125
dec->context = pipe_create_multimedia_context(context->screen);
1127
dec->base.destroy = vl_mpeg12_destroy;
1128
dec->base.begin_frame = vl_mpeg12_begin_frame;
1129
dec->base.decode_macroblock = vl_mpeg12_decode_macroblock;
1130
dec->base.decode_bitstream = vl_mpeg12_decode_bitstream;
1131
dec->base.end_frame = vl_mpeg12_end_frame;
1132
dec->base.flush = vl_mpeg12_flush;
1134
dec->blocks_per_line = MAX2(util_next_power_of_two(dec->base.width) / block_size_pixels, 4);
1135
dec->num_blocks = (dec->base.width * dec->base.height) / block_size_pixels;
1136
dec->width_in_macroblocks = align(dec->base.width, VL_MACROBLOCK_WIDTH) / VL_MACROBLOCK_WIDTH;
1138
/* TODO: Implement 422, 444 */
1139
assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
1141
if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
1142
dec->chroma_width = dec->base.width / 2;
1143
dec->chroma_height = dec->base.height / 2;
1144
dec->num_blocks = dec->num_blocks * 2;
1145
} else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
1146
dec->chroma_width = dec->base.width / 2;
1147
dec->chroma_height = dec->base.height;
1148
dec->num_blocks = dec->num_blocks * 2 + dec->num_blocks;
1150
dec->chroma_width = dec->base.width;
1151
dec->chroma_height = dec->base.height;
1152
dec->num_blocks = dec->num_blocks * 3;
1155
dec->quads = vl_vb_upload_quads(dec->context);
1156
dec->pos = vl_vb_upload_pos(
1158
dec->base.width / VL_MACROBLOCK_WIDTH,
1159
dec->base.height / VL_MACROBLOCK_HEIGHT
1162
dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->context);
1163
dec->ves_mv = vl_vb_get_ves_mv(dec->context);
1165
switch (templat->entrypoint) {
1166
case PIPE_VIDEO_ENTRYPOINT_BITSTREAM:
1167
format_config = find_format_config(dec, bitstream_format_config, num_bitstream_format_configs);
1170
case PIPE_VIDEO_ENTRYPOINT_IDCT:
1171
format_config = find_format_config(dec, idct_format_config, num_idct_format_configs);
1174
case PIPE_VIDEO_ENTRYPOINT_MC:
1175
format_config = find_format_config(dec, mc_format_config, num_mc_format_configs);
1184
if (!format_config) {
1189
if (!init_zscan(dec, format_config))
1192
if (templat->entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1193
if (!init_idct(dec, format_config))
1196
if (!init_mc_source_widthout_idct(dec, format_config))
1200
if (!vl_mc_init(&dec->mc_y, dec->context, dec->base.width, dec->base.height,
1201
VL_MACROBLOCK_HEIGHT, format_config->mc_scale,
1202
mc_vert_shader_callback, mc_frag_shader_callback, dec))
1206
if (!vl_mc_init(&dec->mc_c, dec->context, dec->base.width, dec->base.height,
1207
VL_BLOCK_HEIGHT, format_config->mc_scale,
1208
mc_vert_shader_callback, mc_frag_shader_callback, dec))
1211
if (!init_pipe_state(dec))
1212
goto error_pipe_state;
1214
list_inithead(&dec->buffer_privates);
1219
vl_mc_cleanup(&dec->mc_c);
1222
vl_mc_cleanup(&dec->mc_y);
1225
if (templat->entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1226
vl_idct_cleanup(&dec->idct_y);
1227
vl_idct_cleanup(&dec->idct_c);
1228
dec->idct_source->destroy(dec->idct_source);
1230
dec->mc_source->destroy(dec->mc_source);
1233
vl_zscan_cleanup(&dec->zscan_y);
1234
vl_zscan_cleanup(&dec->zscan_c);