.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Coda multi-standard codec IP - BIT processor functions |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * Javier Martin, <javier.martin@vista-silicon.com> |
---|
6 | 7 | * Xavier Duret |
---|
7 | 8 | * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix |
---|
8 | | - * |
---|
9 | | - * This program is free software; you can redistribute it and/or modify |
---|
10 | | - * it under the terms of the GNU General Public License as published by |
---|
11 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
12 | | - * (at your option) any later version. |
---|
13 | 9 | */ |
---|
14 | 10 | |
---|
15 | 11 | #include <linux/clk.h> |
---|
.. | .. |
---|
102 | 98 | struct coda_dev *dev = ctx->dev; |
---|
103 | 99 | int ret; |
---|
104 | 100 | |
---|
| 101 | + lockdep_assert_held(&dev->coda_mutex); |
---|
| 102 | + |
---|
105 | 103 | coda_command_async(ctx, cmd); |
---|
106 | 104 | ret = coda_wait_timeout(dev); |
---|
107 | 105 | trace_coda_bit_done(ctx); |
---|
.. | .. |
---|
115 | 113 | unsigned long timeout; |
---|
116 | 114 | unsigned int idx; |
---|
117 | 115 | int ret; |
---|
| 116 | + |
---|
| 117 | + lockdep_assert_held(&dev->coda_mutex); |
---|
118 | 118 | |
---|
119 | 119 | if (!dev->rstc) |
---|
120 | 120 | return -ENOENT; |
---|
.. | .. |
---|
180 | 180 | coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
---|
181 | 181 | } |
---|
182 | 182 | |
---|
183 | | -static int coda_bitstream_pad(struct coda_ctx *ctx, u32 size) |
---|
| 183 | +static int coda_h264_bitstream_pad(struct coda_ctx *ctx, u32 size) |
---|
184 | 184 | { |
---|
185 | 185 | unsigned char *buf; |
---|
186 | 186 | u32 n; |
---|
.. | .. |
---|
199 | 199 | return (n < size) ? -ENOSPC : 0; |
---|
200 | 200 | } |
---|
201 | 201 | |
---|
202 | | -static int coda_bitstream_queue(struct coda_ctx *ctx, |
---|
203 | | - struct vb2_v4l2_buffer *src_buf) |
---|
| 202 | +int coda_bitstream_flush(struct coda_ctx *ctx) |
---|
204 | 203 | { |
---|
205 | | - u32 src_size = vb2_get_plane_payload(&src_buf->vb2_buf, 0); |
---|
206 | | - u32 n; |
---|
| 204 | + int ret; |
---|
207 | 205 | |
---|
208 | | - n = kfifo_in(&ctx->bitstream_fifo, |
---|
209 | | - vb2_plane_vaddr(&src_buf->vb2_buf, 0), src_size); |
---|
210 | | - if (n < src_size) |
---|
211 | | - return -ENOSPC; |
---|
| 206 | + if (ctx->inst_type != CODA_INST_DECODER || !ctx->use_bit) |
---|
| 207 | + return 0; |
---|
212 | 208 | |
---|
213 | | - src_buf->sequence = ctx->qsequence++; |
---|
| 209 | + ret = coda_command_sync(ctx, CODA_COMMAND_DEC_BUF_FLUSH); |
---|
| 210 | + if (ret < 0) { |
---|
| 211 | + v4l2_err(&ctx->dev->v4l2_dev, "failed to flush bitstream\n"); |
---|
| 212 | + return ret; |
---|
| 213 | + } |
---|
| 214 | + |
---|
| 215 | + kfifo_init(&ctx->bitstream_fifo, ctx->bitstream.vaddr, |
---|
| 216 | + ctx->bitstream.size); |
---|
| 217 | + coda_kfifo_sync_to_device_full(ctx); |
---|
214 | 218 | |
---|
215 | 219 | return 0; |
---|
| 220 | +} |
---|
| 221 | + |
---|
| 222 | +static int coda_bitstream_queue(struct coda_ctx *ctx, const u8 *buf, u32 size) |
---|
| 223 | +{ |
---|
| 224 | + u32 n = kfifo_in(&ctx->bitstream_fifo, buf, size); |
---|
| 225 | + |
---|
| 226 | + return (n < size) ? -ENOSPC : 0; |
---|
| 227 | +} |
---|
| 228 | + |
---|
| 229 | +static u32 coda_buffer_parse_headers(struct coda_ctx *ctx, |
---|
| 230 | + struct vb2_v4l2_buffer *src_buf, |
---|
| 231 | + u32 payload) |
---|
| 232 | +{ |
---|
| 233 | + u8 *vaddr = vb2_plane_vaddr(&src_buf->vb2_buf, 0); |
---|
| 234 | + u32 size = 0; |
---|
| 235 | + |
---|
| 236 | + switch (ctx->codec->src_fourcc) { |
---|
| 237 | + case V4L2_PIX_FMT_MPEG2: |
---|
| 238 | + size = coda_mpeg2_parse_headers(ctx, vaddr, payload); |
---|
| 239 | + break; |
---|
| 240 | + case V4L2_PIX_FMT_MPEG4: |
---|
| 241 | + size = coda_mpeg4_parse_headers(ctx, vaddr, payload); |
---|
| 242 | + break; |
---|
| 243 | + default: |
---|
| 244 | + break; |
---|
| 245 | + } |
---|
| 246 | + |
---|
| 247 | + return size; |
---|
216 | 248 | } |
---|
217 | 249 | |
---|
218 | 250 | static bool coda_bitstream_try_queue(struct coda_ctx *ctx, |
---|
219 | 251 | struct vb2_v4l2_buffer *src_buf) |
---|
220 | 252 | { |
---|
221 | 253 | unsigned long payload = vb2_get_plane_payload(&src_buf->vb2_buf, 0); |
---|
| 254 | + u8 *vaddr = vb2_plane_vaddr(&src_buf->vb2_buf, 0); |
---|
222 | 255 | int ret; |
---|
| 256 | + int i; |
---|
223 | 257 | |
---|
224 | 258 | if (coda_get_bitstream_payload(ctx) + payload + 512 >= |
---|
225 | 259 | ctx->bitstream.size) |
---|
226 | 260 | return false; |
---|
227 | 261 | |
---|
228 | | - if (vb2_plane_vaddr(&src_buf->vb2_buf, 0) == NULL) { |
---|
| 262 | + if (!vaddr) { |
---|
229 | 263 | v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n"); |
---|
230 | 264 | return true; |
---|
231 | 265 | } |
---|
232 | 266 | |
---|
233 | | - /* Add zero padding before the first H.264 buffer, if it is too small */ |
---|
| 267 | + if (ctx->qsequence == 0 && payload < 512) { |
---|
| 268 | + /* |
---|
| 269 | + * Add padding after the first buffer, if it is too small to be |
---|
| 270 | + * fetched by the CODA, by repeating the headers. Without |
---|
| 271 | + * repeated headers, or the first frame already queued, decoder |
---|
| 272 | + * sequence initialization fails with error code 0x2000 on i.MX6 |
---|
| 273 | + * or error code 0x1 on i.MX51. |
---|
| 274 | + */ |
---|
| 275 | + u32 header_size = coda_buffer_parse_headers(ctx, src_buf, |
---|
| 276 | + payload); |
---|
| 277 | + |
---|
| 278 | + if (header_size) { |
---|
| 279 | + coda_dbg(1, ctx, "pad with %u-byte header\n", |
---|
| 280 | + header_size); |
---|
| 281 | + for (i = payload; i < 512; i += header_size) { |
---|
| 282 | + ret = coda_bitstream_queue(ctx, vaddr, |
---|
| 283 | + header_size); |
---|
| 284 | + if (ret < 0) { |
---|
| 285 | + v4l2_err(&ctx->dev->v4l2_dev, |
---|
| 286 | + "bitstream buffer overflow\n"); |
---|
| 287 | + return false; |
---|
| 288 | + } |
---|
| 289 | + if (ctx->dev->devtype->product == CODA_960) |
---|
| 290 | + break; |
---|
| 291 | + } |
---|
| 292 | + } else { |
---|
| 293 | + coda_dbg(1, ctx, |
---|
| 294 | + "could not parse header, sequence initialization might fail\n"); |
---|
| 295 | + } |
---|
| 296 | + } |
---|
| 297 | + |
---|
| 298 | + /* Add padding before the first buffer, if it is too small */ |
---|
234 | 299 | if (ctx->qsequence == 0 && payload < 512 && |
---|
235 | 300 | ctx->codec->src_fourcc == V4L2_PIX_FMT_H264) |
---|
236 | | - coda_bitstream_pad(ctx, 512 - payload); |
---|
| 301 | + coda_h264_bitstream_pad(ctx, 512 - payload); |
---|
237 | 302 | |
---|
238 | | - ret = coda_bitstream_queue(ctx, src_buf); |
---|
| 303 | + ret = coda_bitstream_queue(ctx, vaddr, payload); |
---|
239 | 304 | if (ret < 0) { |
---|
240 | 305 | v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n"); |
---|
241 | 306 | return false; |
---|
242 | 307 | } |
---|
| 308 | + |
---|
| 309 | + src_buf->sequence = ctx->qsequence++; |
---|
| 310 | + |
---|
243 | 311 | /* Sync read pointer to device */ |
---|
244 | 312 | if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev)) |
---|
245 | 313 | coda_kfifo_sync_to_device_write(ctx); |
---|
246 | 314 | |
---|
| 315 | + /* Set the stream-end flag after the last buffer is queued */ |
---|
| 316 | + if (src_buf->flags & V4L2_BUF_FLAG_LAST) |
---|
| 317 | + coda_bit_stream_end_flag(ctx); |
---|
247 | 318 | ctx->hold = false; |
---|
248 | 319 | |
---|
249 | 320 | return true; |
---|
.. | .. |
---|
253 | 324 | { |
---|
254 | 325 | struct vb2_v4l2_buffer *src_buf; |
---|
255 | 326 | struct coda_buffer_meta *meta; |
---|
256 | | - unsigned long flags; |
---|
257 | 327 | u32 start; |
---|
258 | 328 | |
---|
259 | 329 | if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) |
---|
.. | .. |
---|
268 | 338 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG && |
---|
269 | 339 | ctx->num_metas > 1) |
---|
270 | 340 | break; |
---|
| 341 | + |
---|
| 342 | + if (ctx->num_internal_frames && |
---|
| 343 | + ctx->num_metas >= ctx->num_internal_frames) { |
---|
| 344 | + meta = list_first_entry(&ctx->buffer_meta_list, |
---|
| 345 | + struct coda_buffer_meta, list); |
---|
| 346 | + |
---|
| 347 | + /* |
---|
| 348 | + * If we managed to fill in at least a full reorder |
---|
| 349 | + * window of buffers (num_internal_frames is a |
---|
| 350 | + * conservative estimate for this) and the bitstream |
---|
| 351 | + * prefetcher has at least 2 256 bytes periods beyond |
---|
| 352 | + * the first buffer to fetch, we can safely stop queuing |
---|
| 353 | + * in order to limit the decoder drain latency. |
---|
| 354 | + */ |
---|
| 355 | + if (coda_bitstream_can_fetch_past(ctx, meta->end)) |
---|
| 356 | + break; |
---|
| 357 | + } |
---|
271 | 358 | |
---|
272 | 359 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
---|
273 | 360 | |
---|
.. | .. |
---|
299 | 386 | } |
---|
300 | 387 | |
---|
301 | 388 | /* Buffer start position */ |
---|
302 | | - start = ctx->bitstream_fifo.kfifo.in & |
---|
303 | | - ctx->bitstream_fifo.kfifo.mask; |
---|
| 389 | + start = ctx->bitstream_fifo.kfifo.in; |
---|
304 | 390 | |
---|
305 | 391 | if (coda_bitstream_try_queue(ctx, src_buf)) { |
---|
306 | 392 | /* |
---|
.. | .. |
---|
315 | 401 | meta->timecode = src_buf->timecode; |
---|
316 | 402 | meta->timestamp = src_buf->vb2_buf.timestamp; |
---|
317 | 403 | meta->start = start; |
---|
318 | | - meta->end = ctx->bitstream_fifo.kfifo.in & |
---|
319 | | - ctx->bitstream_fifo.kfifo.mask; |
---|
320 | | - spin_lock_irqsave(&ctx->buffer_meta_lock, |
---|
321 | | - flags); |
---|
| 404 | + meta->end = ctx->bitstream_fifo.kfifo.in; |
---|
| 405 | + meta->last = src_buf->flags & V4L2_BUF_FLAG_LAST; |
---|
| 406 | + if (meta->last) |
---|
| 407 | + coda_dbg(1, ctx, "marking last meta"); |
---|
| 408 | + spin_lock(&ctx->buffer_meta_lock); |
---|
322 | 409 | list_add_tail(&meta->list, |
---|
323 | 410 | &ctx->buffer_meta_list); |
---|
324 | 411 | ctx->num_metas++; |
---|
325 | | - spin_unlock_irqrestore(&ctx->buffer_meta_lock, |
---|
326 | | - flags); |
---|
| 412 | + spin_unlock(&ctx->buffer_meta_lock); |
---|
327 | 413 | |
---|
328 | 414 | trace_coda_bit_queue(ctx, src_buf, meta); |
---|
329 | 415 | } |
---|
.. | .. |
---|
383 | 469 | int i; |
---|
384 | 470 | |
---|
385 | 471 | for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++) |
---|
386 | | - coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]); |
---|
| 472 | + coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i].buf); |
---|
387 | 473 | } |
---|
388 | 474 | |
---|
389 | 475 | static int coda_alloc_framebuffers(struct coda_ctx *ctx, |
---|
.. | .. |
---|
423 | 509 | coda_free_framebuffers(ctx); |
---|
424 | 510 | return -ENOMEM; |
---|
425 | 511 | } |
---|
426 | | - ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], |
---|
| 512 | + ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i].buf, |
---|
427 | 513 | size, name); |
---|
428 | 514 | kfree(name); |
---|
429 | 515 | if (ret < 0) { |
---|
.. | .. |
---|
437 | 523 | u32 y, cb, cr, mvcol; |
---|
438 | 524 | |
---|
439 | 525 | /* Start addresses of Y, Cb, Cr planes */ |
---|
440 | | - y = ctx->internal_frames[i].paddr; |
---|
| 526 | + y = ctx->internal_frames[i].buf.paddr; |
---|
441 | 527 | cb = y + ysize; |
---|
442 | 528 | cr = y + ysize + ysize/4; |
---|
443 | 529 | mvcol = y + ysize + ysize/4 + ysize/4; |
---|
.. | .. |
---|
589 | 675 | return 0; |
---|
590 | 676 | } |
---|
591 | 677 | |
---|
| 678 | +static u32 coda_slice_mode(struct coda_ctx *ctx) |
---|
| 679 | +{ |
---|
| 680 | + int size, unit; |
---|
| 681 | + |
---|
| 682 | + switch (ctx->params.slice_mode) { |
---|
| 683 | + case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE: |
---|
| 684 | + default: |
---|
| 685 | + return 0; |
---|
| 686 | + case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_MB: |
---|
| 687 | + size = ctx->params.slice_max_mb; |
---|
| 688 | + unit = 1; |
---|
| 689 | + break; |
---|
| 690 | + case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES: |
---|
| 691 | + size = ctx->params.slice_max_bits; |
---|
| 692 | + unit = 0; |
---|
| 693 | + break; |
---|
| 694 | + } |
---|
| 695 | + |
---|
| 696 | + return ((size & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET) | |
---|
| 697 | + ((unit & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET) | |
---|
| 698 | + ((1 & CODA_SLICING_MODE_MASK) << CODA_SLICING_MODE_OFFSET); |
---|
| 699 | +} |
---|
| 700 | + |
---|
| 701 | +static int coda_enc_param_change(struct coda_ctx *ctx) |
---|
| 702 | +{ |
---|
| 703 | + struct coda_dev *dev = ctx->dev; |
---|
| 704 | + u32 change_enable = 0; |
---|
| 705 | + u32 success; |
---|
| 706 | + int ret; |
---|
| 707 | + |
---|
| 708 | + if (ctx->params.gop_size_changed) { |
---|
| 709 | + change_enable |= CODA_PARAM_CHANGE_RC_GOP; |
---|
| 710 | + coda_write(dev, ctx->params.gop_size, |
---|
| 711 | + CODA_CMD_ENC_PARAM_RC_GOP); |
---|
| 712 | + ctx->gopcounter = ctx->params.gop_size - 1; |
---|
| 713 | + ctx->params.gop_size_changed = false; |
---|
| 714 | + } |
---|
| 715 | + if (ctx->params.h264_intra_qp_changed) { |
---|
| 716 | + coda_dbg(1, ctx, "parameter change: intra Qp %u\n", |
---|
| 717 | + ctx->params.h264_intra_qp); |
---|
| 718 | + |
---|
| 719 | + if (ctx->params.bitrate) { |
---|
| 720 | + change_enable |= CODA_PARAM_CHANGE_RC_INTRA_QP; |
---|
| 721 | + coda_write(dev, ctx->params.h264_intra_qp, |
---|
| 722 | + CODA_CMD_ENC_PARAM_RC_INTRA_QP); |
---|
| 723 | + } |
---|
| 724 | + ctx->params.h264_intra_qp_changed = false; |
---|
| 725 | + } |
---|
| 726 | + if (ctx->params.bitrate_changed) { |
---|
| 727 | + coda_dbg(1, ctx, "parameter change: bitrate %u kbit/s\n", |
---|
| 728 | + ctx->params.bitrate); |
---|
| 729 | + change_enable |= CODA_PARAM_CHANGE_RC_BITRATE; |
---|
| 730 | + coda_write(dev, ctx->params.bitrate, |
---|
| 731 | + CODA_CMD_ENC_PARAM_RC_BITRATE); |
---|
| 732 | + ctx->params.bitrate_changed = false; |
---|
| 733 | + } |
---|
| 734 | + if (ctx->params.framerate_changed) { |
---|
| 735 | + coda_dbg(1, ctx, "parameter change: frame rate %u/%u Hz\n", |
---|
| 736 | + ctx->params.framerate & 0xffff, |
---|
| 737 | + (ctx->params.framerate >> 16) + 1); |
---|
| 738 | + change_enable |= CODA_PARAM_CHANGE_RC_FRAME_RATE; |
---|
| 739 | + coda_write(dev, ctx->params.framerate, |
---|
| 740 | + CODA_CMD_ENC_PARAM_RC_FRAME_RATE); |
---|
| 741 | + ctx->params.framerate_changed = false; |
---|
| 742 | + } |
---|
| 743 | + if (ctx->params.intra_refresh_changed) { |
---|
| 744 | + coda_dbg(1, ctx, "parameter change: intra refresh MBs %u\n", |
---|
| 745 | + ctx->params.intra_refresh); |
---|
| 746 | + change_enable |= CODA_PARAM_CHANGE_INTRA_MB_NUM; |
---|
| 747 | + coda_write(dev, ctx->params.intra_refresh, |
---|
| 748 | + CODA_CMD_ENC_PARAM_INTRA_MB_NUM); |
---|
| 749 | + ctx->params.intra_refresh_changed = false; |
---|
| 750 | + } |
---|
| 751 | + if (ctx->params.slice_mode_changed) { |
---|
| 752 | + change_enable |= CODA_PARAM_CHANGE_SLICE_MODE; |
---|
| 753 | + coda_write(dev, coda_slice_mode(ctx), |
---|
| 754 | + CODA_CMD_ENC_PARAM_SLICE_MODE); |
---|
| 755 | + ctx->params.slice_mode_changed = false; |
---|
| 756 | + } |
---|
| 757 | + |
---|
| 758 | + if (!change_enable) |
---|
| 759 | + return 0; |
---|
| 760 | + |
---|
| 761 | + coda_write(dev, change_enable, CODA_CMD_ENC_PARAM_CHANGE_ENABLE); |
---|
| 762 | + |
---|
| 763 | + ret = coda_command_sync(ctx, CODA_COMMAND_RC_CHANGE_PARAMETER); |
---|
| 764 | + if (ret < 0) |
---|
| 765 | + return ret; |
---|
| 766 | + |
---|
| 767 | + success = coda_read(dev, CODA_RET_ENC_PARAM_CHANGE_SUCCESS); |
---|
| 768 | + if (success != 1) |
---|
| 769 | + coda_dbg(1, ctx, "parameter change failed: %u\n", success); |
---|
| 770 | + |
---|
| 771 | + return 0; |
---|
| 772 | +} |
---|
| 773 | + |
---|
592 | 774 | static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size) |
---|
593 | 775 | { |
---|
594 | 776 | phys_addr_t ret; |
---|
.. | .. |
---|
713 | 895 | |
---|
714 | 896 | out: |
---|
715 | 897 | if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)) |
---|
716 | | - v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
---|
717 | | - "IRAM smaller than needed\n"); |
---|
| 898 | + coda_dbg(1, ctx, "IRAM smaller than needed\n"); |
---|
718 | 899 | |
---|
719 | 900 | if (dev->devtype->product == CODA_HX4 || |
---|
720 | 901 | dev->devtype->product == CODA_7541) { |
---|
.. | .. |
---|
920 | 1101 | break; |
---|
921 | 1102 | case CODA_960: |
---|
922 | 1103 | coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN); |
---|
923 | | - /* fallthrough */ |
---|
| 1104 | + fallthrough; |
---|
924 | 1105 | case CODA_HX4: |
---|
925 | 1106 | case CODA_7541: |
---|
926 | 1107 | coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN | |
---|
.. | .. |
---|
960 | 1141 | CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET; |
---|
961 | 1142 | break; |
---|
962 | 1143 | } |
---|
963 | | - /* fallthrough */ |
---|
| 1144 | + fallthrough; |
---|
964 | 1145 | case CODA_960: |
---|
965 | 1146 | value = (q_data_src->rect.width & CODA7_PICWIDTH_MASK) |
---|
966 | 1147 | << CODA7_PICWIDTH_OFFSET; |
---|
.. | .. |
---|
999 | 1180 | CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) | |
---|
1000 | 1181 | ((ctx->params.h264_slice_beta_offset_div2 & |
---|
1001 | 1182 | CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) << |
---|
1002 | | - CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET); |
---|
| 1183 | + CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET) | |
---|
| 1184 | + (ctx->params.h264_constrained_intra_pred_flag << |
---|
| 1185 | + CODA_264PARAM_CONSTRAINEDINTRAPREDFLAG_OFFSET) | |
---|
| 1186 | + (ctx->params.h264_chroma_qp_index_offset & |
---|
| 1187 | + CODA_264PARAM_CHROMAQPOFFSET_MASK); |
---|
1003 | 1188 | coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA); |
---|
1004 | 1189 | break; |
---|
1005 | 1190 | case V4L2_PIX_FMT_JPEG: |
---|
.. | .. |
---|
1024 | 1209 | * in JPEG mode |
---|
1025 | 1210 | */ |
---|
1026 | 1211 | if (dst_fourcc != V4L2_PIX_FMT_JPEG) { |
---|
1027 | | - switch (ctx->params.slice_mode) { |
---|
1028 | | - case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE: |
---|
1029 | | - value = 0; |
---|
1030 | | - break; |
---|
1031 | | - case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB: |
---|
1032 | | - value = (ctx->params.slice_max_mb & |
---|
1033 | | - CODA_SLICING_SIZE_MASK) |
---|
1034 | | - << CODA_SLICING_SIZE_OFFSET; |
---|
1035 | | - value |= (1 & CODA_SLICING_UNIT_MASK) |
---|
1036 | | - << CODA_SLICING_UNIT_OFFSET; |
---|
1037 | | - value |= 1 & CODA_SLICING_MODE_MASK; |
---|
1038 | | - break; |
---|
1039 | | - case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES: |
---|
1040 | | - value = (ctx->params.slice_max_bits & |
---|
1041 | | - CODA_SLICING_SIZE_MASK) |
---|
1042 | | - << CODA_SLICING_SIZE_OFFSET; |
---|
1043 | | - value |= (0 & CODA_SLICING_UNIT_MASK) |
---|
1044 | | - << CODA_SLICING_UNIT_OFFSET; |
---|
1045 | | - value |= 1 & CODA_SLICING_MODE_MASK; |
---|
1046 | | - break; |
---|
1047 | | - } |
---|
| 1212 | + value = coda_slice_mode(ctx); |
---|
1048 | 1213 | coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE); |
---|
1049 | 1214 | value = ctx->params.gop_size; |
---|
1050 | 1215 | coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE); |
---|
1051 | 1216 | } |
---|
1052 | 1217 | |
---|
1053 | | - if (ctx->params.bitrate) { |
---|
| 1218 | + if (ctx->params.bitrate && (ctx->params.frame_rc_enable || |
---|
| 1219 | + ctx->params.mb_rc_enable)) { |
---|
| 1220 | + ctx->params.bitrate_changed = false; |
---|
| 1221 | + ctx->params.h264_intra_qp_changed = false; |
---|
| 1222 | + |
---|
1054 | 1223 | /* Rate control enabled */ |
---|
1055 | 1224 | value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) |
---|
1056 | 1225 | << CODA_RATECONTROL_BITRATE_OFFSET; |
---|
.. | .. |
---|
1108 | 1277 | } |
---|
1109 | 1278 | coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION); |
---|
1110 | 1279 | |
---|
1111 | | - coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE); |
---|
| 1280 | + if (ctx->params.frame_rc_enable && !ctx->params.mb_rc_enable) |
---|
| 1281 | + value = 1; |
---|
| 1282 | + else |
---|
| 1283 | + value = 0; |
---|
| 1284 | + coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE); |
---|
1112 | 1285 | |
---|
1113 | 1286 | coda_setup_iram(ctx); |
---|
1114 | 1287 | |
---|
.. | .. |
---|
1187 | 1360 | coda9_set_frame_cache(ctx, q_data_src->fourcc); |
---|
1188 | 1361 | |
---|
1189 | 1362 | /* FIXME */ |
---|
1190 | | - coda_write(dev, ctx->internal_frames[2].paddr, |
---|
| 1363 | + coda_write(dev, ctx->internal_frames[2].buf.paddr, |
---|
1191 | 1364 | CODA9_CMD_SET_FRAME_SUBSAMP_A); |
---|
1192 | | - coda_write(dev, ctx->internal_frames[3].paddr, |
---|
| 1365 | + coda_write(dev, ctx->internal_frames[3].buf.paddr, |
---|
1193 | 1366 | CODA9_CMD_SET_FRAME_SUBSAMP_B); |
---|
1194 | 1367 | } |
---|
1195 | 1368 | } |
---|
.. | .. |
---|
1199 | 1372 | v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n"); |
---|
1200 | 1373 | goto out; |
---|
1201 | 1374 | } |
---|
| 1375 | + |
---|
| 1376 | + coda_dbg(1, ctx, "start encoding %dx%d %4.4s->%4.4s @ %d/%d Hz\n", |
---|
| 1377 | + q_data_src->rect.width, q_data_src->rect.height, |
---|
| 1378 | + (char *)&ctx->codec->src_fourcc, (char *)&dst_fourcc, |
---|
| 1379 | + ctx->params.framerate & 0xffff, |
---|
| 1380 | + (ctx->params.framerate >> 16) + 1); |
---|
1202 | 1381 | |
---|
1203 | 1382 | /* Save stream headers */ |
---|
1204 | 1383 | buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
---|
.. | .. |
---|
1299 | 1478 | u32 rot_mode = 0; |
---|
1300 | 1479 | u32 dst_fourcc; |
---|
1301 | 1480 | u32 reg; |
---|
| 1481 | + int ret; |
---|
| 1482 | + |
---|
| 1483 | + ret = coda_enc_param_change(ctx); |
---|
| 1484 | + if (ret < 0) { |
---|
| 1485 | + v4l2_warn(&ctx->dev->v4l2_dev, "parameter change failed: %d\n", |
---|
| 1486 | + ret); |
---|
| 1487 | + } |
---|
1302 | 1488 | |
---|
1303 | 1489 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
---|
1304 | 1490 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
---|
.. | .. |
---|
1435 | 1621 | return 0; |
---|
1436 | 1622 | } |
---|
1437 | 1623 | |
---|
| 1624 | +static char coda_frame_type_char(u32 flags) |
---|
| 1625 | +{ |
---|
| 1626 | + return (flags & V4L2_BUF_FLAG_KEYFRAME) ? 'I' : |
---|
| 1627 | + (flags & V4L2_BUF_FLAG_PFRAME) ? 'P' : |
---|
| 1628 | + (flags & V4L2_BUF_FLAG_BFRAME) ? 'B' : '?'; |
---|
| 1629 | +} |
---|
| 1630 | + |
---|
1438 | 1631 | static void coda_finish_encode(struct coda_ctx *ctx) |
---|
1439 | 1632 | { |
---|
1440 | 1633 | struct vb2_v4l2_buffer *src_buf, *dst_buf; |
---|
1441 | 1634 | struct coda_dev *dev = ctx->dev; |
---|
1442 | 1635 | u32 wr_ptr, start_ptr; |
---|
1443 | 1636 | |
---|
| 1637 | + if (ctx->aborting) |
---|
| 1638 | + return; |
---|
| 1639 | + |
---|
| 1640 | + /* |
---|
| 1641 | + * Lock to make sure that an encoder stop command running in parallel |
---|
| 1642 | + * will either already have marked src_buf as last, or it will wake up |
---|
| 1643 | + * the capture queue after the buffers are returned. |
---|
| 1644 | + */ |
---|
| 1645 | + mutex_lock(&ctx->wakeup_mutex); |
---|
1444 | 1646 | src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
---|
1445 | 1647 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
---|
1446 | 1648 | |
---|
.. | .. |
---|
1461 | 1663 | vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr); |
---|
1462 | 1664 | } |
---|
1463 | 1665 | |
---|
1464 | | - v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n", |
---|
1465 | | - wr_ptr - start_ptr); |
---|
| 1666 | + coda_dbg(1, ctx, "frame size = %u\n", wr_ptr - start_ptr); |
---|
1466 | 1667 | |
---|
1467 | 1668 | coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM); |
---|
1468 | 1669 | coda_read(dev, CODA_RET_ENC_PIC_FLAG); |
---|
1469 | 1670 | |
---|
1470 | | - if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) { |
---|
| 1671 | + dst_buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | |
---|
| 1672 | + V4L2_BUF_FLAG_PFRAME | |
---|
| 1673 | + V4L2_BUF_FLAG_LAST); |
---|
| 1674 | + if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) |
---|
1471 | 1675 | dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME; |
---|
1472 | | - dst_buf->flags &= ~V4L2_BUF_FLAG_PFRAME; |
---|
1473 | | - } else { |
---|
| 1676 | + else |
---|
1474 | 1677 | dst_buf->flags |= V4L2_BUF_FLAG_PFRAME; |
---|
1475 | | - dst_buf->flags &= ~V4L2_BUF_FLAG_KEYFRAME; |
---|
1476 | | - } |
---|
| 1678 | + dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST; |
---|
1477 | 1679 | |
---|
1478 | | - dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp; |
---|
1479 | | - dst_buf->field = src_buf->field; |
---|
1480 | | - dst_buf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK; |
---|
1481 | | - dst_buf->flags |= |
---|
1482 | | - src_buf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK; |
---|
1483 | | - dst_buf->timecode = src_buf->timecode; |
---|
| 1680 | + v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false); |
---|
1484 | 1681 | |
---|
1485 | 1682 | v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); |
---|
1486 | 1683 | |
---|
1487 | 1684 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
---|
1488 | 1685 | coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_DONE); |
---|
| 1686 | + mutex_unlock(&ctx->wakeup_mutex); |
---|
1489 | 1687 | |
---|
1490 | 1688 | ctx->gopcounter--; |
---|
1491 | 1689 | if (ctx->gopcounter < 0) |
---|
1492 | 1690 | ctx->gopcounter = ctx->params.gop_size - 1; |
---|
1493 | 1691 | |
---|
1494 | | - v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
---|
1495 | | - "job finished: encoding frame (%d) (%s)\n", |
---|
1496 | | - dst_buf->sequence, |
---|
1497 | | - (dst_buf->flags & V4L2_BUF_FLAG_KEYFRAME) ? |
---|
1498 | | - "KEYFRAME" : "PFRAME"); |
---|
| 1692 | + coda_dbg(1, ctx, "job finished: encoded %c frame (%d)%s\n", |
---|
| 1693 | + coda_frame_type_char(dst_buf->flags), dst_buf->sequence, |
---|
| 1694 | + (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : ""); |
---|
1499 | 1695 | } |
---|
1500 | 1696 | |
---|
1501 | 1697 | static void coda_seq_end_work(struct work_struct *work) |
---|
.. | .. |
---|
1509 | 1705 | if (ctx->initialized == 0) |
---|
1510 | 1706 | goto out; |
---|
1511 | 1707 | |
---|
1512 | | - v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
---|
1513 | | - "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, |
---|
1514 | | - __func__); |
---|
| 1708 | + coda_dbg(1, ctx, "%s: sent command 'SEQ_END' to coda\n", __func__); |
---|
1515 | 1709 | if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) { |
---|
1516 | 1710 | v4l2_err(&dev->v4l2_dev, |
---|
1517 | 1711 | "CODA_COMMAND_SEQ_END failed\n"); |
---|
.. | .. |
---|
1567 | 1761 | return 0; |
---|
1568 | 1762 | |
---|
1569 | 1763 | ctx->bitstream.size = roundup_pow_of_two(q_data->sizeimage * 2); |
---|
1570 | | - ctx->bitstream.vaddr = dma_alloc_wc(&ctx->dev->plat_dev->dev, |
---|
1571 | | - ctx->bitstream.size, |
---|
| 1764 | + ctx->bitstream.vaddr = dma_alloc_wc(ctx->dev->dev, ctx->bitstream.size, |
---|
1572 | 1765 | &ctx->bitstream.paddr, GFP_KERNEL); |
---|
1573 | 1766 | if (!ctx->bitstream.vaddr) { |
---|
1574 | 1767 | v4l2_err(&ctx->dev->v4l2_dev, |
---|
.. | .. |
---|
1586 | 1779 | if (ctx->bitstream.vaddr == NULL) |
---|
1587 | 1780 | return; |
---|
1588 | 1781 | |
---|
1589 | | - dma_free_wc(&ctx->dev->plat_dev->dev, ctx->bitstream.size, |
---|
1590 | | - ctx->bitstream.vaddr, ctx->bitstream.paddr); |
---|
| 1782 | + dma_free_wc(ctx->dev->dev, ctx->bitstream.size, ctx->bitstream.vaddr, |
---|
| 1783 | + ctx->bitstream.paddr); |
---|
1591 | 1784 | ctx->bitstream.vaddr = NULL; |
---|
1592 | 1785 | kfifo_init(&ctx->bitstream_fifo, NULL, 0); |
---|
1593 | 1786 | } |
---|
.. | .. |
---|
1644 | 1837 | return profile > V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE; |
---|
1645 | 1838 | } |
---|
1646 | 1839 | |
---|
1647 | | -static int __coda_start_decoding(struct coda_ctx *ctx) |
---|
| 1840 | +static int __coda_decoder_seq_init(struct coda_ctx *ctx) |
---|
1648 | 1841 | { |
---|
1649 | 1842 | struct coda_q_data *q_data_src, *q_data_dst; |
---|
1650 | 1843 | u32 bitstream_buf, bitstream_size; |
---|
.. | .. |
---|
1654 | 1847 | u32 val; |
---|
1655 | 1848 | int ret; |
---|
1656 | 1849 | |
---|
1657 | | - v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
---|
1658 | | - "Video Data Order Adapter: %s\n", |
---|
| 1850 | + lockdep_assert_held(&dev->coda_mutex); |
---|
| 1851 | + |
---|
| 1852 | + coda_dbg(1, ctx, "Video Data Order Adapter: %s\n", |
---|
1659 | 1853 | ctx->use_vdoa ? "Enabled" : "Disabled"); |
---|
1660 | 1854 | |
---|
1661 | 1855 | /* Start decoding */ |
---|
.. | .. |
---|
1665 | 1859 | bitstream_size = ctx->bitstream.size; |
---|
1666 | 1860 | src_fourcc = q_data_src->fourcc; |
---|
1667 | 1861 | dst_fourcc = q_data_dst->fourcc; |
---|
1668 | | - |
---|
1669 | | - coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); |
---|
1670 | 1862 | |
---|
1671 | 1863 | /* Update coda bitstream read and write pointers from kfifo */ |
---|
1672 | 1864 | coda_kfifo_sync_to_device_full(ctx); |
---|
.. | .. |
---|
1736 | 1928 | |
---|
1737 | 1929 | if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) { |
---|
1738 | 1930 | v4l2_err(&dev->v4l2_dev, |
---|
1739 | | - "CODA_COMMAND_SEQ_INIT failed, error code = %d\n", |
---|
| 1931 | + "CODA_COMMAND_SEQ_INIT failed, error code = 0x%x\n", |
---|
1740 | 1932 | coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON)); |
---|
1741 | 1933 | return -EAGAIN; |
---|
1742 | 1934 | } |
---|
.. | .. |
---|
1760 | 1952 | width = round_up(width, 16); |
---|
1761 | 1953 | height = round_up(height, 16); |
---|
1762 | 1954 | |
---|
1763 | | - v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n", |
---|
1764 | | - __func__, ctx->idx, width, height); |
---|
| 1955 | + coda_dbg(1, ctx, "start decoding: %dx%d\n", width, height); |
---|
1765 | 1956 | |
---|
1766 | 1957 | ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED); |
---|
1767 | 1958 | /* |
---|
.. | .. |
---|
1795 | 1986 | (top_bottom & 0x3ff); |
---|
1796 | 1987 | } |
---|
1797 | 1988 | |
---|
| 1989 | + if (dev->devtype->product != CODA_DX6) { |
---|
| 1990 | + u8 profile, level; |
---|
| 1991 | + |
---|
| 1992 | + val = coda_read(dev, CODA7_RET_DEC_SEQ_HEADER_REPORT); |
---|
| 1993 | + profile = val & 0xff; |
---|
| 1994 | + level = (val >> 8) & 0x7f; |
---|
| 1995 | + |
---|
| 1996 | + if (profile || level) |
---|
| 1997 | + coda_update_profile_level_ctrls(ctx, profile, level); |
---|
| 1998 | + } |
---|
| 1999 | + |
---|
| 2000 | + return 0; |
---|
| 2001 | +} |
---|
| 2002 | + |
---|
| 2003 | +static void coda_dec_seq_init_work(struct work_struct *work) |
---|
| 2004 | +{ |
---|
| 2005 | + struct coda_ctx *ctx = container_of(work, |
---|
| 2006 | + struct coda_ctx, seq_init_work); |
---|
| 2007 | + struct coda_dev *dev = ctx->dev; |
---|
| 2008 | + int ret; |
---|
| 2009 | + |
---|
| 2010 | + mutex_lock(&ctx->buffer_mutex); |
---|
| 2011 | + mutex_lock(&dev->coda_mutex); |
---|
| 2012 | + |
---|
| 2013 | + if (ctx->initialized == 1) |
---|
| 2014 | + goto out; |
---|
| 2015 | + |
---|
| 2016 | + ret = __coda_decoder_seq_init(ctx); |
---|
| 2017 | + if (ret < 0) |
---|
| 2018 | + goto out; |
---|
| 2019 | + |
---|
| 2020 | + ctx->initialized = 1; |
---|
| 2021 | + |
---|
| 2022 | +out: |
---|
| 2023 | + mutex_unlock(&dev->coda_mutex); |
---|
| 2024 | + mutex_unlock(&ctx->buffer_mutex); |
---|
| 2025 | +} |
---|
| 2026 | + |
---|
| 2027 | +static int __coda_start_decoding(struct coda_ctx *ctx) |
---|
| 2028 | +{ |
---|
| 2029 | + struct coda_q_data *q_data_src, *q_data_dst; |
---|
| 2030 | + struct coda_dev *dev = ctx->dev; |
---|
| 2031 | + u32 src_fourcc, dst_fourcc; |
---|
| 2032 | + int ret; |
---|
| 2033 | + |
---|
| 2034 | + q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
---|
| 2035 | + q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
---|
| 2036 | + src_fourcc = q_data_src->fourcc; |
---|
| 2037 | + dst_fourcc = q_data_dst->fourcc; |
---|
| 2038 | + |
---|
| 2039 | + if (!ctx->initialized) { |
---|
| 2040 | + ret = __coda_decoder_seq_init(ctx); |
---|
| 2041 | + if (ret < 0) |
---|
| 2042 | + return ret; |
---|
| 2043 | + } else { |
---|
| 2044 | + ctx->frame_mem_ctrl &= ~(CODA_FRAME_CHROMA_INTERLEAVE | (0x3 << 9) | |
---|
| 2045 | + CODA9_FRAME_TILED2LINEAR); |
---|
| 2046 | + if (dst_fourcc == V4L2_PIX_FMT_NV12 || dst_fourcc == V4L2_PIX_FMT_YUYV) |
---|
| 2047 | + ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE; |
---|
| 2048 | + if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP) |
---|
| 2049 | + ctx->frame_mem_ctrl |= (0x3 << 9) | |
---|
| 2050 | + ((ctx->use_vdoa) ? 0 : CODA9_FRAME_TILED2LINEAR); |
---|
| 2051 | + } |
---|
| 2052 | + |
---|
| 2053 | + coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); |
---|
| 2054 | + |
---|
1798 | 2055 | ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc); |
---|
1799 | 2056 | if (ret < 0) { |
---|
1800 | 2057 | v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n"); |
---|
.. | .. |
---|
1803 | 2060 | |
---|
1804 | 2061 | /* Tell the decoder how many frame buffers we allocated. */ |
---|
1805 | 2062 | coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM); |
---|
1806 | | - coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE); |
---|
| 2063 | + coda_write(dev, round_up(q_data_dst->rect.width, 16), |
---|
| 2064 | + CODA_CMD_SET_FRAME_BUF_STRIDE); |
---|
1807 | 2065 | |
---|
1808 | 2066 | if (dev->devtype->product != CODA_DX6) { |
---|
1809 | 2067 | /* Set secondary AXI IRAM */ |
---|
.. | .. |
---|
1879 | 2137 | struct coda_dev *dev = ctx->dev; |
---|
1880 | 2138 | struct coda_q_data *q_data_dst; |
---|
1881 | 2139 | struct coda_buffer_meta *meta; |
---|
1882 | | - unsigned long flags; |
---|
1883 | 2140 | u32 rot_mode = 0; |
---|
1884 | 2141 | u32 reg_addr, reg_stride; |
---|
1885 | 2142 | |
---|
.. | .. |
---|
1893 | 2150 | |
---|
1894 | 2151 | if (coda_get_bitstream_payload(ctx) < 512 && |
---|
1895 | 2152 | (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) { |
---|
1896 | | - v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
---|
1897 | | - "bitstream payload: %d, skipping\n", |
---|
| 2153 | + coda_dbg(1, ctx, "bitstream payload: %d, skipping\n", |
---|
1898 | 2154 | coda_get_bitstream_payload(ctx)); |
---|
1899 | 2155 | v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx); |
---|
1900 | 2156 | return -EAGAIN; |
---|
.. | .. |
---|
1921 | 2177 | ctx->display_idx < ctx->num_internal_frames) { |
---|
1922 | 2178 | vdoa_device_run(ctx->vdoa, |
---|
1923 | 2179 | vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0), |
---|
1924 | | - ctx->internal_frames[ctx->display_idx].paddr); |
---|
| 2180 | + ctx->internal_frames[ctx->display_idx].buf.paddr); |
---|
1925 | 2181 | } else { |
---|
1926 | 2182 | if (dev->devtype->product == CODA_960) { |
---|
1927 | 2183 | /* |
---|
1928 | | - * The CODA960 seems to have an internal list of |
---|
1929 | | - * buffers with 64 entries that includes the |
---|
1930 | | - * registered frame buffers as well as the rotator |
---|
1931 | | - * buffer output. |
---|
1932 | | - * |
---|
1933 | | - * ROT_INDEX needs to be < 0x40, but > |
---|
1934 | | - * ctx->num_internal_frames. |
---|
| 2184 | + * It was previously assumed that the CODA960 has an |
---|
| 2185 | + * internal list of 64 buffer entries that contains |
---|
| 2186 | + * both the registered internal frame buffers as well |
---|
| 2187 | + * as the rotator buffer output, and that the ROT_INDEX |
---|
| 2188 | + * register must be set to a value between the last |
---|
| 2189 | + * internal frame buffers' index and 64. |
---|
| 2190 | + * At least on firmware version 3.1.1 it turns out that |
---|
| 2191 | + * setting ROT_INDEX to any value >= 32 causes CODA |
---|
| 2192 | + * hangups that it can not recover from with the SRC VPU |
---|
| 2193 | + * reset. |
---|
| 2194 | + * It does appear to work however, to just set it to a |
---|
| 2195 | + * fixed value in the [ctx->num_internal_frames, 31] |
---|
| 2196 | + * range, for example CODA_MAX_FRAMEBUFFERS. |
---|
1935 | 2197 | */ |
---|
1936 | | - coda_write(dev, |
---|
1937 | | - CODA_MAX_FRAMEBUFFERS + dst_buf->vb2_buf.index, |
---|
| 2198 | + coda_write(dev, CODA_MAX_FRAMEBUFFERS, |
---|
1938 | 2199 | CODA9_CMD_DEC_PIC_ROT_INDEX); |
---|
1939 | 2200 | |
---|
1940 | 2201 | reg_addr = CODA9_CMD_DEC_PIC_ROT_ADDR_Y; |
---|
.. | .. |
---|
1973 | 2234 | coda_write(dev, ctx->iram_info.axi_sram_use, |
---|
1974 | 2235 | CODA7_REG_BIT_AXI_SRAM_USE); |
---|
1975 | 2236 | |
---|
1976 | | - spin_lock_irqsave(&ctx->buffer_meta_lock, flags); |
---|
| 2237 | + spin_lock(&ctx->buffer_meta_lock); |
---|
1977 | 2238 | meta = list_first_entry_or_null(&ctx->buffer_meta_list, |
---|
1978 | 2239 | struct coda_buffer_meta, list); |
---|
1979 | 2240 | |
---|
1980 | 2241 | if (meta && ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) { |
---|
1981 | 2242 | |
---|
1982 | 2243 | /* If this is the last buffer in the bitstream, add padding */ |
---|
1983 | | - if (meta->end == (ctx->bitstream_fifo.kfifo.in & |
---|
1984 | | - ctx->bitstream_fifo.kfifo.mask)) { |
---|
| 2244 | + if (meta->end == ctx->bitstream_fifo.kfifo.in) { |
---|
1985 | 2245 | static unsigned char buf[512]; |
---|
1986 | 2246 | unsigned int pad; |
---|
1987 | 2247 | |
---|
.. | .. |
---|
1993 | 2253 | kfifo_in(&ctx->bitstream_fifo, buf, pad); |
---|
1994 | 2254 | } |
---|
1995 | 2255 | } |
---|
1996 | | - spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags); |
---|
| 2256 | + spin_unlock(&ctx->buffer_meta_lock); |
---|
1997 | 2257 | |
---|
1998 | 2258 | coda_kfifo_sync_to_device_full(ctx); |
---|
1999 | 2259 | |
---|
.. | .. |
---|
2017 | 2277 | struct coda_q_data *q_data_dst; |
---|
2018 | 2278 | struct vb2_v4l2_buffer *dst_buf; |
---|
2019 | 2279 | struct coda_buffer_meta *meta; |
---|
2020 | | - unsigned long payload; |
---|
2021 | | - unsigned long flags; |
---|
2022 | 2280 | int width, height; |
---|
2023 | 2281 | int decoded_idx; |
---|
2024 | 2282 | int display_idx; |
---|
| 2283 | + struct coda_internal_frame *decoded_frame = NULL; |
---|
2025 | 2284 | u32 src_fourcc; |
---|
2026 | 2285 | int success; |
---|
2027 | 2286 | u32 err_mb; |
---|
2028 | 2287 | int err_vdoa = 0; |
---|
2029 | 2288 | u32 val; |
---|
| 2289 | + |
---|
| 2290 | + if (ctx->aborting) |
---|
| 2291 | + return; |
---|
2030 | 2292 | |
---|
2031 | 2293 | /* Update kfifo out pointer from coda bitstream read pointer */ |
---|
2032 | 2294 | coda_kfifo_sync_from_device(ctx); |
---|
.. | .. |
---|
2103 | 2365 | val = coda_read(dev, CODA_RET_DEC_PIC_OPTION); |
---|
2104 | 2366 | if (val == 0) { |
---|
2105 | 2367 | /* not enough bitstream data */ |
---|
2106 | | - v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
---|
2107 | | - "prescan failed: %d\n", val); |
---|
| 2368 | + coda_dbg(1, ctx, "prescan failed: %d\n", val); |
---|
2108 | 2369 | ctx->hold = true; |
---|
2109 | 2370 | return; |
---|
2110 | 2371 | } |
---|
.. | .. |
---|
2151 | 2412 | v4l2_err(&dev->v4l2_dev, |
---|
2152 | 2413 | "decoded frame index out of range: %d\n", decoded_idx); |
---|
2153 | 2414 | } else { |
---|
| 2415 | + decoded_frame = &ctx->internal_frames[decoded_idx]; |
---|
| 2416 | + |
---|
2154 | 2417 | val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM); |
---|
2155 | 2418 | if (ctx->sequence_offset == -1) |
---|
2156 | 2419 | ctx->sequence_offset = val; |
---|
2157 | 2420 | val -= ctx->sequence_offset; |
---|
2158 | | - spin_lock_irqsave(&ctx->buffer_meta_lock, flags); |
---|
| 2421 | + spin_lock(&ctx->buffer_meta_lock); |
---|
2159 | 2422 | if (!list_empty(&ctx->buffer_meta_list)) { |
---|
2160 | 2423 | meta = list_first_entry(&ctx->buffer_meta_list, |
---|
2161 | 2424 | struct coda_buffer_meta, list); |
---|
2162 | 2425 | list_del(&meta->list); |
---|
2163 | 2426 | ctx->num_metas--; |
---|
2164 | | - spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags); |
---|
| 2427 | + spin_unlock(&ctx->buffer_meta_lock); |
---|
2165 | 2428 | /* |
---|
2166 | 2429 | * Clamp counters to 16 bits for comparison, as the HW |
---|
2167 | 2430 | * counter rolls over at this point for h.264. This |
---|
.. | .. |
---|
2175 | 2438 | val, ctx->sequence_offset, |
---|
2176 | 2439 | meta->sequence); |
---|
2177 | 2440 | } |
---|
2178 | | - ctx->frame_metas[decoded_idx] = *meta; |
---|
| 2441 | + decoded_frame->meta = *meta; |
---|
2179 | 2442 | kfree(meta); |
---|
2180 | 2443 | } else { |
---|
2181 | | - spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags); |
---|
| 2444 | + spin_unlock(&ctx->buffer_meta_lock); |
---|
2182 | 2445 | v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n"); |
---|
2183 | | - memset(&ctx->frame_metas[decoded_idx], 0, |
---|
| 2446 | + memset(&decoded_frame->meta, 0, |
---|
2184 | 2447 | sizeof(struct coda_buffer_meta)); |
---|
2185 | | - ctx->frame_metas[decoded_idx].sequence = val; |
---|
| 2448 | + decoded_frame->meta.sequence = val; |
---|
| 2449 | + decoded_frame->meta.last = false; |
---|
2186 | 2450 | ctx->sequence_offset++; |
---|
2187 | 2451 | } |
---|
2188 | 2452 | |
---|
2189 | | - trace_coda_dec_pic_done(ctx, &ctx->frame_metas[decoded_idx]); |
---|
| 2453 | + trace_coda_dec_pic_done(ctx, &decoded_frame->meta); |
---|
2190 | 2454 | |
---|
2191 | 2455 | val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7; |
---|
2192 | | - if (val == 0) |
---|
2193 | | - ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME; |
---|
2194 | | - else if (val == 1) |
---|
2195 | | - ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME; |
---|
2196 | | - else |
---|
2197 | | - ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME; |
---|
| 2456 | + decoded_frame->type = (val == 0) ? V4L2_BUF_FLAG_KEYFRAME : |
---|
| 2457 | + (val == 1) ? V4L2_BUF_FLAG_PFRAME : |
---|
| 2458 | + V4L2_BUF_FLAG_BFRAME; |
---|
2198 | 2459 | |
---|
2199 | | - ctx->frame_errors[decoded_idx] = err_mb; |
---|
| 2460 | + decoded_frame->error = err_mb; |
---|
2200 | 2461 | } |
---|
2201 | 2462 | |
---|
2202 | 2463 | if (display_idx == -1) { |
---|
.. | .. |
---|
2216 | 2477 | /* If a frame was copied out, return it */ |
---|
2217 | 2478 | if (ctx->display_idx >= 0 && |
---|
2218 | 2479 | ctx->display_idx < ctx->num_internal_frames) { |
---|
| 2480 | + struct coda_internal_frame *ready_frame; |
---|
| 2481 | + |
---|
| 2482 | + ready_frame = &ctx->internal_frames[ctx->display_idx]; |
---|
| 2483 | + |
---|
2219 | 2484 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
---|
2220 | 2485 | dst_buf->sequence = ctx->osequence++; |
---|
2221 | 2486 | |
---|
.. | .. |
---|
2223 | 2488 | dst_buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | |
---|
2224 | 2489 | V4L2_BUF_FLAG_PFRAME | |
---|
2225 | 2490 | V4L2_BUF_FLAG_BFRAME); |
---|
2226 | | - dst_buf->flags |= ctx->frame_types[ctx->display_idx]; |
---|
2227 | | - meta = &ctx->frame_metas[ctx->display_idx]; |
---|
| 2491 | + dst_buf->flags |= ready_frame->type; |
---|
| 2492 | + meta = &ready_frame->meta; |
---|
| 2493 | + if (meta->last && !coda_reorder_enable(ctx)) { |
---|
| 2494 | + /* |
---|
| 2495 | + * If this was the last decoded frame, and reordering |
---|
| 2496 | + * is disabled, this will be the last display frame. |
---|
| 2497 | + */ |
---|
| 2498 | + coda_dbg(1, ctx, "last meta, marking as last frame\n"); |
---|
| 2499 | + dst_buf->flags |= V4L2_BUF_FLAG_LAST; |
---|
| 2500 | + } else if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG && |
---|
| 2501 | + display_idx == -1) { |
---|
| 2502 | + /* |
---|
| 2503 | + * If there is no designated presentation frame anymore, |
---|
| 2504 | + * this frame has to be the last one. |
---|
| 2505 | + */ |
---|
| 2506 | + coda_dbg(1, ctx, |
---|
| 2507 | + "no more frames to return, marking as last frame\n"); |
---|
| 2508 | + dst_buf->flags |= V4L2_BUF_FLAG_LAST; |
---|
| 2509 | + } |
---|
2228 | 2510 | dst_buf->timecode = meta->timecode; |
---|
2229 | 2511 | dst_buf->vb2_buf.timestamp = meta->timestamp; |
---|
2230 | 2512 | |
---|
2231 | 2513 | trace_coda_dec_rot_done(ctx, dst_buf, meta); |
---|
2232 | 2514 | |
---|
2233 | | - switch (q_data_dst->fourcc) { |
---|
2234 | | - case V4L2_PIX_FMT_YUYV: |
---|
2235 | | - payload = width * height * 2; |
---|
2236 | | - break; |
---|
2237 | | - case V4L2_PIX_FMT_YUV420: |
---|
2238 | | - case V4L2_PIX_FMT_YVU420: |
---|
2239 | | - case V4L2_PIX_FMT_NV12: |
---|
2240 | | - default: |
---|
2241 | | - payload = width * height * 3 / 2; |
---|
2242 | | - break; |
---|
2243 | | - case V4L2_PIX_FMT_YUV422P: |
---|
2244 | | - payload = width * height * 2; |
---|
2245 | | - break; |
---|
2246 | | - } |
---|
2247 | | - vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload); |
---|
| 2515 | + vb2_set_plane_payload(&dst_buf->vb2_buf, 0, |
---|
| 2516 | + q_data_dst->sizeimage); |
---|
2248 | 2517 | |
---|
2249 | | - if (ctx->frame_errors[ctx->display_idx] || err_vdoa) |
---|
| 2518 | + if (ready_frame->error || err_vdoa) |
---|
2250 | 2519 | coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_ERROR); |
---|
2251 | 2520 | else |
---|
2252 | 2521 | coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_DONE); |
---|
2253 | 2522 | |
---|
2254 | | - v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
---|
2255 | | - "job finished: decoding frame (%d) (%s)\n", |
---|
2256 | | - dst_buf->sequence, |
---|
2257 | | - (dst_buf->flags & V4L2_BUF_FLAG_KEYFRAME) ? |
---|
2258 | | - "KEYFRAME" : "PFRAME"); |
---|
| 2523 | + if (decoded_frame) { |
---|
| 2524 | + coda_dbg(1, ctx, "job finished: decoded %c frame %u, returned %c frame %u (%u/%u)%s\n", |
---|
| 2525 | + coda_frame_type_char(decoded_frame->type), |
---|
| 2526 | + decoded_frame->meta.sequence, |
---|
| 2527 | + coda_frame_type_char(dst_buf->flags), |
---|
| 2528 | + ready_frame->meta.sequence, |
---|
| 2529 | + dst_buf->sequence, ctx->qsequence, |
---|
| 2530 | + (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? |
---|
| 2531 | + " (last)" : ""); |
---|
| 2532 | + } else { |
---|
| 2533 | + coda_dbg(1, ctx, "job finished: no frame decoded (%d), returned %c frame %u (%u/%u)%s\n", |
---|
| 2534 | + decoded_idx, |
---|
| 2535 | + coda_frame_type_char(dst_buf->flags), |
---|
| 2536 | + ready_frame->meta.sequence, |
---|
| 2537 | + dst_buf->sequence, ctx->qsequence, |
---|
| 2538 | + (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? |
---|
| 2539 | + " (last)" : ""); |
---|
| 2540 | + } |
---|
2259 | 2541 | } else { |
---|
2260 | | - v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
---|
2261 | | - "job finished: no frame decoded\n"); |
---|
| 2542 | + if (decoded_frame) { |
---|
| 2543 | + coda_dbg(1, ctx, "job finished: decoded %c frame %u, no frame returned (%d)\n", |
---|
| 2544 | + coda_frame_type_char(decoded_frame->type), |
---|
| 2545 | + decoded_frame->meta.sequence, |
---|
| 2546 | + ctx->display_idx); |
---|
| 2547 | + } else { |
---|
| 2548 | + coda_dbg(1, ctx, "job finished: no frame decoded (%d) or returned (%d)\n", |
---|
| 2549 | + decoded_idx, ctx->display_idx); |
---|
| 2550 | + } |
---|
2262 | 2551 | } |
---|
2263 | 2552 | |
---|
2264 | 2553 | /* The rotator will copy the current display frame next time */ |
---|
2265 | 2554 | ctx->display_idx = display_idx; |
---|
| 2555 | + |
---|
| 2556 | + /* |
---|
| 2557 | + * The current decode run might have brought the bitstream fill level |
---|
| 2558 | + * below the size where we can start the next decode run. As userspace |
---|
| 2559 | + * might have filled the output queue completely and might thus be |
---|
| 2560 | + * blocked, we can't rely on the next qbuf to trigger the bitstream |
---|
| 2561 | + * refill. Check if we have data to refill the bitstream now. |
---|
| 2562 | + */ |
---|
| 2563 | + mutex_lock(&ctx->bitstream_mutex); |
---|
| 2564 | + coda_fill_bitstream(ctx, NULL); |
---|
| 2565 | + mutex_unlock(&ctx->bitstream_mutex); |
---|
2266 | 2566 | } |
---|
2267 | 2567 | |
---|
2268 | 2568 | static void coda_decode_timeout(struct coda_ctx *ctx) |
---|
.. | .. |
---|
2291 | 2591 | .prepare_run = coda_prepare_decode, |
---|
2292 | 2592 | .finish_run = coda_finish_decode, |
---|
2293 | 2593 | .run_timeout = coda_decode_timeout, |
---|
| 2594 | + .seq_init_work = coda_dec_seq_init_work, |
---|
2294 | 2595 | .seq_end_work = coda_seq_end_work, |
---|
2295 | 2596 | .release = coda_bit_release, |
---|
2296 | 2597 | }; |
---|
.. | .. |
---|
2302 | 2603 | |
---|
2303 | 2604 | /* read status register to attend the IRQ */ |
---|
2304 | 2605 | coda_read(dev, CODA_REG_BIT_INT_STATUS); |
---|
| 2606 | + coda_write(dev, 0, CODA_REG_BIT_INT_REASON); |
---|
2305 | 2607 | coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET, |
---|
2306 | 2608 | CODA_REG_BIT_INT_CLEAR); |
---|
2307 | 2609 | |
---|
.. | .. |
---|
2315 | 2617 | trace_coda_bit_done(ctx); |
---|
2316 | 2618 | |
---|
2317 | 2619 | if (ctx->aborting) { |
---|
2318 | | - v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
---|
2319 | | - "task has been aborted\n"); |
---|
| 2620 | + coda_dbg(1, ctx, "task has been aborted\n"); |
---|
2320 | 2621 | } |
---|
2321 | 2622 | |
---|
2322 | 2623 | if (coda_isbusy(ctx->dev)) { |
---|
2323 | | - v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
---|
2324 | | - "coda is still busy!!!!\n"); |
---|
| 2624 | + coda_dbg(1, ctx, "coda is still busy!!!!\n"); |
---|
2325 | 2625 | return IRQ_NONE; |
---|
2326 | 2626 | } |
---|
2327 | 2627 | |
---|