radeon_vce.c revision af69d88d
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 */
51static void flush(struct rvce_encoder *enc)
52{
53	enc->ws->cs_flush(enc->cs, RADEON_FLUSH_ASYNC, NULL, 0);
54}
55
56#if 0
57static 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 * reset the CPB handling
84 */
85static void reset_cpb(struct rvce_encoder *enc)
86{
87	unsigned i;
88
89	LIST_INITHEAD(&enc->cpb_slots);
90	for (i = 0; i < enc->cpb_num; ++i) {
91		struct rvce_cpb_slot *slot = &enc->cpb_array[i];
92		slot->index = i;
93		slot->picture_type = PIPE_H264_ENC_PICTURE_TYPE_SKIP;
94		slot->frame_num = 0;
95		slot->pic_order_cnt = 0;
96		LIST_ADDTAIL(&slot->list, &enc->cpb_slots);
97	}
98}
99
100/**
101 * sort l0 and l1 to the top of the list
102 */
103static void sort_cpb(struct rvce_encoder *enc)
104{
105	struct rvce_cpb_slot *i, *l0 = NULL, *l1 = NULL;
106
107	LIST_FOR_EACH_ENTRY(i, &enc->cpb_slots, list) {
108		if (i->frame_num == enc->pic.ref_idx_l0)
109			l0 = i;
110
111		if (i->frame_num == enc->pic.ref_idx_l1)
112			l1 = i;
113
114		if (enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_P && l0)
115			break;
116
117		if (enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_B &&
118		    l0 && l1)
119			break;
120	}
121
122	if (l1) {
123		LIST_DEL(&l1->list);
124		LIST_ADD(&l1->list, &enc->cpb_slots);
125	}
126
127	if (l0) {
128		LIST_DEL(&l0->list);
129		LIST_ADD(&l0->list, &enc->cpb_slots);
130	}
131}
132
133/**
134 * get number of cpbs based on dpb
135 */
136static unsigned get_cpb_num(struct rvce_encoder *enc)
137{
138	unsigned w = align(enc->base.width, 16) / 16;
139	unsigned h = align(enc->base.height, 16) / 16;
140	unsigned dpb;
141
142	switch (enc->base.level) {
143	case 10:
144		dpb = 396;
145		break;
146	case 11:
147		dpb = 900;
148		break;
149	case 12:
150	case 13:
151	case 20:
152		dpb = 2376;
153		break;
154	case 21:
155		dpb = 4752;
156		break;
157	case 22:
158	case 30:
159		dpb = 8100;
160		break;
161	case 31:
162		dpb = 18000;
163		break;
164	case 32:
165		dpb = 20480;
166		break;
167	case 40:
168	case 41:
169		dpb = 32768;
170		break;
171	default:
172	case 42:
173		dpb = 34816;
174		break;
175	case 50:
176		dpb = 110400;
177		break;
178	case 51:
179		dpb = 184320;
180		break;
181	}
182
183	return MIN2(dpb / (w * h), 16);
184}
185
186/**
187 * destroy this video encoder
188 */
189static void rvce_destroy(struct pipe_video_codec *encoder)
190{
191	struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
192	if (enc->stream_handle) {
193		struct rvid_buffer fb;
194		rvid_create_buffer(enc->ws, &fb, 512, RADEON_DOMAIN_GTT, 0);
195		enc->fb = &fb;
196		enc->session(enc);
197		enc->feedback(enc);
198		enc->destroy(enc);
199		flush(enc);
200		rvid_destroy_buffer(&fb);
201	}
202	rvid_destroy_buffer(&enc->cpb);
203	enc->ws->cs_destroy(enc->cs);
204	FREE(enc->cpb_array);
205	FREE(enc);
206}
207
208static void rvce_begin_frame(struct pipe_video_codec *encoder,
209			     struct pipe_video_buffer *source,
210			     struct pipe_picture_desc *picture)
211{
212	struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
213	struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)source;
214	struct pipe_h264_enc_picture_desc *pic = (struct pipe_h264_enc_picture_desc *)picture;
215
216	bool need_rate_control =
217		enc->pic.rate_ctrl.rate_ctrl_method != pic->rate_ctrl.rate_ctrl_method ||
218		enc->pic.quant_i_frames != pic->quant_i_frames ||
219		enc->pic.quant_p_frames != pic->quant_p_frames ||
220		enc->pic.quant_b_frames != pic->quant_b_frames;
221
222	enc->pic = *pic;
223
224	enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma);
225	enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma);
226
227	if (pic->picture_type == PIPE_H264_ENC_PICTURE_TYPE_IDR)
228		reset_cpb(enc);
229	else if (pic->picture_type == PIPE_H264_ENC_PICTURE_TYPE_P ||
230	         pic->picture_type == PIPE_H264_ENC_PICTURE_TYPE_B)
231		sort_cpb(enc);
232
233	if (!enc->stream_handle) {
234		struct rvid_buffer fb;
235		enc->stream_handle = rvid_alloc_stream_handle();
236		rvid_create_buffer(enc->ws, &fb, 512, RADEON_DOMAIN_GTT, 0);
237		enc->fb = &fb;
238		enc->session(enc);
239		enc->create(enc);
240		enc->rate_control(enc);
241		need_rate_control = false;
242		enc->config_extension(enc);
243		enc->motion_estimation(enc);
244		enc->rdo(enc);
245		enc->pic_control(enc);
246		enc->feedback(enc);
247		flush(enc);
248		//dump_feedback(enc, &fb);
249		rvid_destroy_buffer(&fb);
250	}
251
252	enc->session(enc);
253
254	if (need_rate_control)
255		enc->rate_control(enc);
256}
257
258static void rvce_encode_bitstream(struct pipe_video_codec *encoder,
259				  struct pipe_video_buffer *source,
260				  struct pipe_resource *destination,
261				  void **fb)
262{
263	struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
264	enc->get_buffer(destination, &enc->bs_handle, NULL);
265	enc->bs_size = destination->width0;
266
267	*fb = enc->fb = CALLOC_STRUCT(rvid_buffer);
268	if (!rvid_create_buffer(enc->ws, enc->fb, 512, RADEON_DOMAIN_GTT, 0)) {
269		RVID_ERR("Can't create feedback buffer.\n");
270		return;
271	}
272	enc->encode(enc);
273	enc->feedback(enc);
274}
275
276static void rvce_end_frame(struct pipe_video_codec *encoder,
277			   struct pipe_video_buffer *source,
278			   struct pipe_picture_desc *picture)
279{
280	struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
281	struct rvce_cpb_slot *slot = LIST_ENTRY(
282		struct rvce_cpb_slot, enc->cpb_slots.prev, list);
283
284	flush(enc);
285
286	/* update the CPB backtrack with the just encoded frame */
287	slot->picture_type = enc->pic.picture_type;
288	slot->frame_num = enc->pic.frame_num;
289	slot->pic_order_cnt = enc->pic.pic_order_cnt;
290	if (!enc->pic.not_referenced) {
291		LIST_DEL(&slot->list);
292		LIST_ADD(&slot->list, &enc->cpb_slots);
293	}
294}
295
296static void rvce_get_feedback(struct pipe_video_codec *encoder,
297			      void *feedback, unsigned *size)
298{
299	struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
300	struct rvid_buffer *fb = feedback;
301
302	if (size) {
303		uint32_t *ptr = enc->ws->buffer_map(fb->cs_handle, enc->cs, PIPE_TRANSFER_READ_WRITE);
304
305		if (ptr[1]) {
306			*size = ptr[4] - ptr[9];
307		} else {
308			*size = 0;
309		}
310
311		enc->ws->buffer_unmap(fb->cs_handle);
312	}
313	//dump_feedback(enc, fb);
314	rvid_destroy_buffer(fb);
315	FREE(fb);
316}
317
318/**
319 * flush any outstanding command buffers to the hardware
320 */
321static void rvce_flush(struct pipe_video_codec *encoder)
322{
323}
324
325static void rvce_cs_flush(void *ctx, unsigned flags,
326			  struct pipe_fence_handle **fence)
327{
328	// just ignored
329}
330
331struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
332					     const struct pipe_video_codec *templ,
333					     struct radeon_winsys* ws,
334					     rvce_get_buffer get_buffer)
335{
336	struct r600_common_screen *rscreen = (struct r600_common_screen *)context->screen;
337	struct rvce_encoder *enc;
338	struct pipe_video_buffer *tmp_buf, templat = {};
339	struct radeon_surface *tmp_surf;
340	unsigned cpb_size;
341
342	if (!rscreen->info.vce_fw_version) {
343		RVID_ERR("Kernel doesn't supports VCE!\n");
344		return NULL;
345
346	} else if (!rvce_is_fw_version_supported(rscreen)) {
347		RVID_ERR("Unsupported VCE fw version loaded!\n");
348		return NULL;
349	}
350
351	enc = CALLOC_STRUCT(rvce_encoder);
352	if (!enc)
353		return NULL;
354
355	enc->base = *templ;
356	enc->base.context = context;
357
358	enc->base.destroy = rvce_destroy;
359	enc->base.begin_frame = rvce_begin_frame;
360	enc->base.encode_bitstream = rvce_encode_bitstream;
361	enc->base.end_frame = rvce_end_frame;
362	enc->base.flush = rvce_flush;
363	enc->base.get_feedback = rvce_get_feedback;
364	enc->get_buffer = get_buffer;
365
366	enc->ws = ws;
367	enc->cs = ws->cs_create(ws, RING_VCE, rvce_cs_flush, enc, NULL);
368	if (!enc->cs) {
369		RVID_ERR("Can't get command submission context.\n");
370		goto error;
371	}
372
373	templat.buffer_format = PIPE_FORMAT_NV12;
374	templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
375	templat.width = enc->base.width;
376	templat.height = enc->base.height;
377	templat.interlaced = false;
378	if (!(tmp_buf = context->create_video_buffer(context, &templat))) {
379		RVID_ERR("Can't create video buffer.\n");
380		goto error;
381	}
382
383	enc->cpb_num = get_cpb_num(enc);
384	if (!enc->cpb_num)
385		goto error;
386
387	get_buffer(((struct vl_video_buffer *)tmp_buf)->resources[0], NULL, &tmp_surf);
388	cpb_size = align(tmp_surf->level[0].pitch_bytes, 128);
389	cpb_size = cpb_size * align(tmp_surf->npix_y, 16);
390	cpb_size = cpb_size * 3 / 2;
391	cpb_size = cpb_size * enc->cpb_num;
392	tmp_buf->destroy(tmp_buf);
393	if (!rvid_create_buffer(enc->ws, &enc->cpb, cpb_size, RADEON_DOMAIN_VRAM, 0)) {
394		RVID_ERR("Can't create CPB buffer.\n");
395		goto error;
396	}
397
398	enc->cpb_array = CALLOC(enc->cpb_num, sizeof(struct rvce_cpb_slot));
399	if (!enc->cpb_array)
400		goto error;
401
402	reset_cpb(enc);
403
404	radeon_vce_40_2_2_init(enc);
405
406	return &enc->base;
407
408error:
409	if (enc->cs)
410		enc->ws->cs_destroy(enc->cs);
411
412	rvid_destroy_buffer(&enc->cpb);
413
414	FREE(enc->cpb_array);
415	FREE(enc);
416	return NULL;
417}
418
419/**
420 * check if kernel has the right fw version loaded
421 */
422bool rvce_is_fw_version_supported(struct r600_common_screen *rscreen)
423{
424	return rscreen->info.vce_fw_version == ((40 << 24) | (2 << 16) | (2 << 8));
425}
426