int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, AVPacket *avpkt) { QSVFrame *out_frame; mfxFrameSurface1 *insurf; mfxFrameSurface1 *outsurf; mfxSyncPoint sync; mfxBitstream bs = { { { 0 } } }; int ret; int n_out_frames; int buffered = 0; if (!q->engine_ready) { ret = ff_qsv_decode_init(avctx, q, avpkt); if (ret) return ret; } if (avpkt->size ) { if (av_fifo_size(q->input_fifo)) { /* we have got rest of previous packet into buffer */ if (av_fifo_space(q->input_fifo) < avpkt->size) { ret = av_fifo_grow(q->input_fifo, avpkt->size); if (ret < 0) return ret; } av_fifo_generic_write(q->input_fifo, avpkt->data, avpkt->size, NULL); bs.Data = q->input_fifo->rptr; bs.DataLength = av_fifo_size(q->input_fifo); buffered = 1; } else { bs.Data = avpkt->data; bs.DataLength = avpkt->size; } bs.MaxLength = bs.DataLength; bs.TimeStamp = avpkt->pts; } while (1) { ret = get_surface(avctx, q, &insurf); if (ret < 0) return ret; do { ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL, insurf, &outsurf, &sync); if (ret != MFX_WRN_DEVICE_BUSY) break; av_usleep(500); } while (1); if (MFX_WRN_VIDEO_PARAM_CHANGED==ret) { /* TODO: handle here sequence header changing */ } if (sync) { QSVFrame *out_frame = find_frame(q, outsurf); if (!out_frame) { av_log(avctx, AV_LOG_ERROR, "The returned surface does not correspond to any frame\n"); return AVERROR_BUG; } out_frame->queued = 1; av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL); av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL); continue; } if (MFX_ERR_MORE_SURFACE != ret && ret < 0) break; } /* make sure we do not enter an infinite loop if the SDK * did not consume any data and did not return anything */ if (!sync && !bs.DataOffset) { av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n"); bs.DataOffset = avpkt->size; } if (buffered) { qsv_fifo_relocate(q->input_fifo, bs.DataOffset); } else if (bs.DataOffset!=avpkt->size) { /* some data of packet was not consumed. store it to local buffer */ av_fifo_generic_write(q->input_fifo, avpkt->data+bs.DataOffset, avpkt->size - bs.DataOffset, NULL); } if (MFX_ERR_MORE_DATA!=ret && ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error %d during QSV decoding.\n", ret); return ff_qsv_error(ret); } n_out_frames = av_fifo_size(q->async_fifo) / (sizeof(out_frame)+sizeof(sync)); if (n_out_frames > q->async_depth || (!avpkt->size && n_out_frames) ) { AVFrame *src_frame; av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL); av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL); out_frame->queued = 0; MFXVideoCORE_SyncOperation(q->session, sync, 60000); src_frame = out_frame->frame; ret = av_frame_ref(frame, src_frame); if (ret < 0) return ret; outsurf = out_frame->surface; frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp; frame->repeat_pict = outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 : outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 : outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0; frame->top_field_first = outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF; frame->interlaced_frame = !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE); *got_frame = 1; } return avpkt->size; }
int main(void) { /* create a FIFO buffer */ AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int)); int i, j, n, *p; /* fill data */ for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) av_fifo_generic_write(fifo, &i, sizeof(int), NULL); /* peek at FIFO */ n = av_fifo_size(fifo) / sizeof(int); for (i = -n + 1; i < n; i++) { int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int)); printf("%d: %d\n", i, *v); } printf("\n"); /* peek_at at FIFO */ n = av_fifo_size(fifo) / sizeof(int); for (i = 0; i < n; i++) { av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL); printf("%d: %d\n", i, j); } printf("\n"); /* generic peek at FIFO */ n = av_fifo_size(fifo); p = malloc(n); if (p == NULL) { fprintf(stderr, "failed to allocate memory.\n"); exit(1); } (void) av_fifo_generic_peek(fifo, p, n, NULL); /* read data at p */ n /= sizeof(int); for(i = 0; i < n; ++i) printf("%d: %d\n", i, p[i]); putchar('\n'); /* read data */ for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) { av_fifo_generic_read(fifo, &j, sizeof(int), NULL); printf("%d ", j); } printf("\n"); /* test *ndx overflow */ av_fifo_reset(fifo); fifo->rndx = fifo->wndx = ~(uint32_t)0 - 5; /* fill data */ for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) av_fifo_generic_write(fifo, &i, sizeof(int), NULL); /* peek_at at FIFO */ n = av_fifo_size(fifo) / sizeof(int); for (i = 0; i < n; i++) { av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL); printf("%d: %d\n", i, j); } putchar('\n'); /* test fifo_grow */ (void) av_fifo_grow(fifo, 15 * sizeof(int)); /* fill data */ n = av_fifo_size(fifo) / sizeof(int); for (i = n; av_fifo_space(fifo) >= sizeof(int); ++i) av_fifo_generic_write(fifo, &i, sizeof(int), NULL); /* peek_at at FIFO */ n = av_fifo_size(fifo) / sizeof(int); for (i = 0; i < n; i++) { av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL); printf("%d: %d\n", i, j); } av_fifo_free(fifo); free(p); return 0; }