Can be tested via the following command: ./ffmpeg -i foo.ts -f decklink -vcodec v210 'DeckLink Duo (1)' Note that the 8-bit support works as it did before, and setting the pix_fmt isn't required for 10-bit mode. The code defaults to operating in 8-bit mode when no vcodec is specified, for backward compatibility. Updated to reflect feedback from Marton Balint <cus@passwd.hu>
Unified · Split
| @@ -44,20 +44,45 @@ extern "C" { · 43 unmodified lines | ||
| 44 | 44 | class decklink_frame : public IDeckLinkVideoFrame |
| 45 | 45 | { |
| 46 | 46 | public: |
| 47 | − decklink_frame(struct decklink_ctx *ctx, AVFrame *avframe) : | |
| 48 | − _ctx(ctx), _avframe(avframe), _refs(1) { } | |
| 49 | − | |
| 50 | − virtual long STDMETHODCALLTYPE GetWidth (void) { return _avframe->width; } | |
| 51 | − virtual long STDMETHODCALLTYPE GetHeight (void) { return _avframe->height; } | |
| 52 | − virtual long STDMETHODCALLTYPE GetRowBytes (void) { return _avframe->linesize[0] < 0 ? -_avframe->linesize[0] : _avframe->linesize[0]; } | |
| 53 | − virtual BMDPixelFormat STDMETHODCALLTYPE GetPixelFormat(void) { return bmdFormat8BitYUV; } | |
| 54 | − virtual BMDFrameFlags STDMETHODCALLTYPE GetFlags (void) { return _avframe->linesize[0] < 0 ? bmdFrameFlagFlipVertical : bmdFrameFlagDefault; } | |
| 55 | − virtual HRESULT STDMETHODCALLTYPE GetBytes (void **buffer) | |
| 47 | + decklink_frame(struct decklink_ctx *ctx, AVFrame *avframe, AVCodecID codec_id, int height, int width) : | |
| 48 | + _ctx(ctx), _avframe(avframe), _avpacket(NULL), _codec_id(codec_id), _height(height), _width(width), _refs(1) { } | |
| 49 | + decklink_frame(struct decklink_ctx *ctx, AVPacket *avpacket, AVCodecID codec_id, int height, int width) : | |
| 50 | + _ctx(ctx), _avframe(NULL), _avpacket(avpacket), _codec_id(codec_id), _height(height), _width(width), _refs(1) { } | |
| 51 | + | |
| 52 | + virtual long STDMETHODCALLTYPE GetWidth (void) { return _width; } | |
| 53 | + virtual long STDMETHODCALLTYPE GetHeight (void) { return _height; } | |
| 54 | + virtual long STDMETHODCALLTYPE GetRowBytes (void) | |
| 55 | + { | |
| 56 | + if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) | |
| 57 | + return _avframe->linesize[0] < 0 ? -_avframe->linesize[0] : _avframe->linesize[0]; | |
| 58 | + else | |
| 59 | + return ((GetWidth() + 47) / 48) * 128; | |
| 60 | + } | |
| 61 | + virtual BMDPixelFormat STDMETHODCALLTYPE GetPixelFormat(void) | |
| 56 | 62 | { |
| 57 | − if (_avframe->linesize[0] < 0) | |
| 58 | − *buffer = (void *)(_avframe->data[0] + _avframe->linesize[0] * (_avframe->height - 1)); | |
| 63 | + if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) | |
| 64 | + return bmdFormat8BitYUV; | |
| 59 | 65 | else |
| 60 | − *buffer = (void *)(_avframe->data[0]); | |
| 66 | + return bmdFormat10BitYUV; | |
| 67 | + } | |
| 68 | + virtual BMDFrameFlags STDMETHODCALLTYPE GetFlags (void) | |
| 69 | + { | |
| 70 | + if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) | |
| 71 | + return _avframe->linesize[0] < 0 ? bmdFrameFlagFlipVertical : bmdFrameFlagDefault; | |
| 72 | + else | |
| 73 | + return bmdFrameFlagDefault; | |
| 74 | + } | |
| 75 | + | |
| 76 | + virtual HRESULT STDMETHODCALLTYPE GetBytes (void **buffer) | |
| 77 | + { | |
| 78 | + if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) { | |
| 79 | + if (_avframe->linesize[0] < 0) | |
| 80 | + *buffer = (void *)(_avframe->data[0] + _avframe->linesize[0] * (_avframe->height - 1)); | |
| 81 | + else | |
| 82 | + *buffer = (void *)(_avframe->data[0]); | |
| 83 | + } else { | |
| 84 | + *buffer = (void *)(_avpacket->data); | |
| 85 | + } | |
| 61 | 86 | return S_OK; |
| 62 | 87 | } |
| 63 | 88 | |
| @@ -71,6 +96,7 @@ public: · 7 unmodified lines | ||
| 71 | 96 | int ret = --_refs; |
| 72 | 97 | if (!ret) { |
| 73 | 98 | av_frame_free(&_avframe); |
| 99 | + av_packet_free(&_avpacket); | |
| 74 | 100 | delete this; |
| 75 | 101 | } |
| 76 | 102 | return ret; |
| @@ -78,6 +104,10 @@ public: · 1 unmodified line | ||
| 78 | 104 | |
| 79 | 105 | struct decklink_ctx *_ctx; |
| 80 | 106 | AVFrame *_avframe; |
| 107 | + AVPacket *_avpacket; | |
| 108 | + AVCodecID _codec_id; | |
| 109 | + int _height; | |
| 110 | + int _width; | |
| 81 | 111 | |
| 82 | 112 | private: |
| 83 | 113 | std::atomic<int> _refs; |
| @@ -90,9 +120,11 @@ public: · 6 unmodified lines | ||
| 90 | 120 | { |
| 91 | 121 | decklink_frame *frame = static_cast<decklink_frame *>(_frame); |
| 92 | 122 | struct decklink_ctx *ctx = frame->_ctx; |
| 93 | − AVFrame *avframe = frame->_avframe; | |
| 94 | 123 | |
| 95 | − av_frame_unref(avframe); | |
| 124 | + if (frame->_avframe) | |
| 125 | + av_frame_unref(frame->_avframe); | |
| 126 | + if (frame->_avpacket) | |
| 127 | + av_packet_unref(frame->_avpacket); | |
| 96 | 128 | |
| 97 | 129 | pthread_mutex_lock(&ctx->mutex); |
| 98 | 130 | ctx->frames_buffer_available_spots++; |
| @@ -118,11 +150,18 @@ static int decklink_setup_video(AVFormatContext *avctx, AVStream *st) · 19 unmodified lines | ||
| 118 | 150 | return -1; |
| 119 | 151 | } |
| 120 | 152 | |
| 121 | − if (c->format != AV_PIX_FMT_UYVY422) { | |
| 122 | − av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!" | |
| 123 | − " Only AV_PIX_FMT_UYVY422 is supported.\n"); | |
| 153 | + if (c->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) { | |
| 154 | + if (c->format != AV_PIX_FMT_UYVY422) { | |
| 155 | + av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!" | |
| 156 | + " Only AV_PIX_FMT_UYVY422 is supported.\n"); | |
| 157 | + return -1; | |
| 158 | + } | |
| 159 | + } else if (c->codec_id != AV_CODEC_ID_V210) { | |
| 160 | + av_log(avctx, AV_LOG_ERROR, "Unsupported codec type!" | |
| 161 | + " Only V210 and wrapped frame with AV_PIX_FMT_UYVY422 are supported.\n"); | |
| 124 | 162 | return -1; |
| 125 | 163 | } |
| 164 | + | |
| 126 | 165 | if (ff_decklink_set_format(avctx, c->width, c->height, |
| 127 | 166 | st->time_base.num, st->time_base.den, c->field_order)) { |
| 128 | 167 | av_log(avctx, AV_LOG_ERROR, "Unsupported video size, framerate or field order!" |
| @@ -230,27 +269,42 @@ static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt) · 101 unmodified lines | ||
| 230 | 269 | { |
| 231 | 270 | struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data; |
| 232 | 271 | struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx; |
| 233 | − AVFrame *avframe, *tmp = (AVFrame *)pkt->data; | |
| 272 | + AVStream *st = avctx->streams[pkt->stream_index]; | |
| 273 | + AVFrame *avframe = NULL, *tmp = (AVFrame *)pkt->data; | |
| 274 | + AVPacket *avpacket = NULL; | |
| 234 | 275 | decklink_frame *frame; |
| 235 | 276 | buffercount_type buffered; |
| 236 | 277 | HRESULT hr; |
| 237 | 278 | |
| 238 | − if (tmp->format != AV_PIX_FMT_UYVY422 || | |
| 239 | − tmp->width != ctx->bmd_width || | |
| 240 | − tmp->height != ctx->bmd_height) { | |
| 241 | − av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid pixel format or dimension.\n"); | |
| 242 | − return AVERROR(EINVAL); | |
| 243 | − } | |
| 244 | − avframe = av_frame_clone(tmp); | |
| 245 | − if (!avframe) { | |
| 246 | − av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n"); | |
| 247 | − return AVERROR(EIO); | |
| 279 | + if (st->codecpar->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) { | |
| 280 | + if (tmp->format != AV_PIX_FMT_UYVY422 || | |
| 281 | + tmp->width != ctx->bmd_width || | |
| 282 | + tmp->height != ctx->bmd_height) { | |
| 283 | + av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid pixel format or dimension.\n"); | |
| 284 | + return AVERROR(EINVAL); | |
| 285 | + } | |
| 286 | + | |
| 287 | + avframe = av_frame_clone(tmp); | |
| 288 | + if (!avframe) { | |
| 289 | + av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n"); | |
| 290 | + return AVERROR(EIO); | |
| 291 | + } | |
| 292 | + | |
| 293 | + frame = new decklink_frame(ctx, avframe, st->codecpar->codec_id, avframe->height, avframe->width); | |
| 294 | + } else { | |
| 295 | + avpacket = av_packet_clone(pkt); | |
| 296 | + if (!avpacket) { | |
| 297 | + av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n"); | |
| 298 | + return AVERROR(EIO); | |
| 299 | + } | |
| 300 | + | |
| 301 | + frame = new decklink_frame(ctx, avpacket, st->codecpar->codec_id, ctx->bmd_height, ctx->bmd_width); | |
| 248 | 302 | } |
| 249 | 303 | |
| 250 | − frame = new decklink_frame(ctx, avframe); | |
| 251 | 304 | if (!frame) { |
| 252 | 305 | av_log(avctx, AV_LOG_ERROR, "Could not create new frame.\n"); |
| 253 | 306 | av_frame_free(&avframe); |
| 307 | + av_packet_free(&avpacket); | |
| 254 | 308 | return AVERROR(EIO); |
| 255 | 309 | } |
| 256 | 310 | |