summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/radeon/radeon_vcn_enc.c
blob: 05e7e8362462c369d48909f95245214bc21e78f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**************************************************************************
 *
 * Copyright 2017 Advanced Micro Devices, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include <stdio.h>

#include "pipe/p_video_codec.h"

#include "util/u_video.h"
#include "util/u_memory.h"

#include "vl/vl_video_buffer.h"

#include "r600_pipe_common.h"
#include "radeon_video.h"
#include "radeon_vcn_enc.h"

static void radeon_vcn_enc_get_param(struct radeon_encoder *enc, struct pipe_h264_enc_picture_desc *pic)
{
	enc->enc_pic.picture_type = pic->picture_type;
	enc->enc_pic.frame_num = pic->frame_num;
	enc->enc_pic.pic_order_cnt = pic->pic_order_cnt;
	enc->enc_pic.pic_order_cnt_type = pic->pic_order_cnt_type;
	enc->enc_pic.ref_idx_l0 = pic->ref_idx_l0;
	enc->enc_pic.ref_idx_l1 = pic->ref_idx_l1;
	enc->enc_pic.not_referenced = pic->not_referenced;
	enc->enc_pic.is_idr = pic->is_idr;
	enc->enc_pic.crop_left = 0;
	enc->enc_pic.crop_right = (align(enc->base.width, 16) - enc->base.width) / 2;
	enc->enc_pic.crop_top = 0;
	enc->enc_pic.crop_bottom = (align(enc->base.height, 16) - enc->base.height) / 2;
}

static void flush(struct radeon_encoder *enc)
{
	enc->ws->cs_flush(enc->cs, RADEON_FLUSH_ASYNC, NULL);
}

static void radeon_enc_flush(struct pipe_video_codec *encoder)
{
	struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
	flush(enc);
}

static void radeon_enc_cs_flush(void *ctx, unsigned flags,
								struct pipe_fence_handle **fence)
{
	// just ignored
}

static unsigned get_cpb_num(struct radeon_encoder *enc)
{
	unsigned w = align(enc->base.width, 16) / 16;
	unsigned h = align(enc->base.height, 16) / 16;
	unsigned dpb;

	switch (enc->base.level) {
	case 10:
		dpb = 396;
		break;
	case 11:
		dpb = 900;
		break;
	case 12:
	case 13:
	case 20:
		dpb = 2376;
		break;
	case 21:
		dpb = 4752;
		break;
	case 22:
	case 30:
		dpb = 8100;
		break;
	case 31:
		dpb = 18000;
		break;
	case 32:
		dpb = 20480;
		break;
	case 40:
	case 41:
		dpb = 32768;
		break;
	case 42:
		dpb = 34816;
		break;
	case 50:
		dpb = 110400;
		break;
	default:
	case 51:
	case 52:
		dpb = 184320;
		break;
	}

	return MIN2(dpb / (w * h), 16);
}

static void radeon_enc_begin_frame(struct pipe_video_codec *encoder,
							 struct pipe_video_buffer *source,
							 struct pipe_picture_desc *picture)
{
	struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
	struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)source;
	struct pipe_h264_enc_picture_desc *pic = (struct pipe_h264_enc_picture_desc *)picture;

	radeon_vcn_enc_get_param(enc, pic);

	enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma);
	enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma);

	enc->need_feedback = false;

	if (!enc->stream_handle) {
		struct rvid_buffer fb;
		enc->stream_handle = si_vid_alloc_stream_handle();
		enc->si = CALLOC_STRUCT(rvid_buffer);
		si_vid_create_buffer(enc->screen, enc->si, 128 * 1024, PIPE_USAGE_STAGING);
		si_vid_create_buffer(enc->screen, &fb, 4096, PIPE_USAGE_STAGING);
		enc->fb = &fb;
		enc->begin(enc, pic);
		flush(enc);
		si_vid_destroy_buffer(&fb);
	}
}

static void radeon_enc_encode_bitstream(struct pipe_video_codec *encoder,
								  struct pipe_video_buffer *source,
								  struct pipe_resource *destination,
								  void **fb)
{
	struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
	enc->get_buffer(destination, &enc->bs_handle, NULL);
	enc->bs_size = destination->width0;

	*fb = enc->fb = CALLOC_STRUCT(rvid_buffer);

	if (!si_vid_create_buffer(enc->screen, enc->fb, 4096, PIPE_USAGE_STAGING)) {
		RVID_ERR("Can't create feedback buffer.\n");
		return;
	}

	enc->need_feedback = true;
	enc->encode(enc);
}

static void radeon_enc_end_frame(struct pipe_video_codec *encoder,
						   struct pipe_video_buffer *source,
						   struct pipe_picture_desc *picture)
{
	struct radeon_encoder *enc = (struct radeon_encoder*)encoder;
	flush(enc);
}

static void radeon_enc_destroy(struct pipe_video_codec *encoder)
{
	/* TODO*/
}

static void radeon_enc_get_feedback(struct pipe_video_codec *encoder,
							  void *feedback, unsigned *size)
{
	/* TODO*/
}

struct pipe_video_codec *radeon_create_encoder(struct pipe_context *context,
		const struct pipe_video_codec *templ,
		struct radeon_winsys* ws,
		radeon_enc_get_buffer get_buffer)
{
	/* TODO*/
}