| 1 |
/*############################################################################ |
| 2 |
# Copyright (C) 2020 Intel Corporation |
| 3 |
# |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
############################################################################*/ |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#include "mfxdefs.h" |
| 11 |
#include "mfxvideo.h" |
| 12 |
#include "mfxmvc.h" |
| 13 |
|
| 14 |
/* These macro required for code compilation. */ |
| 15 |
#define INFINITE 0x7FFFFFFF |
| 16 |
#define UNUSED_PARAM(x) (void)(x) |
| 17 |
|
| 18 |
mfxSession session; |
| 19 |
mfxBitstream *bitstream, *bits; |
| 20 |
mfxVideoParam init_param, param; |
| 21 |
mfxFrameAllocRequest request; |
| 22 |
mfxStatus sts; |
| 23 |
mfxFrameSurface1* work, *disp; |
| 24 |
mfxSyncPoint syncp; |
| 25 |
|
| 26 |
// Define a structure to encapsulate both the linked list node and the size of the list |
| 27 |
typedef struct SyncPointList { |
| 28 |
mfxSyncPoint syncPoint; // Store sync point |
| 29 |
mfxFrameSurface1* frameSurface; // Store frame surface pointer |
| 30 |
struct SyncPointList* next; // Pointer to the next node in the list |
| 31 |
int size; // Size of the linked list |
| 32 |
} SyncPointList; |
| 33 |
|
| 34 |
// Initialize the list |
| 35 |
void init_sync_point_list(SyncPointList* list) { |
| 36 |
list->next = NULL; // Set the next pointer to NULL (empty list) |
| 37 |
list->size = 0; // Initialize the size to 0 |
| 38 |
} |
| 39 |
|
| 40 |
// Add a sync point and frame surface to the end of the list |
| 41 |
void push_sync_point(SyncPointList* list, mfxSyncPoint syncPoint, mfxFrameSurface1* frameSurface) { |
| 42 |
// Traverse the list to find the last node |
| 43 |
SyncPointList* temp = list; |
| 44 |
while (temp->next != NULL) { |
| 45 |
temp = temp->next; |
| 46 |
} |
| 47 |
|
| 48 |
// Allocate memory for the new node |
| 49 |
SyncPointList* newNode = (SyncPointList*)malloc(sizeof(SyncPointList)); |
| 50 |
newNode->syncPoint = syncPoint; |
| 51 |
newNode->frameSurface = frameSurface; |
| 52 |
newNode->next = NULL; // The new node's next pointer is NULL |
| 53 |
|
| 54 |
// Link the new node to the end of the list |
| 55 |
temp->next = newNode; |
| 56 |
|
| 57 |
// Update the size of the list |
| 58 |
list->size++; |
| 59 |
} |
| 60 |
|
| 61 |
// Remove a sync point and frame surface from the head of the list |
| 62 |
void pop_sync_point(SyncPointList* list) { |
| 63 |
if (list->next != NULL) { // If the list is not empty |
| 64 |
SyncPointList* temp = list->next; |
| 65 |
list->next = list->next->next; // Update the head to the next node |
| 66 |
free(temp); // Free the old head node |
| 67 |
|
| 68 |
// Update the size of the list |
| 69 |
list->size--; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
static void allocate_pool_of_frame_surfaces(int nFrames) |
| 74 |
{ |
| 75 |
UNUSED_PARAM(nFrames); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
static int end_of_stream() |
| 80 |
{ |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
static void append_more_bitstream(mfxBitstream *bs) |
| 85 |
{ |
| 86 |
UNUSED_PARAM(bs); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
static void find_free_surface_from_the_pool(mfxFrameSurface1 **pool) |
| 91 |
{ |
| 92 |
UNUSED_PARAM(pool); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
static void realloc_surface(mfxFrameSurface1* work, mfxFrameInfo FrameInfo) |
| 97 |
{ |
| 98 |
UNUSED_PARAM(work); |
| 99 |
UNUSED_PARAM(FrameInfo); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
static void do_something_with_decoded_frame(mfxFrameSurface1* disp) |
| 104 |
{ |
| 105 |
UNUSED_PARAM(disp); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
static void free_pool_of_frame_surfaces() |
| 110 |
{ |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
static void add_surface_to_pool(mfxFrameSurface1* surf) |
| 115 |
{ |
| 116 |
UNUSED_PARAM(surf); |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
static void get_next_surface_from_pool(mfxFrameSurface1 **surface) |
| 121 |
{ |
| 122 |
UNUSED_PARAM(surface); |
| 123 |
return; |
| 124 |
} |
| 125 |
/* end of internal stuff */ |
| 126 |
|
| 127 |
static void prg_decoding1 () { |
| 128 |
/*beg1*/ |
| 129 |
MFXVideoDECODE_Init(session, &init_param); |
| 130 |
sts=MFX_ERR_MORE_DATA; |
| 131 |
for (;;) { |
| 132 |
if (sts==MFX_ERR_MORE_DATA && !end_of_stream()) |
| 133 |
append_more_bitstream(bitstream); |
| 134 |
bits=(end_of_stream())?NULL:bitstream; |
| 135 |
sts=MFXVideoDECODE_DecodeFrameAsync(session,bits,NULL,&disp,&syncp); |
| 136 |
if (end_of_stream() && sts==MFX_ERR_MORE_DATA) break; |
| 137 |
// skipped other error handling |
| 138 |
if (sts==MFX_ERR_NONE) { |
| 139 |
disp->FrameInterface->Synchronize(disp, INFINITE); // or MFXVideoCORE_SyncOperation(session,syncp,INFINITE) |
| 140 |
do_something_with_decoded_frame(disp); |
| 141 |
disp->FrameInterface->Release(disp); |
| 142 |
} |
| 143 |
} |
| 144 |
MFXVideoDECODE_Close(session); |
| 145 |
/*end1*/ |
| 146 |
} |
| 147 |
|
| 148 |
static void prg_decoding2 () { |
| 149 |
/*beg2*/ |
| 150 |
MFXVideoDECODE_QueryIOSurf(session, &init_param, &request); |
| 151 |
MFXVideoDECODE_Init(session, &init_param); |
| 152 |
for (int i = 0; i < request.NumFrameSuggested; i++) { |
| 153 |
MFXMemory_GetSurfaceForDecode(session, &work); |
| 154 |
add_surface_to_pool(work); |
| 155 |
} |
| 156 |
sts=MFX_ERR_MORE_DATA; |
| 157 |
for (;;) { |
| 158 |
if (sts==MFX_ERR_MORE_DATA && !end_of_stream()) |
| 159 |
append_more_bitstream(bitstream); |
| 160 |
bits=(end_of_stream())?NULL:bitstream; |
| 161 |
// application logic to distinguish free and busy surfaces |
| 162 |
find_free_surface_from_the_pool(&work); |
| 163 |
sts=MFXVideoDECODE_DecodeFrameAsync(session,bits,work,&disp,&syncp); |
| 164 |
if (end_of_stream() && sts==MFX_ERR_MORE_DATA) break; |
| 165 |
// skipped other error handling |
| 166 |
if (sts==MFX_ERR_NONE) { |
| 167 |
disp->FrameInterface->Synchronize(disp, INFINITE); // or MFXVideoCORE_SyncOperation(session,syncp,INFINITE) |
| 168 |
do_something_with_decoded_frame(disp); |
| 169 |
disp->FrameInterface->Release(disp); |
| 170 |
} |
| 171 |
} |
| 172 |
for (int i = 0; i < request.NumFrameSuggested; i++) { |
| 173 |
get_next_surface_from_pool(&work); |
| 174 |
work->FrameInterface->Release(work); |
| 175 |
} |
| 176 |
MFXVideoDECODE_Close(session); |
| 177 |
/*end2*/ |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
static void prg_decoding3 () { |
| 182 |
/*beg3*/ |
| 183 |
MFXVideoDECODE_DecodeHeader(session, bitstream, &init_param); |
| 184 |
MFXVideoDECODE_QueryIOSurf(session, &init_param, &request); |
| 185 |
allocate_pool_of_frame_surfaces(request.NumFrameSuggested); |
| 186 |
MFXVideoDECODE_Init(session, &init_param); |
| 187 |
sts=MFX_ERR_MORE_DATA; |
| 188 |
for (;;) { |
| 189 |
if (sts==MFX_ERR_MORE_DATA && !end_of_stream()) |
| 190 |
append_more_bitstream(bitstream); |
| 191 |
find_free_surface_from_the_pool(&work); |
| 192 |
bits=(end_of_stream())?NULL:bitstream; |
| 193 |
sts=MFXVideoDECODE_DecodeFrameAsync(session,bits,work,&disp,&syncp); |
| 194 |
if (sts==MFX_ERR_MORE_SURFACE) continue; |
| 195 |
if (end_of_stream() && sts==MFX_ERR_MORE_DATA) break; |
| 196 |
if (sts==MFX_ERR_REALLOC_SURFACE) { |
| 197 |
MFXVideoDECODE_GetVideoParam(session, ¶m); |
| 198 |
realloc_surface(work, param.mfx.FrameInfo); |
| 199 |
continue; |
| 200 |
} |
| 201 |
// skipped other error handling |
| 202 |
if (sts==MFX_ERR_NONE) { |
| 203 |
disp->FrameInterface->Synchronize(disp, INFINITE); // or MFXVideoCORE_SyncOperation(session,syncp,INFINITE) |
| 204 |
do_something_with_decoded_frame(disp); |
| 205 |
} |
| 206 |
} |
| 207 |
MFXVideoDECODE_Close(session); |
| 208 |
free_pool_of_frame_surfaces(); |
| 209 |
/*end3*/ |
| 210 |
} |
| 211 |
|
| 212 |
static void prg_decoding4 () { |
| 213 |
/*beg4*/ |
| 214 |
sts=MFX_ERR_MORE_DATA; |
| 215 |
for (;;) { |
| 216 |
if (sts==MFX_ERR_MORE_DATA && !end_of_stream()) |
| 217 |
append_more_bitstream(bitstream); |
| 218 |
bits=(end_of_stream())?NULL:bitstream; |
| 219 |
sts=MFXVideoDECODE_DecodeFrameAsync(session,bits,NULL,&disp,&syncp); |
| 220 |
if (sts==MFX_ERR_MORE_SURFACE) continue; |
| 221 |
if (end_of_stream() && sts==MFX_ERR_MORE_DATA) break; |
| 222 |
// skipped other error handling |
| 223 |
if (sts==MFX_ERR_NONE) { |
| 224 |
disp->FrameInterface->Synchronize(disp, INFINITE); // or MFXVideoCORE_SyncOperation(session,syncp,INFINITE) |
| 225 |
do_something_with_decoded_frame(disp); |
| 226 |
disp->FrameInterface->Release(disp); |
| 227 |
} |
| 228 |
} |
| 229 |
/*end4*/ |
| 230 |
} |
| 231 |
|
| 232 |
static void prg_decoding5 () { |
| 233 |
/*beg5*/ |
| 234 |
mfxExtBuffer *eb[2]; |
| 235 |
mfxExtMVCSeqDesc seq_desc; |
| 236 |
mfxVideoParam init_param; |
| 237 |
|
| 238 |
init_param.ExtParam=(mfxExtBuffer **)&eb; |
| 239 |
init_param.NumExtParam=1; |
| 240 |
eb[0]=(mfxExtBuffer *)&seq_desc; |
| 241 |
MFXVideoDECODE_DecodeHeader(session, bitstream, &init_param); |
| 242 |
|
| 243 |
/* select views to decode */ |
| 244 |
mfxExtMVCTargetViews tv; |
| 245 |
init_param.NumExtParam=2; |
| 246 |
eb[1]=(mfxExtBuffer *)&tv; |
| 247 |
|
| 248 |
/* initialize decoder */ |
| 249 |
MFXVideoDECODE_Init(session, &init_param); |
| 250 |
|
| 251 |
/* perform decoding */ |
| 252 |
for (;;) { |
| 253 |
MFXVideoDECODE_DecodeFrameAsync(session, bits, work, &disp, &syncp); |
| 254 |
disp->FrameInterface->Synchronize(disp, INFINITE); // or MFXVideoCORE_SyncOperation(session,syncp,INFINITE) |
| 255 |
} |
| 256 |
|
| 257 |
/* close decoder */ |
| 258 |
MFXVideoDECODE_Close(session); |
| 259 |
/*end5*/ |
| 260 |
} |
| 261 |
|
| 262 |
static void prg_decoding6 () { |
| 263 |
/*beg6*/ |
| 264 |
MFXVideoDECODE_Init(session, &init_param); |
| 265 |
sts=MFX_ERR_MORE_DATA; |
| 266 |
for (;;) { |
| 267 |
if (sts==MFX_ERR_MORE_DATA && !end_of_stream()) |
| 268 |
append_more_bitstream(bitstream); |
| 269 |
bits=(end_of_stream())?NULL:bitstream; |
| 270 |
MFXMemory_GetSurfaceForDecode(session, &work); |
| 271 |
sts=MFXVideoDECODE_DecodeFrameAsync(session,bits,work,&disp,&syncp); |
| 272 |
work->FrameInterface->Release(work); |
| 273 |
if (end_of_stream() && sts==MFX_ERR_MORE_DATA) break; |
| 274 |
// skipped other error handling |
| 275 |
if (sts==MFX_ERR_NONE) { |
| 276 |
disp->FrameInterface->Synchronize(disp, INFINITE); // or MFXVideoCORE_SyncOperation(session,syncp,INFINITE) |
| 277 |
do_something_with_decoded_frame(disp); |
| 278 |
disp->FrameInterface->Release(disp); |
| 279 |
} |
| 280 |
} |
| 281 |
MFXVideoDECODE_Close(session); |
| 282 |
/*end6*/ |
| 283 |
} |
| 284 |
|
| 285 |
static void prg_decoding7 () { |
| 286 |
/*beg7*/ |
| 287 |
SyncPointList syncPointsList; |
| 288 |
init_sync_point_list(&syncPointsList); // Initialize the list |
| 289 |
/* initialize decoder */ |
| 290 |
MFXVideoDECODE_Init(session, &init_param); |
| 291 |
/* perform decoding */ |
| 292 |
for (;;) { |
| 293 |
// Asynchronously decode a frame |
| 294 |
sts = MFXVideoDECODE_DecodeFrameAsync(session, bits, work, &disp, &syncp); |
| 295 |
if (sts == MFX_ERR_NONE) { |
| 296 |
|
| 297 |
// Add the sync point to the linked list |
| 298 |
if(disp != NULL) |
| 299 |
push_sync_point(&syncPointsList, syncp, disp); |
| 300 |
|
| 301 |
// If the number of elements in the list exceeds AsyncDepth or there is no more data to decode |
| 302 |
if (syncPointsList.size >= init_param.AsyncDepth || !bits) { |
| 303 |
while (syncPointsList.next != NULL) { |
| 304 |
// Wait for the sync point to complete |
| 305 |
sts = MFXVideoCORE_SyncOperation(session, syncPointsList.next->syncPoint, MFX_INFINITE); |
| 306 |
if (sts == MFX_ERR_NONE) { |
| 307 |
pop_sync_point(&syncPointsList); // Remove the processed item |
| 308 |
} else { |
| 309 |
// If synchronization fails, exit the loop |
| 310 |
break; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
/* close decoder */ |
| 317 |
MFXVideoDECODE_Close(session); |
| 318 |
/*end7*/ |
| 319 |
} |