| @@ -26,7 +26,9 @@ · 25 unmodified lines |
| 26 | | 26 | |
| 27 | #include "libavutil/avutil.h" | 27 | #include "libavutil/avutil.h" |
| 28 | #include "h264.h" | 28 | #include "h264.h" |
| | 29 | #include "internal.h" |
| 29 | #include "vda.h" | 30 | #include "vda.h" |
| | 31 | #include "vda_internal.h" |
| 30 | | 32 | |
| 31 | typedef struct VDAContext { | 33 | typedef struct VDAContext { |
| 32 | // The current bitstream buffer. | 34 | // The current bitstream buffer. |
| @@ -37,6 +39,8 @@ typedef struct VDAContext { · 4 unmodified lines |
| 37 | | 39 | |
| 38 | // The reference size used for fast reallocation. | 40 | // The reference size used for fast reallocation. |
| 39 | int allocated_size; | 41 | int allocated_size; |
| | 42 | |
| | 43 | CVImageBufferRef frame; |
| 40 | } VDAContext; | 44 | } VDAContext; |
| 41 | | 45 | |
| 42 | /* Decoder callback that adds the VDA frame to the queue in display order. */ | 46 | /* Decoder callback that adds the VDA frame to the queue in display order. */ |
| @@ -78,7 +82,7 @@ static int vda_sync_decode(VDAContext *ctx, struct vda_context *vda_ctx) · 35 unmodified lines |
| 78 | } | 82 | } |
| 79 | | 83 | |
| 80 | | 84 | |
| 81 | static int vda_h264_start_frame(AVCodecContext *avctx, | 85 | static int vda_old_h264_start_frame(AVCodecContext *avctx, |
| 82 | av_unused const uint8_t *buffer, | 86 | av_unused const uint8_t *buffer, |
| 83 | av_unused uint32_t size) | 87 | av_unused uint32_t size) |
| 84 | { | 88 | { |
| @@ -93,7 +97,7 @@ static int vda_h264_start_frame(AVCodecContext *avctx, · 8 unmodified lines |
| 93 | return 0; | 97 | return 0; |
| 94 | } | 98 | } |
| 95 | | 99 | |
| 96 | static int vda_h264_decode_slice(AVCodecContext *avctx, | 100 | static int vda_old_h264_decode_slice(AVCodecContext *avctx, |
| 97 | const uint8_t *buffer, | 101 | const uint8_t *buffer, |
| 98 | uint32_t size) | 102 | uint32_t size) |
| 99 | { | 103 | { |
| @@ -120,7 +124,7 @@ static int vda_h264_decode_slice(AVCodecContext *avctx, · 20 unmodified lines |
| 120 | return 0; | 124 | return 0; |
| 121 | } | 125 | } |
| 122 | | 126 | |
| 123 | static int vda_h264_end_frame(AVCodecContext *avctx) | 127 | static int vda_old_h264_end_frame(AVCodecContext *avctx) |
| 124 | { | 128 | { |
| 125 | H264Context *h = avctx->priv_data; | 129 | H264Context *h = avctx->priv_data; |
| 126 | VDAContext *vda = avctx->internal->hwaccel_priv_data; | 130 | VDAContext *vda = avctx->internal->hwaccel_priv_data; |
| @@ -208,7 +212,7 @@ int ff_vda_create_decoder(struct vda_context *vda_ctx, · 81 unmodified lines |
| 208 | | 212 | |
| 209 | status = VDADecoderCreate(config_info, | 213 | status = VDADecoderCreate(config_info, |
| 210 | buffer_attributes, | 214 | buffer_attributes, |
| 211 | vda_decoder_callback, | 215 | (VDADecoderOutputCallback *)vda_decoder_callback, |
| 212 | vda_ctx, | 216 | vda_ctx, |
| 213 | &vda_ctx->decoder); | 217 | &vda_ctx->decoder); |
| 214 | | 218 | |
| @@ -234,17 +238,256 @@ int ff_vda_destroy_decoder(struct vda_context *vda_ctx) · 19 unmodified lines |
| 234 | return status; | 238 | return status; |
| 235 | } | 239 | } |
| 236 | | 240 | |
| 237 | static void vda_h264_uninit(AVCodecContext *avctx) | 241 | static int vda_h264_uninit(AVCodecContext *avctx) |
| 238 | { | 242 | { |
| 239 | VDAContext *vda = avctx->internal->priv_data; | 243 | VDAContext *vda = avctx->internal->hwaccel_priv_data; |
| 240 | av_freep(&vda->bitstream); | 244 | av_freep(&vda->bitstream); |
| | 245 | if (vda->frame) |
| | 246 | CVPixelBufferRelease(vda->frame); |
| | 247 | return 0; |
| 241 | } | 248 | } |
| 242 | | 249 | |
| 243 | AVHWAccel ff_h264_vda_hwaccel = { | 250 | AVHWAccel ff_h264_vda_old_hwaccel = { |
| 244 | .name = "h264_vda", | 251 | .name = "h264_vda", |
| 245 | .type = AVMEDIA_TYPE_VIDEO, | 252 | .type = AVMEDIA_TYPE_VIDEO, |
| 246 | .id = AV_CODEC_ID_H264, | 253 | .id = AV_CODEC_ID_H264, |
| 247 | .pix_fmt = AV_PIX_FMT_VDA_VLD, | 254 | .pix_fmt = AV_PIX_FMT_VDA_VLD, |
| | 255 | .start_frame = vda_old_h264_start_frame, |
| | 256 | .decode_slice = vda_old_h264_decode_slice, |
| | 257 | .end_frame = vda_old_h264_end_frame, |
| | 258 | .uninit = vda_h264_uninit, |
| | 259 | .priv_data_size = sizeof(VDAContext), |
| | 260 | }; |
| | 261 | |
| | 262 | void ff_vda_output_callback(void *opaque, |
| | 263 | CFDictionaryRef user_info, |
| | 264 | OSStatus status, |
| | 265 | uint32_t infoFlags, |
| | 266 | CVImageBufferRef image_buffer) |
| | 267 | { |
| | 268 | AVCodecContext *ctx = opaque; |
| | 269 | VDAContext *vda = ctx->internal->hwaccel_priv_data; |
| | 270 | |
| | 271 | |
| | 272 | if (vda->frame) { |
| | 273 | CVPixelBufferRelease(vda->frame); |
| | 274 | vda->frame = NULL; |
| | 275 | } |
| | 276 | |
| | 277 | if (!image_buffer) |
| | 278 | return; |
| | 279 | |
| | 280 | vda->frame = CVPixelBufferRetain(image_buffer); |
| | 281 | } |
| | 282 | |
| | 283 | static int vda_h264_start_frame(AVCodecContext *avctx, |
| | 284 | const uint8_t *buffer, |
| | 285 | uint32_t size) |
| | 286 | { |
| | 287 | VDAContext *vda = avctx->internal->hwaccel_priv_data; |
| | 288 | |
| | 289 | vda->bitstream_size = 0; |
| | 290 | |
| | 291 | return 0; |
| | 292 | } |
| | 293 | |
| | 294 | static int vda_h264_decode_slice(AVCodecContext *avctx, |
| | 295 | const uint8_t *buffer, |
| | 296 | uint32_t size) |
| | 297 | { |
| | 298 | VDAContext *vda = avctx->internal->hwaccel_priv_data; |
| | 299 | void *tmp; |
| | 300 | |
| | 301 | tmp = av_fast_realloc(vda->bitstream, |
| | 302 | &vda->allocated_size, |
| | 303 | vda->bitstream_size + size + 4); |
| | 304 | if (!tmp) |
| | 305 | return AVERROR(ENOMEM); |
| | 306 | |
| | 307 | vda->bitstream = tmp; |
| | 308 | |
| | 309 | AV_WB32(vda->bitstream + vda->bitstream_size, size); |
| | 310 | memcpy(vda->bitstream + vda->bitstream_size + 4, buffer, size); |
| | 311 | |
| | 312 | vda->bitstream_size += size + 4; |
| | 313 | |
| | 314 | return 0; |
| | 315 | } |
| | 316 | |
| | 317 | static void release_buffer(void *opaque, uint8_t *data) |
| | 318 | { |
| | 319 | CVImageBufferRef frame = (CVImageBufferRef)data; |
| | 320 | CVPixelBufferRelease(frame); |
| | 321 | } |
| | 322 | |
| | 323 | static int vda_h264_end_frame(AVCodecContext *avctx) |
| | 324 | { |
| | 325 | H264Context *h = avctx->priv_data; |
| | 326 | VDAContext *vda = avctx->internal->hwaccel_priv_data; |
| | 327 | AVVDAContext *vda_ctx = avctx->hwaccel_context; |
| | 328 | AVFrame *frame = &h->cur_pic_ptr->f; |
| | 329 | uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames |
| | 330 | CFDataRef coded_frame; |
| | 331 | OSStatus status; |
| | 332 | |
| | 333 | if (!vda->bitstream_size) |
| | 334 | return AVERROR_INVALIDDATA; |
| | 335 | |
| | 336 | |
| | 337 | coded_frame = CFDataCreate(kCFAllocatorDefault, |
| | 338 | vda->bitstream, |
| | 339 | vda->bitstream_size); |
| | 340 | |
| | 341 | status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL); |
| | 342 | |
| | 343 | if (status == kVDADecoderNoErr) |
| | 344 | status = VDADecoderFlush(vda_ctx->decoder, flush_flags); |
| | 345 | |
| | 346 | CFRelease(coded_frame); |
| | 347 | |
| | 348 | if (status != kVDADecoderNoErr) { |
| | 349 | av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status); |
| | 350 | return AVERROR_UNKNOWN; |
| | 351 | } |
| | 352 | |
| | 353 | if (vda->frame) { |
| | 354 | av_buffer_unref(&frame->buf[0]); |
| | 355 | |
| | 356 | frame->buf[0] = av_buffer_create((uint8_t*)vda->frame, |
| | 357 | sizeof(vda->frame), |
| | 358 | release_buffer, NULL, |
| | 359 | AV_BUFFER_FLAG_READONLY); |
| | 360 | if (!frame->buf) |
| | 361 | return AVERROR(ENOMEM); |
| | 362 | |
| | 363 | frame->data[3] = (uint8_t*)vda->frame; |
| | 364 | vda->frame = NULL; |
| | 365 | } |
| | 366 | |
| | 367 | return 0; |
| | 368 | } |
| | 369 | |
| | 370 | int ff_vda_default_init(AVCodecContext *avctx) |
| | 371 | { |
| | 372 | AVVDAContext *vda_ctx = avctx->hwaccel_context; |
| | 373 | OSStatus status = kVDADecoderNoErr; |
| | 374 | CFNumberRef height; |
| | 375 | CFNumberRef width; |
| | 376 | CFNumberRef format; |
| | 377 | CFDataRef avc_data; |
| | 378 | CFMutableDictionaryRef config_info; |
| | 379 | CFMutableDictionaryRef buffer_attributes; |
| | 380 | CFMutableDictionaryRef io_surface_properties; |
| | 381 | CFNumberRef cv_pix_fmt; |
| | 382 | int32_t fmt = 'avc1', pix_fmt = kCVPixelFormatType_422YpCbCr8; |
| | 383 | |
| | 384 | // kCVPixelFormatType_420YpCbCr8Planar; |
| | 385 | |
| | 386 | /* Each VCL NAL in the bistream sent to the decoder |
| | 387 | * is preceded by a 4 bytes length header. |
| | 388 | * Change the avcC atom header if needed, to signal headers of 4 bytes. */ |
| | 389 | if (avctx->extradata_size >= 4 && (avctx->extradata[4] & 0x03) != 0x03) { |
| | 390 | uint8_t *rw_extradata; |
| | 391 | |
| | 392 | if (!(rw_extradata = av_malloc(avctx->extradata_size))) |
| | 393 | return AVERROR(ENOMEM); |
| | 394 | |
| | 395 | memcpy(rw_extradata, avctx->extradata, avctx->extradata_size); |
| | 396 | |
| | 397 | rw_extradata[4] |= 0x03; |
| | 398 | |
| | 399 | avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, avctx->extradata_size); |
| | 400 | |
| | 401 | av_freep(&rw_extradata); |
| | 402 | } else { |
| | 403 | avc_data = CFDataCreate(kCFAllocatorDefault, |
| | 404 | avctx->extradata, avctx->extradata_size); |
| | 405 | } |
| | 406 | |
| | 407 | config_info = CFDictionaryCreateMutable(kCFAllocatorDefault, |
| | 408 | 4, |
| | 409 | &kCFTypeDictionaryKeyCallBacks, |
| | 410 | &kCFTypeDictionaryValueCallBacks); |
| | 411 | |
| | 412 | height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &avctx->height); |
| | 413 | width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &avctx->width); |
| | 414 | format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &fmt); |
| | 415 | CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height); |
| | 416 | CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width); |
| | 417 | CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data); |
| | 418 | CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format); |
| | 419 | |
| | 420 | buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, |
| | 421 | 2, |
| | 422 | &kCFTypeDictionaryKeyCallBacks, |
| | 423 | &kCFTypeDictionaryValueCallBacks); |
| | 424 | io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault, |
| | 425 | 0, |
| | 426 | &kCFTypeDictionaryKeyCallBacks, |
| | 427 | &kCFTypeDictionaryValueCallBacks); |
| | 428 | cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, |
| | 429 | kCFNumberSInt32Type, |
| | 430 | &pix_fmt); |
| | 431 | |
| | 432 | CFDictionarySetValue(buffer_attributes, |
| | 433 | kCVPixelBufferPixelFormatTypeKey, |
| | 434 | cv_pix_fmt); |
| | 435 | CFDictionarySetValue(buffer_attributes, |
| | 436 | kCVPixelBufferIOSurfacePropertiesKey, |
| | 437 | io_surface_properties); |
| | 438 | |
| | 439 | status = VDADecoderCreate(config_info, |
| | 440 | buffer_attributes, |
| | 441 | (VDADecoderOutputCallback *)ff_vda_output_callback, |
| | 442 | avctx, |
| | 443 | &vda_ctx->decoder); |
| | 444 | |
| | 445 | CFRelease(format); |
| | 446 | CFRelease(height); |
| | 447 | CFRelease(width); |
| | 448 | CFRelease(avc_data); |
| | 449 | CFRelease(config_info); |
| | 450 | CFRelease(cv_pix_fmt); |
| | 451 | CFRelease(io_surface_properties); |
| | 452 | CFRelease(buffer_attributes); |
| | 453 | |
| | 454 | if (status != kVDADecoderNoErr) { |
| | 455 | av_log(avctx, AV_LOG_ERROR, "Cannot initialize VDA %d\n", status); |
| | 456 | } |
| | 457 | |
| | 458 | switch (status) { |
| | 459 | case kVDADecoderHardwareNotSupportedErr: |
| | 460 | case kVDADecoderFormatNotSupportedErr: |
| | 461 | return AVERROR(ENOSYS); |
| | 462 | case kVDADecoderConfigurationError: |
| | 463 | return AVERROR(EINVAL); |
| | 464 | case kVDADecoderDecoderFailedErr: |
| | 465 | return AVERROR_INVALIDDATA; |
| | 466 | case kVDADecoderNoErr: |
| | 467 | return 0; |
| | 468 | default: |
| | 469 | return AVERROR_UNKNOWN; |
| | 470 | } |
| | 471 | } |
| | 472 | |
| | 473 | static int vda_h264_alloc_frame(AVCodecContext *avctx, AVFrame *frame) |
| | 474 | { |
| | 475 | frame->width = avctx->width; |
| | 476 | frame->height = avctx->height; |
| | 477 | frame->format = avctx->pix_fmt; |
| | 478 | frame->buf[0] = av_buffer_alloc(1); |
| | 479 | |
| | 480 | if (!frame->buf[0]) |
| | 481 | return AVERROR(ENOMEM); |
| | 482 | return 0; |
| | 483 | } |
| | 484 | |
| | 485 | AVHWAccel ff_h264_vda_hwaccel = { |
| | 486 | .name = "h264_vda", |
| | 487 | .type = AVMEDIA_TYPE_VIDEO, |
| | 488 | .id = AV_CODEC_ID_H264, |
| | 489 | .pix_fmt = AV_PIX_FMT_VDA, |
| | 490 | .alloc_frame = vda_h264_alloc_frame, |
| 248 | .start_frame = vda_h264_start_frame, | 491 | .start_frame = vda_h264_start_frame, |
| 249 | .decode_slice = vda_h264_decode_slice, | 492 | .decode_slice = vda_h264_decode_slice, |
| 250 | .end_frame = vda_h264_end_frame, | 493 | .end_frame = vda_h264_end_frame, |