Skip to content

Commit

Permalink
feat(avafpacket): support AVAFPacket to PBAFFrame
Browse files Browse the repository at this point in the history
Signed-off-by: pingkai <pingkai010@gmail.com>
  • Loading branch information
pingkai committed Jun 9, 2020
1 parent 526f199 commit 7686348
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 41 deletions.
15 changes: 14 additions & 1 deletion framework/base/media/AVAFPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "base/media/IAFPacket.h"
#include "AVAFPacket.h"
#include "utils/ffmpeg_utils.h"
#ifdef __APPLE__
#include "PBAFFrame.h"
#include "avFrame2pixelBuffer.h"
#endif

using namespace std;

Expand Down Expand Up @@ -197,4 +201,13 @@ void AVAFFrame::updateInfo()
{
copyInfo();
}

#ifdef __APPLE__
AVAFFrame::operator PBAFFrame *()
{
CVPixelBufferRef pixelBuffer = avFrame2pixelBuffer(mAvFrame);
if (pixelBuffer) {
return new PBAFFrame(pixelBuffer, mInfo.pts, mInfo.duration);
}
return nullptr;
}
#endif
7 changes: 7 additions & 0 deletions framework/base/media/AVAFPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ extern "C" {
#include <libavcodec/avcodec.h>
};

#ifdef __APPLE__
class PBAFFrame;
#endif

class AVAFPacket : public IAFPacket {
public:
Expand Down Expand Up @@ -67,6 +70,10 @@ class AVAFFrame : public IAFFrame {

explicit operator AVFrame *() const;

#ifdef __APPLE__
explicit operator PBAFFrame *();
#endif

void updateInfo();


Expand Down
37 changes: 36 additions & 1 deletion framework/base/media/PBAFFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@
// Created by moqi on 2019-08-28.
//
#include "PBAFFrame.h"
#include "AVAFPacket.h"

extern "C" {
#include <libavutil/frame.h>
#include <libavutil/imgutils.h>
}

PBAFFrame::PBAFFrame(CVPixelBufferRef pixelBuffer, int64_t pts, int64_t duration) : mPBuffer(CVPixelBufferRetain(pixelBuffer))
{

mInfo.pts = pts;
mInfo.duration = duration;
mInfo.video.format = AF_PIX_FMT_APPLE_PIXEL_BUFFER;
mInfo.video.width = (int) CVPixelBufferGetWidth(mPBuffer);
mInfo.video.height = (int) CVPixelBufferGetHeight(mPBuffer);

OSType pixel_format = CVPixelBufferGetPixelFormatType(pixelBuffer);
if (pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
mInfo.video.colorRange = COLOR_RANGE_FULL;
} else if (pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) {
mInfo.video.colorRange = COLOR_RANGE_LIMITIED;
} else {
mInfo.video.colorRange = COLOR_RANGE_UNSPECIFIED;
}

CFTypeRef colorAttachments = CVBufferGetAttachment((CVPixelBufferRef) pixelBuffer, kCVImageBufferYCbCrMatrixKey, NULL);
if (colorAttachments != nullptr) {
if (CFStringCompare((CFStringRef) colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_601_4, 0) == kCFCompareEqualTo) {
mInfo.video.colorSpace = COLOR_SPACE_BT601;
} else if (CFStringCompare((CFStringRef) colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_709_2, 0) == kCFCompareEqualTo) {
mInfo.video.colorSpace = COLOR_SPACE_BT709;
} else if (CFStringCompare((CFStringRef) colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_2020, 0) == kCFCompareEqualTo) {
mInfo.video.colorSpace = COLOR_SPACE_BT2020;
} else {
mInfo.video.colorSpace = COLOR_SPACE_UNSPECIFIED;
}
} else {
mInfo.video.colorSpace = COLOR_SPACE_UNSPECIFIED;
}
}

PBAFFrame::operator AVAFFrame *()
{
CVReturn err;
Expand Down Expand Up @@ -57,4 +92,4 @@ PBAFFrame::operator AVAFFrame *()
AVAFFrame *pAvFrame = new AVAFFrame(pFrame, FrameTypeVideo);
av_frame_free(&pFrame);
return pAvFrame;
}
}
43 changes: 4 additions & 39 deletions framework/base/media/PBAFFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,12 @@
#include "base/media/IAFPacket.h"
#include <CoreMedia/CoreMedia.h>
#include <utils/AFMediaType.h>
#include <base/media/AVAFPacket.h>

class AVAFFrame;

class PBAFFrame : public IAFFrame {
public:
PBAFFrame(CVPixelBufferRef pixelBuffer, int64_t pts, int64_t duration) :
mPBuffer(CVPixelBufferRetain(pixelBuffer))
{

mInfo.pts = pts;
mInfo.duration = duration;
mInfo.video.format = AF_PIX_FMT_APPLE_PIXEL_BUFFER;
mInfo.video.width = (int) CVPixelBufferGetWidth(mPBuffer);
mInfo.video.height = (int) CVPixelBufferGetHeight(mPBuffer);

OSType pixel_format = CVPixelBufferGetPixelFormatType(pixelBuffer);
if(pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange){
mInfo.video.colorRange = COLOR_RANGE_FULL;
} else if(pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) {
mInfo.video.colorRange = COLOR_RANGE_LIMITIED;
} else {
mInfo.video.colorRange = COLOR_RANGE_UNSPECIFIED;
}

CFTypeRef colorAttachments = CVBufferGetAttachment((CVPixelBufferRef)pixelBuffer, kCVImageBufferYCbCrMatrixKey, NULL);
if (colorAttachments != NULL) {
if(CFStringCompare((CFStringRef)colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_601_4, 0) == kCFCompareEqualTo) {
mInfo.video.colorSpace = COLOR_SPACE_BT601;
}
else if(CFStringCompare((CFStringRef)colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_709_2, 0) == kCFCompareEqualTo) {
mInfo.video.colorSpace = COLOR_SPACE_BT709;
}
else if(CFStringCompare((CFStringRef)colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_2020, 0) == kCFCompareEqualTo) {
mInfo.video.colorSpace = COLOR_SPACE_BT2020;
} else {
mInfo.video.colorSpace = COLOR_SPACE_UNSPECIFIED;
}
} else {
mInfo.video.colorSpace = COLOR_SPACE_UNSPECIFIED;
}

}
PBAFFrame(CVPixelBufferRef pixelBuffer, int64_t pts, int64_t duration);

~PBAFFrame() override
{
Expand Down Expand Up @@ -80,7 +45,7 @@ class PBAFFrame : public IAFFrame {
return nullptr;
}

operator AVAFFrame *();
explicit operator AVAFFrame *();

private:
CVPixelBufferRef mPBuffer;
Expand Down
Loading

0 comments on commit 7686348

Please sign in to comment.