radeon/vce: remove RVCE_NUM_CPB_EXTRA_FRAMES
[mesa.git] / src / gallium / drivers / radeon / radeon_vce.c
1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
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 THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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 /*
29 * Authors:
30 * Christian König <christian.koenig@amd.com>
31 *
32 */
33
34 #include <stdio.h>
35
36 #include "pipe/p_video_codec.h"
37
38 #include "util/u_video.h"
39 #include "util/u_memory.h"
40
41 #include "vl/vl_video_buffer.h"
42
43 #include "../../winsys/radeon/drm/radeon_winsys.h"
44 #include "r600_pipe_common.h"
45 #include "radeon_video.h"
46 #include "radeon_vce.h"
47
48 /**
49 * flush commands to the hardware
50 */
51 static void flush(struct rvce_encoder *enc)
52 {
53 enc->ws->cs_flush(enc->cs, RADEON_FLUSH_ASYNC, 0);
54 }
55
56 #if 0
57 static void dump_feedback(struct rvce_encoder *enc, struct rvid_buffer *fb)
58 {
59 uint32_t *ptr = enc->ws->buffer_map(fb->cs_handle, enc->cs, PIPE_TRANSFER_READ_WRITE);
60 unsigned i = 0;
61 fprintf(stderr, "\n");
62 fprintf(stderr, "encStatus:\t\t\t%08x\n", ptr[i++]);
63 fprintf(stderr, "encHasBitstream:\t\t%08x\n", ptr[i++]);
64 fprintf(stderr, "encHasAudioBitstream:\t\t%08x\n", ptr[i++]);
65 fprintf(stderr, "encBitstreamOffset:\t\t%08x\n", ptr[i++]);
66 fprintf(stderr, "encBitstreamSize:\t\t%08x\n", ptr[i++]);
67 fprintf(stderr, "encAudioBitstreamOffset:\t%08x\n", ptr[i++]);
68 fprintf(stderr, "encAudioBitstreamSize:\t\t%08x\n", ptr[i++]);
69 fprintf(stderr, "encExtrabytes:\t\t\t%08x\n", ptr[i++]);
70 fprintf(stderr, "encAudioExtrabytes:\t\t%08x\n", ptr[i++]);
71 fprintf(stderr, "videoTimeStamp:\t\t\t%08x\n", ptr[i++]);
72 fprintf(stderr, "audioTimeStamp:\t\t\t%08x\n", ptr[i++]);
73 fprintf(stderr, "videoOutputType:\t\t%08x\n", ptr[i++]);
74 fprintf(stderr, "attributeFlags:\t\t\t%08x\n", ptr[i++]);
75 fprintf(stderr, "seiPrivatePackageOffset:\t%08x\n", ptr[i++]);
76 fprintf(stderr, "seiPrivatePackageSize:\t\t%08x\n", ptr[i++]);
77 fprintf(stderr, "\n");
78 enc->ws->buffer_unmap(fb->cs_handle);
79 }
80 #endif
81
82 /**
83 * destroy this video encoder
84 */
85 static void rvce_destroy(struct pipe_video_codec *encoder)
86 {
87 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
88 if (enc->stream_handle) {
89 struct rvid_buffer fb;
90 rvid_create_buffer(enc->ws, &fb, 512, RADEON_DOMAIN_GTT);
91 enc->fb = &fb;
92 enc->session(enc);
93 enc->feedback(enc);
94 enc->destroy(enc);
95 flush(enc);
96 rvid_destroy_buffer(&fb);
97 }
98 rvid_destroy_buffer(&enc->cpb);
99 enc->ws->cs_destroy(enc->cs);
100 FREE(enc);
101 }
102
103 static void rvce_begin_frame(struct pipe_video_codec *encoder,
104 struct pipe_video_buffer *source,
105 struct pipe_picture_desc *picture)
106 {
107 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
108 struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)source;
109 struct pipe_h264_enc_picture_desc *pic = (struct pipe_h264_enc_picture_desc *)picture;
110
111 bool need_rate_control =
112 enc->pic.rate_ctrl.rate_ctrl_method != pic->rate_ctrl.rate_ctrl_method ||
113 enc->pic.quant_i_frames != pic->quant_i_frames ||
114 enc->pic.quant_p_frames != pic->quant_p_frames ||
115 enc->pic.quant_b_frames != pic->quant_b_frames;
116
117 enc->pic = *pic;
118
119 enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma);
120 enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma);
121
122 if (!enc->stream_handle) {
123 struct rvid_buffer fb;
124 enc->stream_handle = rvid_alloc_stream_handle();
125 rvid_create_buffer(enc->ws, &fb, 512, RADEON_DOMAIN_GTT);
126 enc->fb = &fb;
127 enc->session(enc);
128 enc->create(enc);
129 enc->rate_control(enc);
130 need_rate_control = false;
131 enc->config_extension(enc);
132 enc->motion_estimation(enc);
133 enc->rdo(enc);
134 enc->pic_control(enc);
135 enc->feedback(enc);
136 flush(enc);
137 //dump_feedback(enc, &fb);
138 rvid_destroy_buffer(&fb);
139 }
140
141 enc->session(enc);
142
143 if (need_rate_control)
144 enc->rate_control(enc);
145 }
146
147 static void rvce_encode_bitstream(struct pipe_video_codec *encoder,
148 struct pipe_video_buffer *source,
149 struct pipe_resource *destination,
150 void **fb)
151 {
152 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
153 enc->get_buffer(destination, &enc->bs_handle, NULL);
154 enc->bs_size = destination->width0;
155
156 *fb = enc->fb = CALLOC_STRUCT(rvid_buffer);
157 if (!rvid_create_buffer(enc->ws, enc->fb, 512, RADEON_DOMAIN_GTT)) {
158 RVID_ERR("Can't create feedback buffer.\n");
159 return;
160 }
161 enc->encode(enc);
162 enc->feedback(enc);
163 }
164
165 static void rvce_end_frame(struct pipe_video_codec *encoder,
166 struct pipe_video_buffer *source,
167 struct pipe_picture_desc *picture)
168 {
169 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
170 flush(enc);
171 }
172
173 static void rvce_get_feedback(struct pipe_video_codec *encoder,
174 void *feedback, unsigned *size)
175 {
176 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
177 struct rvid_buffer *fb = feedback;
178
179 if (size) {
180 uint32_t *ptr = enc->ws->buffer_map(fb->cs_handle, enc->cs, PIPE_TRANSFER_READ_WRITE);
181
182 if (ptr[1]) {
183 *size = ptr[4] - ptr[9];
184 } else {
185 *size = 0;
186 }
187
188 enc->ws->buffer_unmap(fb->cs_handle);
189 }
190 //dump_feedback(enc, fb);
191 rvid_destroy_buffer(fb);
192 FREE(fb);
193 }
194
195 /**
196 * flush any outstanding command buffers to the hardware
197 */
198 static void rvce_flush(struct pipe_video_codec *encoder)
199 {
200 }
201
202 static void rvce_cs_flush(void *ctx, unsigned flags)
203 {
204 // just ignored
205 }
206
207 struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
208 const struct pipe_video_codec *templ,
209 struct radeon_winsys* ws,
210 rvce_get_buffer get_buffer)
211 {
212 struct r600_common_screen *rscreen = (struct r600_common_screen *)context->screen;
213 struct rvce_encoder *enc;
214 struct pipe_video_buffer *tmp_buf, templat = {};
215 struct radeon_surface *tmp_surf;
216 unsigned pitch, vpitch;
217
218 if (!rscreen->info.vce_fw_version) {
219 RVID_ERR("Kernel doesn't supports VCE!\n");
220 return NULL;
221
222 } else if (!rvce_is_fw_version_supported(rscreen)) {
223 RVID_ERR("Unsupported VCE fw version loaded!\n");
224 return NULL;
225 }
226
227 enc = CALLOC_STRUCT(rvce_encoder);
228 if (!enc)
229 return NULL;
230
231 enc->base = *templ;
232 enc->base.context = context;
233
234 enc->base.destroy = rvce_destroy;
235 enc->base.begin_frame = rvce_begin_frame;
236 enc->base.encode_bitstream = rvce_encode_bitstream;
237 enc->base.end_frame = rvce_end_frame;
238 enc->base.flush = rvce_flush;
239 enc->base.get_feedback = rvce_get_feedback;
240 enc->get_buffer = get_buffer;
241
242 enc->ws = ws;
243 enc->cs = ws->cs_create(ws, RING_VCE, NULL);
244 if (!enc->cs) {
245 RVID_ERR("Can't get command submission context.\n");
246 goto error;
247 }
248
249 enc->ws->cs_set_flush_callback(enc->cs, rvce_cs_flush, enc);
250 templat.buffer_format = PIPE_FORMAT_NV12;
251 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
252 templat.width = enc->base.width;
253 templat.height = enc->base.height;
254 templat.interlaced = false;
255 if (!(tmp_buf = context->create_video_buffer(context, &templat))) {
256 RVID_ERR("Can't create video buffer.\n");
257 goto error;
258 }
259
260 get_buffer(((struct vl_video_buffer *)tmp_buf)->resources[0], NULL, &tmp_surf);
261 pitch = align(tmp_surf->level[0].pitch_bytes, 128);
262 vpitch = align(tmp_surf->npix_y, 16);
263 tmp_buf->destroy(tmp_buf);
264 if (!rvid_create_buffer(enc->ws, &enc->cpb,
265 pitch * vpitch * 1.5 * RVCE_NUM_CPB_FRAMES,
266 RADEON_DOMAIN_VRAM)) {
267 RVID_ERR("Can't create CPB buffer.\n");
268 goto error;
269 }
270
271 radeon_vce_40_2_2_init(enc);
272
273 return &enc->base;
274
275 error:
276 if (enc->cs)
277 enc->ws->cs_destroy(enc->cs);
278
279 rvid_destroy_buffer(&enc->cpb);
280
281 FREE(enc);
282 return NULL;
283 }
284
285 /**
286 * check if kernel has the right fw version loaded
287 */
288 bool rvce_is_fw_version_supported(struct r600_common_screen *rscreen)
289 {
290 return rscreen->info.vce_fw_version == ((40 << 24) | (2 << 16) | (2 << 8));
291 }