History

lavc/videotoolboxenc: Add support for 10-bit HEVC encoding.

Hardware-accelerated encoding may not support 10-bit encoding. Use '-require_sw 1' in this case. Fixes: #7581

Rick Kern committed 7 years ago

Commit 98478c01 parent 714c8b01 file · +56 −46Browse files

Unified · Split

libavcodec/videotoolboxenc.c+56−46
@@ -762,6 +762,11 @@ static int get_cv_pixel_format(AVCodecContext* avctx, · 761 unmodified lines
762762 *av_pixel_format = range == AVCOL_RANGE_JPEG ?
763763 kCVPixelFormatType_420YpCbCr8PlanarFullRange :
764764 kCVPixelFormatType_420YpCbCr8Planar;
765+ } else if (fmt == AV_PIX_FMT_P010LE) {
766+ *av_pixel_format = range == AVCOL_RANGE_JPEG ?
767+ kCVPixelFormatType_420YpCbCr10BiPlanarFullRange :
768+ kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange;
769+ *av_pixel_format = kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange;
765770 } else {
766771 return AVERROR(EINVAL);
767772 }
@@ -1986,6 +1991,17 @@ static int get_cv_pixel_info( · 1218 unmodified lines
19861991 strides[2] = frame ? frame->linesize[2] : (avctx->width + 1) / 2;
19871992 break;
19881993
1994+ case AV_PIX_FMT_P010LE:
1995+ *plane_count = 2;
1996+ widths[0] = avctx->width;
1997+ heights[0] = avctx->height;
1998+ strides[0] = frame ? frame->linesize[0] : (avctx->width * 2 + 63) & -64;
1999+
2000+ widths[1] = (avctx->width + 1) / 2;
2001+ heights[1] = (avctx->height + 1) / 2;
2002+ strides[1] = frame ? frame->linesize[1] : ((avctx->width + 1) / 2 + 63) & -64;
2003+ break;
2004+
19892005 default:
19902006 av_log(
19912007 avctx,
@@ -2348,21 +2364,11 @@ static int vtenc_populate_extradata(AVCodecContext *avctx, · 356 unmodified lines
23482364 CFDictionaryRef pixel_buffer_info)
23492365 {
23502366 VTEncContext *vtctx = avctx->priv_data;
2351− AVFrame *frame = av_frame_alloc();
2352− int y_size = avctx->width * avctx->height;
2353− int chroma_size = (avctx->width / 2) * (avctx->height / 2);
2354− CMSampleBufferRef buf = NULL;
23552367 int status;
2356−
2357− if (!frame)
2358− return AVERROR(ENOMEM);
2359−
2360− frame->buf[0] = av_buffer_alloc(y_size + 2 * chroma_size);
2361−
2362− if(!frame->buf[0]){
2363− status = AVERROR(ENOMEM);
2364− goto pe_cleanup;
2365− }
2368+ CVPixelBufferPoolRef pool = NULL;
2369+ CVPixelBufferRef pix_buf = NULL;
2370+ CMTime time;
2371+ CMSampleBufferRef buf = NULL;
23662372
23672373 status = vtenc_create_encoder(avctx,
23682374 codec_type,
@@ -2374,39 +2380,38 @@ static int vtenc_populate_extradata(AVCodecContext *avctx, · 5 unmodified lines
23742380 if (status)
23752381 goto pe_cleanup;
23762382
2377− frame->data[0] = frame->buf[0]->data;
2378− memset(frame->data[0], 0, y_size);
2379−
2380− frame->data[1] = frame->buf[0]->data + y_size;
2381− memset(frame->data[1], 128, chroma_size);
2382−
2383−
2384− if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
2385− frame->data[2] = frame->buf[0]->data + y_size + chroma_size;
2386− memset(frame->data[2], 128, chroma_size);
2383+ pool = VTCompressionSessionGetPixelBufferPool(vtctx->session);
2384+ if(!pool){
2385+ av_log(avctx, AV_LOG_ERROR, "Error getting pixel buffer pool.\n");
2386+ goto pe_cleanup;
23872387 }
23882388
2389− frame->linesize[0] = avctx->width;
2389+ status = CVPixelBufferPoolCreatePixelBuffer(NULL,
2390+ pool,
2391+ &pix_buf);
23902392
2391− if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
2392− frame->linesize[1] =
2393− frame->linesize[2] = (avctx->width + 1) / 2;
2394− } else {
2395− frame->linesize[1] = (avctx->width + 1) / 2;
2393+ if(status != kCVReturnSuccess){
2394+ av_log(avctx, AV_LOG_ERROR, "Error creating frame from pool: %d\n", status);
2395+ goto pe_cleanup;
23962396 }
23972397
2398− frame->format = avctx->pix_fmt;
2399− frame->width = avctx->width;
2400− frame->height = avctx->height;
2401− frame->colorspace = avctx->colorspace;
2402− frame->color_range = avctx->color_range;
2403− frame->color_trc = avctx->color_trc;
2404− frame->color_primaries = avctx->color_primaries;
2398+ unsigned pbuftype = CVPixelBufferGetPixelFormatType(pix_buf);
2399+
2400+ time = CMTimeMake(0, avctx->time_base.den);
2401+ status = VTCompressionSessionEncodeFrame(vtctx->session,
2402+ pix_buf,
2403+ time,
2404+ kCMTimeInvalid,
2405+ NULL,
2406+ NULL,
2407+ NULL);
24052408
2406− frame->pts = 0;
2407− status = vtenc_send_frame(avctx, vtctx, frame);
24082409 if (status) {
2409− av_log(avctx, AV_LOG_ERROR, "Error sending frame: %d\n", status);
2410+ av_log(avctx,
2411+ AV_LOG_ERROR,
2412+ "Error sending frame for extradata: %d\n",
2413+ status);
2414+
24102415 goto pe_cleanup;
24112416 }
24122417
@@ -2434,9 +2439,6 @@ pe_cleanup: · 21 unmodified lines
24342439 vtctx->session = NULL;
24352440 vtctx->frame_ct_out = 0;
24362441
2437− av_frame_unref(frame);
2438− av_frame_free(&frame);
2439−
24402442 av_assert0(status != 0 || (avctx->extradata && avctx->extradata_size > 0));
24412443
24422444 return status;
@@ -2475,10 +2477,18 @@ static av_cold int vtenc_close(AVCodecContext *avctx) · 32 unmodified lines
24752477 return 0;
24762478 }
24772479
2478−static const enum AVPixelFormat pix_fmts[] = {
2480+static const enum AVPixelFormat avc_pix_fmts[] = {
2481+ AV_PIX_FMT_VIDEOTOOLBOX,
2482+ AV_PIX_FMT_NV12,
2483+ AV_PIX_FMT_YUV420P,
2484+ AV_PIX_FMT_NONE
2485+};
2486+
2487+static const enum AVPixelFormat hevc_pix_fmts[] = {
24792488 AV_PIX_FMT_VIDEOTOOLBOX,
24802489 AV_PIX_FMT_NV12,
24812490 AV_PIX_FMT_YUV420P,
2491+ AV_PIX_FMT_P010LE,
24822492 AV_PIX_FMT_NONE
24832493 };
24842494
@@ -2539,7 +2549,7 @@ AVCodec ff_h264_videotoolbox_encoder = { · 54 unmodified lines
25392549 .type = AVMEDIA_TYPE_VIDEO,
25402550 .id = AV_CODEC_ID_H264,
25412551 .priv_data_size = sizeof(VTEncContext),
2542− .pix_fmts = pix_fmts,
2552+ .pix_fmts = avc_pix_fmts,
25432553 .init = vtenc_init,
25442554 .encode2 = vtenc_frame,
25452555 .close = vtenc_close,
@@ -2571,7 +2581,7 @@ AVCodec ff_hevc_videotoolbox_encoder = { · 25 unmodified lines
25712581 .type = AVMEDIA_TYPE_VIDEO,
25722582 .id = AV_CODEC_ID_HEVC,
25732583 .priv_data_size = sizeof(VTEncContext),
2574− .pix_fmts = pix_fmts,
2584+ .pix_fmts = hevc_pix_fmts,
25752585 .init = vtenc_init,
25762586 .encode2 = vtenc_frame,
25772587 .close = vtenc_close,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t