[haiku-commits] haiku: hrev52238 - src/add-ons/media/plugins/ffmpeg

  • From: Stefano Ceccherini <stefano.ceccherini@xxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 16 Aug 2018 02:08:46 -0400 (EDT)

hrev52238 adds 1 changeset to branch 'master'
old head: 6bbfad51b7a4d399605dcefd1a0389ce0ac725cd
new head: 53086e436e0213b1f66e8081d2fd9b4bd3de4cc5
overview: 
https://git.haiku-os.org/haiku/log/?qt=range&q=53086e436e02+%5E6bbfad51b7a4

----------------------------------------------------------------------------

53086e436e02: AVCodecEncoder: avcodec_close() is deprecated.
  A codec context cannot be reopened, anymore.
  Reorganize code accordingly.
  Remove usage of the AVFormat context (which was not enabled, anyway) since 
it's no longer possible to do that.
  Use av_frame_free() instead of av_free()

                             [ JackBurton79 <stefano.ceccherini@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev52238
Commit:      53086e436e0213b1f66e8081d2fd9b4bd3de4cc5
URL:         https://git.haiku-os.org/haiku/commit/?id=53086e436e02
Author:      JackBurton79 <stefano.ceccherini@xxxxxxxxx>
Date:        Tue Aug 14 14:00:37 2018 UTC

----------------------------------------------------------------------------

2 files changed, 12 insertions(+), 64 deletions(-)
.../media/plugins/ffmpeg/AVCodecEncoder.cpp      | 67 ++++----------------
.../media/plugins/ffmpeg/AVCodecEncoder.h        |  9 +--

----------------------------------------------------------------------------

diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp 
b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp
index 9830887e79..ae533041dd 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp
@@ -43,9 +43,8 @@ AVCodecEncoder::AVCodecEncoder(uint32 codecID, int 
bitRateScale)
        fBitRateScale(bitRateScale),
        fCodecID((CodecID)codecID),
        fCodec(NULL),
-       fOwnContext(avcodec_alloc_context3(NULL)),
-       fCodecContext(fOwnContext),
-       fCodecInitStatus(CODEC_INIT_NEEDED),
+       fCodecContext(avcodec_alloc_context3(NULL)),
+       fCodecInitialized(false),
        fFrame(av_frame_alloc()),
        fSwsContext(NULL),
        fFramesWritten(0)
@@ -89,8 +88,6 @@ AVCodecEncoder::~AVCodecEncoder()
 {
        TRACE("AVCodecEncoder::~AVCodecEncoder()\n");
 
-       _CloseCodecIfNeeded();
-
        if (fSwsContext != NULL)
                sws_freeContext(fSwsContext);
 
@@ -110,10 +107,10 @@ AVCodecEncoder::~AVCodecEncoder()
                fFrame->linesize[1] = 0;
                fFrame->linesize[2] = 0;
                fFrame->linesize[3] = 0;
-               av_free(fFrame);
+               av_frame_free(&fFrame);
        }
 
-       avcodec_free_context(&fOwnContext);
+       avcodec_free_context(&fCodecContext);
 
        delete[] fChunkBuffer;
 }
@@ -160,28 +157,9 @@ AVCodecEncoder::SetUp(const media_format* inputFormat)
                return B_NO_INIT;
        }
 
-       _CloseCodecIfNeeded();
-
        fInputFormat = *inputFormat;
        fFramesWritten = 0;
 
-       const uchar* userData = inputFormat->user_data;
-       if (*(uint32*)userData == 'ffmp') {
-               userData += sizeof(uint32);
-               // The Writer plugin used is the FFmpeg plugin. It stores the
-               // AVCodecContext pointer in the user data section. Use this
-               // context instead of our own. It requires the Writer living in
-               // the same team, of course.
-               app_info appInfo;
-               if (be_app->GetAppInfo(&appInfo) == B_OK
-                       && *(team_id*)userData == appInfo.team) {
-                       userData += sizeof(team_id);
-                       // Use the AVCodecContext from the Writer. This works 
better
-                       // than using our own context with some encoders.
-                       fCodecContext = *(AVCodecContext**)userData;
-               }
-       }
-
        return _Setup();
 }
 
@@ -296,8 +274,9 @@ AVCodecEncoder::_Setup()
        if (fInputFormat.type == B_MEDIA_RAW_VIDEO) {
                TRACE("  B_MEDIA_RAW_VIDEO\n");
                // frame rate
-               fCodecContext->time_base.den = 
(int)fInputFormat.u.raw_video.field_rate;
-               fCodecContext->time_base.num = 1;
+               fCodecContext->time_base = (AVRational){1, 
(int)fInputFormat.u.raw_video.field_rate};
+               fCodecContext->framerate = 
(AVRational){(int)fInputFormat.u.raw_video.field_rate, 1};
+               
                // video size
                fCodecContext->width = 
fInputFormat.u.raw_video.display.line_width;
                fCodecContext->height = 
fInputFormat.u.raw_video.display.line_count;
@@ -486,18 +465,9 @@ AVCodecEncoder::_Setup()
 bool
 AVCodecEncoder::_OpenCodecIfNeeded()
 {
-       if (fCodecContext != fOwnContext) {
-               // We are using the AVCodecContext of the AVFormatWriter plugin,
-               // and don't maintain it's open/close state.
-               return true;
-       }
-
-       if (fCodecInitStatus == CODEC_INIT_DONE)
+       if (fCodecInitialized)
                return true;
 
-       if (fCodecInitStatus == CODEC_INIT_FAILED)
-               return false;
-
        fCodecContext->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
 
        // Some codecs need this to be set before open
@@ -508,29 +478,14 @@ AVCodecEncoder::_OpenCodecIfNeeded()
        // Open the codec
        int result = avcodec_open2(fCodecContext, fCodec, NULL);
        if (result >= 0)
-               fCodecInitStatus = CODEC_INIT_DONE;
+               fCodecInitialized = true;
        else
-               fCodecInitStatus = CODEC_INIT_FAILED;
+               fCodecInitialized = false;
 
        TRACE("  avcodec_open(%p, %p): %d\n", fCodecContext, fCodec, result);
 
-       return fCodecInitStatus == CODEC_INIT_DONE;
-
-}
+       return fCodecInitialized;
 
-
-void
-AVCodecEncoder::_CloseCodecIfNeeded()
-{
-       if (fCodecContext != fOwnContext) {
-               // See _OpenCodecIfNeeded().
-               return;
-       }
-
-       if (fCodecInitStatus == CODEC_INIT_DONE) {
-               avcodec_close(fCodecContext);
-               fCodecInitStatus = CODEC_INIT_NEEDED;
-       }
 }
 
 
diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h 
b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h
index 22fe01c7ef..9e5e8cfdb9 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h
+++ b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h
@@ -50,7 +50,6 @@ private:
                        status_t                        _Setup();
 
                        bool                            _OpenCodecIfNeeded();
-                       void                            _CloseCodecIfNeeded();
 
                        status_t                        _EncodeAudio(const 
void* buffer,
                                                                        int64 
frameCount,
@@ -75,15 +74,9 @@ private:
                        // TODO: Refactor common base class from 
AVCodec[De|En]Coder!
                        CodecID                         fCodecID;
                        AVCodec*                        fCodec;
-                       AVCodecContext*         fOwnContext;
                        AVCodecContext*         fCodecContext;
 
-                       enum {
-                               CODEC_INIT_NEEDED = 0,
-                               CODEC_INIT_DONE,
-                               CODEC_INIT_FAILED
-                       };
-                       uint32                          fCodecInitStatus;
+                       bool                            fCodecInitialized;
 
                        // For video (color space conversion):
                        AVPicture                       fSrcFrame;


Other related posts: