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

  • From: Stefano Ceccherini <stefano.ceccherini@xxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sat, 4 Aug 2018 12:33:41 +0200

Nice one, I tried to approach this various times but not being confident
with ffmpeg I wasnt sure where I could do that and where not.

Il sab 4 ago 2018, 12:26 Barrett17 <b.vitruvio@xxxxxxxxx> ha scritto:

hrev52166 adds 6 changesets to branch 'master'
old head: 38fc668b2e71ad4fcad9dc1f91d88343c625e856
new head: 03437cd7b8459cde76e8ee5a1bab6aa386934111
overview:
https://git.haiku-os.org/haiku/log/?qt=range&q=03437cd7b845+%5E38fc668b2e71


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

334b8f3f9c2d: ffmpeg: Remove obsolete code

6283eeb2ffb2: AVFormatReader: Move codec to codecpar

428d87c581ab: AVFormatWriter: Move codec to codecpar

  * Little cleanup included.

ee9d0e02386b: AVFormatWriter: Rename fContext to fFormatContext

  * Seriously, this naming is bad.

e302fe32cf4b: AVCodecDecoder: Rename fContext to fCodecContext

03437cd7b845: AVCodecEncoder: Rename fContext to fCodecContext

                                        [ Barrett17 <b.vitruvio@xxxxxxxxx>
]


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

14 files changed, 321 insertions(+), 461 deletions(-)
.../media/plugins/ffmpeg/AVCodecDecoder.cpp      | 186 ++++++++----------
.../media/plugins/ffmpeg/AVCodecDecoder.h        |   8 +-
.../media/plugins/ffmpeg/AVCodecEncoder.cpp      | 195 ++++++++-----------
.../media/plugins/ffmpeg/AVCodecEncoder.h        |   4 +-
.../media/plugins/ffmpeg/AVFormatReader.cpp      | 132 +++++--------
.../media/plugins/ffmpeg/AVFormatWriter.cpp      | 152 +++++++--------
.../media/plugins/ffmpeg/AVFormatWriter.h        |   2 +-
src/add-ons/media/plugins/ffmpeg/CodecTable.cpp  |   8 -
.../media/plugins/ffmpeg/EncoderTable.cpp        |  24 ---
src/add-ons/media/plugins/ffmpeg/EncoderTable.h  |   2 -
.../media/plugins/ffmpeg/FFmpegPlugin.cpp        |   2 -
src/add-ons/media/plugins/ffmpeg/Utilities.h     |  37 ++++
src/add-ons/media/plugins/ffmpeg/gfx_util.cpp    |  26 ---
src/add-ons/media/plugins/ffmpeg/gfx_util.h      |   4 -


############################################################################

Commit:      334b8f3f9c2dbea74ad5f289d0114f47d816619b
URL:         https://git.haiku-os.org/haiku/commit/?id=334b8f3f9c2d
Author:      Barrett17 <b.vitruvio@xxxxxxxxx>
Date:        Fri Aug  3 14:36:30 2018 UTC

ffmpeg: Remove obsolete code


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

diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
index 13ec9f3834..f3a2880369 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
@@ -51,16 +51,7 @@
        static int sDumpedPackets = 0;
 #endif

-
-#if LIBAVCODEC_VERSION_INT > ((54 << 16) | (50 << 8))
 typedef AVCodecID CodecID;
-#endif
-#if LIBAVCODEC_VERSION_INT < ((55 << 16) | (45 << 8))
-#define av_frame_alloc avcodec_alloc_frame
-#define av_frame_unref avcodec_get_frame_defaults
-#define av_frame_free avcodec_free_frame
-#endif
-

 struct wave_format_ex {
        uint16 format_tag;
@@ -127,14 +118,11 @@ AVCodecDecoder::AVCodecDecoder()

        fDecodedDataBuffer(av_frame_alloc()),
        fDecodedDataBufferOffset(0),
-       fDecodedDataBufferSize(0)
-#if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
-       ,
+       fDecodedDataBufferSize(0),
        fBufferSinkContext(NULL),
        fBufferSourceContext(NULL),
        fFilterGraph(NULL),
        fFilterFrame(NULL)
-#endif
 {
        TRACE("AVCodecDecoder::AVCodecDecoder()\n");

@@ -173,10 +161,8 @@ AVCodecDecoder::~AVCodecDecoder()
        av_free(fContext);
        av_free(fDecodedDataBuffer);

-#if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
        av_frame_free(&fFilterFrame);
        avfilter_graph_free(&fFilterGraph);
-#endif

 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
        if (fSwsContext != NULL)
@@ -799,10 +785,8 @@
AVCodecDecoder::_ApplyEssentialAudioContainerPropertiesToContext()
                = static_cast<int>(containerProperties.frame_size);
        ConvertRawAudioFormatToAVSampleFormat(
                containerProperties.output.format, fContext->sample_fmt);
-#if LIBAVCODEC_VERSION_INT > ((52 << 16) | (114 << 8))
        ConvertRawAudioFormatToAVSampleFormat(
                containerProperties.output.format,
fContext->request_sample_fmt);
-#endif
        fContext->sample_rate
                = static_cast<int>(containerProperties.output.frame_rate);
        fContext->channels
@@ -1648,18 +1632,10 @@
AVCodecDecoder::_DeinterlaceAndColorConvertVideoFrame()
                avpicture_alloc(&deinterlacedPicture, fContext->pix_fmt,
displayWidth,
                        displayHeight);

-#if LIBAVCODEC_VERSION_INT < ((57 << 16) | (0 << 8))
-               if (avpicture_deinterlace(&deinterlacedPicture,
&rawPicture,
-                               fContext->pix_fmt, displayWidth,
displayHeight) < 0) {
-                       TRACE("[v] avpicture_deinterlace() - error\n");
-               } else
-                       useDeinterlacedPicture = true;
-#else
                // deinterlace implemented using avfilter
                _ProcessFilterGraph(&deinterlacedPicture, &rawPicture,
                                fContext->pix_fmt, displayWidth,
displayHeight);
                useDeinterlacedPicture = true;
-#endif
        }

        // Some decoders do not set pix_fmt until they have decoded 1 frame
@@ -1740,8 +1716,6 @@
AVCodecDecoder::_DeinterlaceAndColorConvertVideoFrame()
 }


-#if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
-
 /*! \brief Init the deinterlace filter graph.

        \returns B_OK the filter graph could be built.
@@ -1835,4 +1809,3 @@ AVCodecDecoder::_ProcessFilterGraph(AVPicture *dst,
const AVPicture *src,
        av_frame_unref(fFilterFrame);
        return B_OK;
 }
-#endif
diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h
b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h
index b2978cb502..4eb4e00564 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h
+++ b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h
@@ -19,11 +19,9 @@

 extern "C" {
        #include "avcodec.h"
-#if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
        #include "avfilter.h"
        #include "buffersink.h"
        #include "buffersrc.h"
-#endif
        #include "swresample.h"
        #include "swscale.h"
 }
@@ -100,14 +98,12 @@ private:
                        void            _UpdateMediaHeaderForVideoFrame();
                        status_t
_DeinterlaceAndColorConvertVideoFrame();

-#if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
                        // video deinterlace filter graph
                        status_t        _InitFilterGraph(enum
AVPixelFormat pixfmt,
                                                        int32 width, int32
height);
                        status_t        _ProcessFilterGraph(AVPicture *dst,
                                                        const AVPicture
*src, enum AVPixelFormat pixfmt,
                                                        int32 width, int32
height);
-#endif

                        media_header            fHeader;
                                                                        //
Contains the properties of the current
@@ -158,7 +154,6 @@ private:

                        AVPacket                        fTempPacket;

-#if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
                        // video deinterlace feature
                        AVFilterContext*        fBufferSinkContext;
                        AVFilterContext*        fBufferSourceContext;
@@ -167,7 +162,6 @@ private:
                        int32                           fLastWidth;
                        int32                           fLastHeight;
                        enum AVPixelFormat      fLastPixfmt;
-#endif
 };

 #endif // AVCODEC_DECODER_H
diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp
b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp
index c6608cb8b8..752de24c29 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp
@@ -35,18 +35,6 @@ extern "C" {

 static const size_t kDefaultChunkBufferSize = 2 * 1024 * 1024;

-#if LIBAVCODEC_VERSION_INT < ((54 << 16) | (50 << 8))
-#define AV_PIX_FMT_NONE PIX_FMT_NONE
-#define AV_CODEC_ID_NONE CODEC_ID_NONE
-#define AV_CODEC_ID_MPEG1VIDEO CODEC_ID_MPEG1VIDEO
-#define AV_CODEC_ID_MPEG2VIDEO CODEC_ID_MPEG2VIDEO
-#endif
-#if LIBAVCODEC_VERSION_INT < ((55 << 16) | (45 << 8))
-#define av_frame_alloc avcodec_alloc_frame
-#define av_frame_unref avcodec_get_frame_defaults
-#define av_frame_free avcodec_free_frame
-#endif
-

 AVCodecEncoder::AVCodecEncoder(uint32 codecID, int bitRateScale)
        :
@@ -716,17 +704,12 @@ AVCodecEncoder::_EncodeVideo(const void* buffer,
int64 frameCount,
                        fDstFrame.linesize);

                // Encode one video chunk/frame.
-#if LIBAVCODEC_VERSION_INT < ((55 << 16) | (45 << 8))
-               int usedBytes = avcodec_encode_video(fContext,
fChunkBuffer,
-                       kDefaultChunkBufferSize, fFrame);
-#else
                int gotPacket;
                AVPacket pkt;
                pkt.data = NULL;
                pkt.size = 0;
                av_init_packet(&pkt);
                int usedBytes = avcodec_encode_video2(fContext, &pkt,
fFrame, &gotPacket);
-#endif
                // avcodec.h says we need to set it.
                fFrame->pts++;

@@ -735,17 +718,6 @@ AVCodecEncoder::_EncodeVideo(const void* buffer,
int64 frameCount,
                        return B_ERROR;
                }

-#if LIBAVCODEC_VERSION_INT < ((55 << 16) | (45 << 8))
-               // Maybe we need to use this PTS to calculate start_time:
-               if (fContext->coded_frame->pts != kNoPTSValue) {
-                       TRACE("  codec frame PTS: %lld (codec time_base:
%d/%d)\n",
-                               fContext->coded_frame->pts,
fContext->time_base.num,
-                               fContext->time_base.den);
-               } else {
-                       TRACE("  codec frame PTS: N/A (codec time_base:
%d/%d)\n",
-                               fContext->time_base.num,
fContext->time_base.den);
-               }
-#else
                // Maybe we need to use this PTS to calculate start_time:
                if (pkt.pts != AV_NOPTS_VALUE) {
                        TRACE("  codec frame PTS: %lld (codec time_base:
%d/%d)\n",
@@ -755,7 +727,6 @@ AVCodecEncoder::_EncodeVideo(const void* buffer, int64
frameCount,
                        TRACE("  codec frame PTS: N/A (codec time_base:
%d/%d)\n",
                                fContext->time_base.num,
fContext->time_base.den);
                }
-#endif

                // Setup media_encode_info, most important is the time
stamp.
                info->start_time = (bigtime_t)(fFramesWritten * 1000000LL
@@ -766,11 +737,7 @@ AVCodecEncoder::_EncodeVideo(const void* buffer,
int64 frameCount,
                        info->flags |= B_MEDIA_KEY_FRAME;

                // Write the chunk
-#if LIBAVCODEC_VERSION_INT < ((55 << 16) | (45 << 8))
-               ret = WriteChunk(fChunkBuffer, usedBytes, info);
-#else
                ret = WriteChunk(pkt.data, pkt.size, info);
-#endif
                if (ret != B_OK) {
                        TRACE("  error writing chunk: %s\n",
strerror(ret));
                        break;
diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h
b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h
index 40cc42c808..b2e41625c2 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h
+++ b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h
@@ -17,9 +17,7 @@ extern "C" {
 #include "EncoderPlugin.h"


-#if LIBAVCODEC_VERSION_INT > ((54 << 16) | (50 << 8))
 typedef AVCodecID CodecID;
-#endif


 class AVCodecEncoder : public Encoder {
diff --git a/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp
b/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp
index bd7d8c29ee..211cd7a3bd 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp
@@ -47,14 +47,6 @@ extern "C" {

 #define ERROR(a...) fprintf(stderr, a)

-#if LIBAVCODEC_VERSION_INT < ((54 << 16) | (50 << 8))
-#define AV_CODEC_ID_PCM_S16BE CODEC_ID_PCM_S16BE
-#define AV_CODEC_ID_PCM_S16LE CODEC_ID_PCM_S16LE
-#define AV_CODEC_ID_PCM_U16BE CODEC_ID_PCM_U16BE
-#define AV_CODEC_ID_PCM_U16LE CODEC_ID_PCM_U16LE
-#define AV_CODEC_ID_PCM_S8 CODEC_ID_PCM_S8
-#define AV_CODEC_ID_PCM_U8 CODEC_ID_PCM_U8
-#endif

 static const int64 kNoPTSValue = AV_NOPTS_VALUE;

diff --git a/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
b/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
index 8a101b58fa..edd64a8e98 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
@@ -42,11 +42,6 @@ extern "C" {

 #define ERROR(a...) fprintf(stderr, a)

-#if LIBAVCODEC_VERSION_INT < ((54 << 16) | (50 << 8))
-#define AV_CODEC_ID_NONE CODEC_ID_NONE
-#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
-#endif
-

 static const size_t kIOBufferSize = 64 * 1024;
        // TODO: This could depend on the BMediaFile creation flags, IIRC,
@@ -58,9 +53,7 @@ static const size_t kIOBufferSize = 64 * 1024;
 #define OPEN_CODEC_CONTEXT 1
 #define GET_CONTEXT_DEFAULTS 0

-#if LIBAVCODEC_VERSION_INT > ((54 << 16) | (50 << 8))
 typedef AVCodecID CodecID;
-#endif

 // #pragma mark - AVFormatWriter::StreamCookie

diff --git a/src/add-ons/media/plugins/ffmpeg/CodecTable.cpp
b/src/add-ons/media/plugins/ffmpeg/CodecTable.cpp
index 302b1587ad..b27337f53e 100644
--- a/src/add-ons/media/plugins/ffmpeg/CodecTable.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/CodecTable.cpp
@@ -12,15 +12,7 @@ extern "C" {
 }


-#if LIBAVCODEC_VERSION_INT > ((54 << 16) | (50 << 8))
 typedef AVCodecID CodecID;
-#else
-#define AV_CODEC_ID_NONE CODEC_ID_NONE
-#define AV_CODEC_ID_PCM_S16LE CODEC_ID_PCM_S16LE
-#define AV_CODEC_ID_RAWVIDEO CODEC_ID_RAWVIDEO
-#define AV_CODEC_ID_DVD_SUBTITLE CODEC_ID_DVD_SUBTITLE
-#define AV_CODEC_ID_ADPCM_IMA_QT CODEC_ID_ADPCM_IMA_QT
-#endif


 //XXX: newer versions have it in libavformat/internal.h
diff --git a/src/add-ons/media/plugins/ffmpeg/EncoderTable.cpp
b/src/add-ons/media/plugins/ffmpeg/EncoderTable.cpp
index 2b300e612e..d0987b5ffe 100644
--- a/src/add-ons/media/plugins/ffmpeg/EncoderTable.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/EncoderTable.cpp
@@ -7,30 +7,6 @@
 #include "EncoderTable.h"


-#if LIBAVCODEC_VERSION_INT < ((54 << 16) | (50 << 8))
-#define AV_CODEC_ID_NONE CODEC_ID_NONE
-#define AV_CODEC_ID_PCM_F32LE CODEC_ID_PCM_F32LE
-#define AV_CODEC_ID_PCM_F64LE CODEC_ID_PCM_F64LE
-#define AV_CODEC_ID_PCM_S32LE CODEC_ID_PCM_S32LE
-#define AV_CODEC_ID_PCM_S16LE CODEC_ID_PCM_S16LE
-#define AV_CODEC_ID_PCM_U8 CODEC_ID_PCM_U8
-#define AV_CODEC_ID_PCM_F32BE CODEC_ID_PCM_F32BE
-#define AV_CODEC_ID_PCM_F64BE CODEC_ID_PCM_F64BE
-#define AV_CODEC_ID_PCM_S32BE CODEC_ID_PCM_S32BE
-#define AV_CODEC_ID_PCM_S16BE CODEC_ID_PCM_S16BE
-#define AV_CODEC_ID_AAC CODEC_ID_AAC
-#define AV_CODEC_ID_AC3 CODEC_ID_AC3
-#define AV_CODEC_ID_DVVIDEO CODEC_ID_DVVIDEO
-#define AV_CODEC_ID_FLAC CODEC_ID_FLAC
-#define AV_CODEC_ID_MJPEG CODEC_ID_MJPEG
-#define AV_CODEC_ID_MPEG1VIDEO CODEC_ID_MPEG1VIDEO
-#define AV_CODEC_ID_MPEG2VIDEO CODEC_ID_MPEG2VIDEO
-#define AV_CODEC_ID_MPEG4 CODEC_ID_MPEG4
-#define AV_CODEC_ID_THEORA CODEC_ID_THEORA
-#endif
-
-
-
 const EncoderDescription gEncoderTable[] = {
        // Video codecs
        {
diff --git a/src/add-ons/media/plugins/ffmpeg/EncoderTable.h
b/src/add-ons/media/plugins/ffmpeg/EncoderTable.h
index c2d36184b8..0ebe50d4f9 100644
--- a/src/add-ons/media/plugins/ffmpeg/EncoderTable.h
+++ b/src/add-ons/media/plugins/ffmpeg/EncoderTable.h
@@ -13,9 +13,7 @@ extern "C" {
 }


-#if LIBAVCODEC_VERSION_INT > ((54 << 16) | (50 << 8))
 typedef AVCodecID CodecID;
-#endif


 struct EncoderDescription {
diff --git a/src/add-ons/media/plugins/ffmpeg/FFmpegPlugin.cpp
b/src/add-ons/media/plugins/ffmpeg/FFmpegPlugin.cpp
index 83697b41bc..b1545ef18b 100644
--- a/src/add-ons/media/plugins/ffmpeg/FFmpegPlugin.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/FFmpegPlugin.cpp
@@ -87,9 +87,7 @@ FFmpegPlugin::GlobalInitilizer::GlobalInitilizer()

        av_register_all();
                // This will also call av_codec_init() by registering
codecs.
-#if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
        avfilter_register_all();
-#endif
 }


diff --git a/src/add-ons/media/plugins/ffmpeg/gfx_util.cpp
b/src/add-ons/media/plugins/ffmpeg/gfx_util.cpp
index f20c61c251..f641744d34 100644
--- a/src/add-ons/media/plugins/ffmpeg/gfx_util.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/gfx_util.cpp
@@ -22,32 +22,6 @@ extern "C" {
   #define TRACE(a...)
 #endif

-#if LIBAVCODEC_VERSION_INT < ((54 << 16) | (50 << 8))
-#define AVPixelFormat PixelFormat
-#define AV_PIX_FMT_NONE PIX_FMT_NONE
-#define AV_PIX_FMT_YUV410P PIX_FMT_YUV410P
-#define AV_PIX_FMT_YUV411P PIX_FMT_YUV411P
-#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
-#define AV_PIX_FMT_YUVJ420P PIX_FMT_YUVJ420P
-#define AV_PIX_FMT_YUV422P PIX_FMT_YUV422P
-#define AV_PIX_FMT_YUVJ422P PIX_FMT_YUVJ422P
-#define AV_PIX_FMT_YUYV422 PIX_FMT_YUYV422
-#define AV_PIX_FMT_YUV420P10LE PIX_FMT_YUV420P10LE
-#define AV_PIX_FMT_YUV444P PIX_FMT_YUV444P
-#define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
-#define AV_PIX_FMT_BGR24 PIX_FMT_BGR24
-#define AV_PIX_FMT_RGB565 PIX_FMT_RGB565
-#define AV_PIX_FMT_RGB555 PIX_FMT_RGB555
-#define AV_PIX_FMT_GRAY8 PIX_FMT_GRAY8
-#define AV_PIX_FMT_MONOBLACK PIX_FMT_MONOBLACK
-#define AV_PIX_FMT_PAL8 PIX_FMT_PAL8
-#define AV_PIX_FMT_BGR32 PIX_FMT_BGR32
-#define AV_PIX_FMT_BGR565 PIX_FMT_BGR565
-#define AV_PIX_FMT_BGR555 PIX_FMT_BGR555
-#define AV_PIX_FMT_RGB32 PIX_FMT_RGB32
-#define AV_PIX_FMT_GBRP PIX_FMT_GBRP
-#endif
-

 //! This function will try to find the best colorspaces for both the
ff-codec
 // and the Media Kit sides.
diff --git a/src/add-ons/media/plugins/ffmpeg/gfx_util.h
b/src/add-ons/media/plugins/ffmpeg/gfx_util.h
index cc1f63806d..aa8aa4f301 100644
--- a/src/add-ons/media/plugins/ffmpeg/gfx_util.h
+++ b/src/add-ons/media/plugins/ffmpeg/gfx_util.h
@@ -28,10 +28,6 @@ extern "C" {
        #include "libavcodec/avcodec.h"
 }

-#if LIBAVCODEC_VERSION_INT < ((54 << 16) | (50 << 8))
-typedef PixelFormat AVPixelFormat;
-#endif
-

 // this function will be used by the wrapper to write into
 // the Media Kit provided buffer from the self-allocated ffmpeg codec
buffer


############################################################################

Commit:      6283eeb2ffb2f31d2f7312cea48476ca7d32b9dd
URL:         https://git.haiku-os.org/haiku/commit/?id=6283eeb2ffb2
Author:      Barrett17 <b.vitruvio@xxxxxxxxx>
Date:        Sat Aug  4 09:23:06 2018 UTC

AVFormatReader: Move codec to codecpar


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

diff --git a/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp
b/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp
index 211cd7a3bd..57cdd67b00 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp
@@ -1,6 +1,7 @@
 /*
  * Copyright 2009-2010, Stephan Aßmus <superstippi@xxxxxx>
  * Copyright 2014, Colin Günther <coling@xxxxxx>
+ * Copyright 2018, Dario Casalinuovo
  * All rights reserved. Distributed under the terms of the GNU L-GPL
license.
  */

@@ -398,9 +399,9 @@ StreamBase::FrameRate() const
 {
        // TODO: Find a way to always calculate a correct frame rate...
        double frameRate = 1.0;
-       switch (fStream->codec->codec_type) {
+       switch (fStream->codecpar->codec_type) {
                case AVMEDIA_TYPE_AUDIO:
-                       frameRate = (double)fStream->codec->sample_rate;
+                       frameRate = (double)fStream->codecpar->sample_rate;
                        break;
                case AVMEDIA_TYPE_VIDEO:
                        if (fStream->avg_frame_rate.den &&
fStream->avg_frame_rate.num)
@@ -409,10 +410,6 @@ StreamBase::FrameRate() const
                                frameRate = av_q2d(fStream->r_frame_rate);
                        else if (fStream->time_base.den &&
fStream->time_base.num)
                                frameRate = 1 / av_q2d(fStream->time_base);
-                       else if (fStream->codec->time_base.den
-                               && fStream->codec->time_base.num) {
-                               frameRate = 1 /
av_q2d(fStream->codec->time_base);
-                       }

                        // TODO: Fix up interlaced video for real
                        if (frameRate == 50.0f)
@@ -962,32 +959,8 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
        if (ret != B_OK)
                return ret;

-       // Get a pointer to the AVCodecContext for the stream at
streamIndex.
-       AVCodecContext* codecContext = fStream->codec;
-
-#if 0
-// stippi: Here I was experimenting with the question if some fields of
the
-// AVCodecContext change (or get filled out at all), if the AVCodec is
opened.
-       class CodecOpener {
-       public:
-               CodecOpener(AVCodecContext* context)
-               {
-                       fCodecContext = context;
-                       AVCodec* codec =
avcodec_find_decoder(context->codec_id);
-                       fCodecOpen = avcodec_open(context, codec) >= 0;
-                       if (!fCodecOpen)
-                               TRACE("  failed to open the codec!\n");
-               }
-               ~CodecOpener()
-               {
-                       if (fCodecOpen)
-                               avcodec_close(fCodecContext);
-               }
-       private:
-               AVCodecContext*         fCodecContext;
-               bool                            fCodecOpen;
-       } codecOpener(codecContext);
-#endif
+       // Get a pointer to the AVCodecPaarameters for the stream at
streamIndex.
+       AVCodecParameters* codecParams = fStream->codecpar;

        // initialize the media_format for this stream
        media_format* format = &fFormat;
@@ -996,10 +969,10 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
        media_format_description description;

        // Set format family and type depending on codec_type of the
stream.
-       switch (codecContext->codec_type) {
+       switch (codecParams->codec_type) {
                case AVMEDIA_TYPE_AUDIO:
-                       if ((codecContext->codec_id >=
AV_CODEC_ID_PCM_S16LE)
-                               && (codecContext->codec_id <=
AV_CODEC_ID_PCM_U8)) {
+                       if ((codecParams->codec_id >=
AV_CODEC_ID_PCM_S16LE)
+                               && (codecParams->codec_id <=
AV_CODEC_ID_PCM_U8)) {
                                TRACE("  raw audio\n");
                                format->type = B_MEDIA_RAW_AUDIO;
                                description.family = B_ANY_FORMAT_FAMILY;
@@ -1027,7 +1000,7 @@ AVFormatReader::Stream::Init(int32 virtualIndex)

        if (format->type == B_MEDIA_RAW_AUDIO) {
                // We cannot describe all raw-audio formats, some are
unsupported.
-               switch (codecContext->codec_id) {
+               switch (codecParams->codec_id) {
                        case AV_CODEC_ID_PCM_S16LE:
                                format->u.raw_audio.format
                                        =
media_raw_audio_format::B_AUDIO_SHORT;
@@ -1068,7 +1041,7 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
                }
        } else {
                if (description.family == B_MISC_FORMAT_FAMILY)
-                       description.u.misc.codec = codecContext->codec_id;
+                       description.u.misc.codec = codecParams->codec_id;

                BMediaFormats formats;
                status_t status = formats.GetFormatFor(description,
format);
@@ -1076,7 +1049,7 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
                        TRACE("  formats.GetFormatFor() error: %s\n",
strerror(status));

                format->user_data_type = B_CODEC_TYPE_INFO;
-               *(uint32*)format->user_data = codecContext->codec_tag;
+               *(uint32*)format->user_data = codecParams->codec_tag;
                format->user_data[4] = 0;
        }

@@ -1085,10 +1058,11 @@ AVFormatReader::Stream::Init(int32 virtualIndex)

        switch (format->type) {
                case B_MEDIA_RAW_AUDIO:
-                       format->u.raw_audio.frame_rate =
(float)codecContext->sample_rate;
-                       format->u.raw_audio.channel_count =
codecContext->channels;
-                       format->u.raw_audio.channel_mask =
codecContext->channel_layout;
-
 ConvertAVSampleFormatToRawAudioFormat(codecContext->sample_fmt,
+                       format->u.raw_audio.frame_rate =
(float)codecParams->sample_rate;
+                       format->u.raw_audio.channel_count =
codecParams->channels;
+                       format->u.raw_audio.channel_mask =
codecParams->channel_layout;
+                       ConvertAVSampleFormatToRawAudioFormat(
+                               (AVSampleFormat)codecParams->format,
                                format->u.raw_audio.format);
                        format->u.raw_audio.buffer_size = 0;

@@ -1102,28 +1076,32 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
                        break;

                case B_MEDIA_ENCODED_AUDIO:
-                       format->u.encoded_audio.bit_rate =
codecContext->bit_rate;
-                       format->u.encoded_audio.frame_size =
codecContext->frame_size;
+                       format->u.encoded_audio.bit_rate =
codecParams->bit_rate;
+                       format->u.encoded_audio.frame_size =
codecParams->frame_size;
                        // Fill in some info about possible output format
                        format->u.encoded_audio.output
                                = media_multi_audio_format::wildcard;
                        format->u.encoded_audio.output.frame_rate
-                               = (float)codecContext->sample_rate;
+                               = (float)codecParams->sample_rate;
                        // Channel layout bits match in Be API and FFmpeg.
                        format->u.encoded_audio.output.channel_count
-                               = codecContext->channels;
+                               = codecParams->channels;
                        format->u.encoded_audio.multi_info.channel_mask
-                               = codecContext->channel_layout;
+                               = codecParams->channel_layout;
                        format->u.encoded_audio.output.byte_order
-                               =
avformat_to_beos_byte_order(codecContext->sample_fmt);
-
 ConvertAVSampleFormatToRawAudioFormat(codecContext->sample_fmt,
+                               = avformat_to_beos_byte_order(
+
 (AVSampleFormat)codecParams->format);
+
+                       ConvertAVSampleFormatToRawAudioFormat(
+
 (AVSampleFormat)codecParams->format,
                                format->u.encoded_audio.output.format);
-                       if (codecContext->block_align > 0) {
+
+                       if (codecParams->block_align > 0) {
                                format->u.encoded_audio.output.buffer_size
-                                       = codecContext->block_align;
+                                       = codecParams->block_align;
                        } else {
                                format->u.encoded_audio.output.buffer_size
-                                       = codecContext->frame_size *
codecContext->channels
+                                       = codecParams->frame_size *
codecParams->channels
                                                *
(format->u.encoded_audio.output.format
                                                        &
media_raw_audio_format::B_AUDIO_SIZE_MASK);
                        }
@@ -1132,9 +1110,9 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
                case B_MEDIA_ENCODED_VIDEO:
 // TODO: Specifying any of these seems to throw off the format matching
 // later on.
-//                     format->u.encoded_video.avg_bit_rate =
codecContext->bit_rate;
-//                     format->u.encoded_video.max_bit_rate =
codecContext->bit_rate
-//                             + codecContext->bit_rate_tolerance;
+//                     format->u.encoded_video.avg_bit_rate =
codecParams->bit_rate;
+//                     format->u.encoded_video.max_bit_rate =
codecParams->bit_rate
+//                             + codecParams->bit_rate_tolerance;

 //                     format->u.encoded_video.encoding
 //                             = media_encoded_video_format::B_ANY;
@@ -1148,24 +1126,24 @@ AVFormatReader::Stream::Init(int32 virtualIndex)

                        format->u.encoded_video.output.first_active = 0;
                        format->u.encoded_video.output.last_active
-                               = codecContext->height - 1;
+                               = codecParams->height - 1;
                                // TODO: Maybe libavformat actually
provides that info
                                // somewhere...
                        format->u.encoded_video.output.orientation
                                = B_VIDEO_TOP_LEFT_RIGHT;

-
 ConvertAVCodecContextToVideoAspectWidthAndHeight(*codecContext,
+
 ConvertAVCodecParametersToVideoAspectWidthAndHeight(*codecParams,

format->u.encoded_video.output.pixel_width_aspect,

format->u.encoded_video.output.pixel_height_aspect);

                        format->u.encoded_video.output.display.format
-                               =
pixfmt_to_colorspace(codecContext->pix_fmt);
+                               =
pixfmt_to_colorspace(codecParams->format);
                        format->u.encoded_video.output.display.line_width
-                               = codecContext->width;
+                               = codecParams->width;
                        format->u.encoded_video.output.display.line_count
-                               = codecContext->height;
-                       TRACE("  width/height: %d/%d\n",
codecContext->width,
-                               codecContext->height);
+                               = codecParams->height;
+                       TRACE("  width/height: %d/%d\n",
codecParams->width,
+                               codecParams->height);

format->u.encoded_video.output.display.bytes_per_row = 0;

format->u.encoded_video.output.display.pixel_offset = 0;
                        format->u.encoded_video.output.display.line_offset
= 0;
@@ -1179,17 +1157,17 @@ AVFormatReader::Stream::Init(int32 virtualIndex)
        }

        // Add the meta data, if any
-       if (codecContext->extradata_size > 0) {
-               format->SetMetaData(codecContext->extradata,
-                       codecContext->extradata_size);
+       if (codecParams->extradata_size > 0) {
+               format->SetMetaData(codecParams->extradata,
+                       codecParams->extradata_size);
                TRACE("  extradata: %p\n", format->MetaData());
        }

-       TRACE("  extradata_size: %d\n", codecContext->extradata_size);
-//     TRACE("  intra_matrix: %p\n", codecContext->intra_matrix);
-//     TRACE("  inter_matrix: %p\n", codecContext->inter_matrix);
-//     TRACE("  get_buffer(): %p\n", codecContext->get_buffer);
-//     TRACE("  release_buffer(): %p\n", codecContext->release_buffer);
+       TRACE("  extradata_size: %d\n", codecParams->extradata_size);
+//     TRACE("  intra_matrix: %p\n", codecParams->intra_matrix);
+//     TRACE("  inter_matrix: %p\n", codecParams->inter_matrix);
+//     TRACE("  get_buffer(): %p\n", codecParams->get_buffer);
+//     TRACE("  release_buffer(): %p\n", codecParams->release_buffer);

 #ifdef TRACE_AVFORMAT_READER
        char formatString[512];
@@ -1268,7 +1246,7 @@ AVFormatReader::Stream::GetStreamInfo(int64*
frameCount,
        }
        #endif

-       *frameCount = fStream->nb_frames * fStream->codec->frame_size;
+       *frameCount = fStream->nb_frames * fStream->codecpar->frame_size;
        if (*frameCount == 0) {
                // Calculate from duration and frame rate
                *frameCount = (int64)(*duration * frameRate / 1000000LL);
@@ -1279,8 +1257,8 @@ AVFormatReader::Stream::GetStreamInfo(int64*
frameCount,

        *format = fFormat;

-       *infoBuffer = fStream->codec->extradata;
-       *infoSize = fStream->codec->extradata_size;
+       *infoBuffer = fStream->codecpar->extradata;
+       *infoSize = fStream->codecpar->extradata_size;

        return B_OK;
 }


############################################################################

Commit:      428d87c581abd559965c1ea8d1eab2256c8fc55d
URL:         https://git.haiku-os.org/haiku/commit/?id=428d87c581ab
Author:      Barrett17 <b.vitruvio@xxxxxxxxx>
Date:        Sat Aug  4 09:24:05 2018 UTC

AVFormatWriter: Move codec to codecpar

* Little cleanup included.


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

diff --git a/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
b/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
index edd64a8e98..0f66a31de5 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
@@ -1,5 +1,6 @@
 /*
  * Copyright 2009-2010, Stephan Aßmus <superstippi@xxxxxx>
+ * Copyright 2018, Dario Casalinuovo
  * All rights reserved. Distributed under the terms of the GNU L-GPL
license.
  */

@@ -118,30 +119,22 @@ AVFormatWriter::StreamCookie::Init(media_format*
format,
                return B_ERROR;
        }

-//     TRACE("  fStream->codec: %p\n", fStream->codec);
+//     TRACE("  fStream->codecpar: %p\n", fStream->codecpar);
        // TODO: This is a hack for now! Use avcodec_find_encoder_by_name()
        // or something similar...
-       fStream->codec->codec_id = (CodecID)codecInfo->sub_id;
-       if (fStream->codec->codec_id == AV_CODEC_ID_NONE)
-               fStream->codec->codec_id = raw_audio_codec_id_for(*format);
+       fStream->codecpar->codec_id = (CodecID)codecInfo->sub_id;
+       if (fStream->codecpar->codec_id == AV_CODEC_ID_NONE)
+               fStream->codecpar->codec_id =
raw_audio_codec_id_for(*format);

        // Setup the stream according to the media format...
        if (format->type == B_MEDIA_RAW_VIDEO) {
-               fStream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-#if GET_CONTEXT_DEFAULTS
-// NOTE: API example does not do this:
-               avcodec_get_context_defaults(fStream->codec);
-#endif
-               // frame rate
-               fStream->codec->time_base.den =
(int)format->u.raw_video.field_rate;
-               fStream->codec->time_base.num = 1;
-
+               fStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
                fStream->time_base.den =
(int)format->u.raw_video.field_rate;
                fStream->time_base.num = 1;

                // video size
-               fStream->codec->width =
format->u.raw_video.display.line_width;
-               fStream->codec->height =
format->u.raw_video.display.line_count;
+               fStream->codecpar->width =
format->u.raw_video.display.line_width;
+               fStream->codecpar->height =
format->u.raw_video.display.line_count;
                // pixel aspect ratio
                fStream->sample_aspect_ratio.num
                        = format->u.raw_video.pixel_width_aspect;
@@ -150,51 +143,46 @@ AVFormatWriter::StreamCookie::Init(media_format*
format,
                if (fStream->sample_aspect_ratio.num == 0
                        || fStream->sample_aspect_ratio.den == 0) {
                        av_reduce(&fStream->sample_aspect_ratio.num,
-                               &fStream->sample_aspect_ratio.den,
fStream->codec->width,
-                               fStream->codec->height, 255);
+                               &fStream->sample_aspect_ratio.den,
fStream->codecpar->width,
+                               fStream->codecpar->height, 255);
                }

-               fStream->codec->gop_size = 12;
-
-               fStream->codec->sample_aspect_ratio =
fStream->sample_aspect_ratio;
+               fStream->codecpar->sample_aspect_ratio =
fStream->sample_aspect_ratio;

                // Use the last supported pixel format of the AVCodec,
which we hope
                // is the one with the best quality (true for all
currently supported
                // encoders).
-//             AVCodec* codec = fStream->codec->codec;
+//             AVCodec* codec = fStream->codecpar->codec;
 //             for (int i = 0; codec->pix_fmts[i] != PIX_FMT_NONE; i++)
-//                     fStream->codec->pix_fmt = codec->pix_fmts[i];
-               fStream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
+//                     fStream->codecpar->pix_fmt = codec->pix_fmts[i];
+               fStream->codecpar->format = AV_PIX_FMT_YUV420P;

        } else if (format->type == B_MEDIA_RAW_AUDIO) {
-               fStream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
-#if GET_CONTEXT_DEFAULTS
-// NOTE: API example does not do this:
-               avcodec_get_context_defaults(fStream->codec);
-#endif
+               fStream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+
                // frame rate
-               fStream->codec->sample_rate =
(int)format->u.raw_audio.frame_rate;
+               fStream->codecpar->sample_rate =
(int)format->u.raw_audio.frame_rate;

                // channels
-               fStream->codec->channels =
format->u.raw_audio.channel_count;
+               fStream->codecpar->channels =
format->u.raw_audio.channel_count;

                // set fStream to the audio format we want to use. This is
only a hint
                // (each encoder has a different set of accepted formats)
                switch (format->u.raw_audio.format) {
                        case media_raw_audio_format::B_AUDIO_FLOAT:
-                               fStream->codec->sample_fmt =
AV_SAMPLE_FMT_FLT;
+                               fStream->codecpar->format =
AV_SAMPLE_FMT_FLT;
                                break;
                        case media_raw_audio_format::B_AUDIO_DOUBLE:
-                               fStream->codec->sample_fmt =
AV_SAMPLE_FMT_DBL;
+                               fStream->codecpar->format =
AV_SAMPLE_FMT_DBL;
                                break;
                        case media_raw_audio_format::B_AUDIO_INT:
-                               fStream->codec->sample_fmt =
AV_SAMPLE_FMT_S32;
+                               fStream->codecpar->format =
AV_SAMPLE_FMT_S32;
                                break;
                        case media_raw_audio_format::B_AUDIO_SHORT:
-                               fStream->codec->sample_fmt =
AV_SAMPLE_FMT_S16;
+                               fStream->codecpar->format =
AV_SAMPLE_FMT_S16;
                                break;
                        case media_raw_audio_format::B_AUDIO_UCHAR:
-                               fStream->codec->sample_fmt =
AV_SAMPLE_FMT_U8;
+                               fStream->codecpar->format =
AV_SAMPLE_FMT_U8;
                                break;

                        case media_raw_audio_format::B_AUDIO_CHAR:
@@ -205,24 +193,24 @@ AVFormatWriter::StreamCookie::Init(media_format*
format,

                // Now negociate the actual format with the encoder
                // First check if the requested format is acceptable
-               AVCodec* codec =
avcodec_find_encoder(fStream->codec->codec_id);
+               AVCodec* codec =
avcodec_find_encoder(fStream->codecpar->codec_id);

                if (codec == NULL)
                        return B_MEDIA_BAD_FORMAT;

                const enum AVSampleFormat *p = codec->sample_fmts;
                for (; *p != -1; p++) {
-                       if (*p == fStream->codec->sample_fmt)
+                       if (*p == fStream->codecpar->format)
                                break;
                }
                // If not, force one of the acceptable ones
                if (*p == -1) {
-                       fStream->codec->sample_fmt = codec->sample_fmts[0];
+                       fStream->codecpar->format = codec->sample_fmts[0];

                        // And finally set the format struct to the
accepted format. It is
                        // then up to the caller to make sure we get data
matching that
                        // format.
-                       switch (fStream->codec->sample_fmt) {
+                       switch (fStream->codecpar->format) {
                                case AV_SAMPLE_FMT_FLT:
                                        format->u.raw_audio.format
                                                =
media_raw_audio_format::B_AUDIO_FLOAT;
@@ -254,43 +242,39 @@ AVFormatWriter::StreamCookie::Init(media_format*
format,
                        switch (format->u.raw_audio.channel_count) {
                                default:
                                case 2:
-                                       fStream->codec->channel_layout =
AV_CH_LAYOUT_STEREO;
+                                       fStream->codecpar->channel_layout
= AV_CH_LAYOUT_STEREO;
                                        break;
                                case 1:
-                                       fStream->codec->channel_layout =
AV_CH_LAYOUT_MONO;
+                                       fStream->codecpar->channel_layout
= AV_CH_LAYOUT_MONO;
                                        break;
                                case 3:
-                                       fStream->codec->channel_layout =
AV_CH_LAYOUT_SURROUND;
+                                       fStream->codecpar->channel_layout
= AV_CH_LAYOUT_SURROUND;
                                        break;
                                case 4:
-                                       fStream->codec->channel_layout =
AV_CH_LAYOUT_QUAD;
+                                       fStream->codecpar->channel_layout
= AV_CH_LAYOUT_QUAD;
                                        break;
                                case 5:
-                                       fStream->codec->channel_layout =
AV_CH_LAYOUT_5POINT0;
+                                       fStream->codecpar->channel_layout
= AV_CH_LAYOUT_5POINT0;
                                        break;
                                case 6:
-                                       fStream->codec->channel_layout =
AV_CH_LAYOUT_5POINT1;
+                                       fStream->codecpar->channel_layout
= AV_CH_LAYOUT_5POINT1;
                                        break;
                                case 8:
-                                       fStream->codec->channel_layout =
AV_CH_LAYOUT_7POINT1;
+                                       fStream->codecpar->channel_layout
= AV_CH_LAYOUT_7POINT1;
                                        break;
                                case 10:
-                                       fStream->codec->channel_layout =
AV_CH_LAYOUT_7POINT1_WIDE;
+                                       fStream->codecpar->channel_layout
= AV_CH_LAYOUT_7POINT1_WIDE;
                                        break;
                        }
                } else {
                        // The bits match 1:1 for media_multi_channels and
FFmpeg defines.
-                       fStream->codec->channel_layout =
format->u.raw_audio.channel_mask;
+                       fStream->codecpar->channel_layout =
format->u.raw_audio.channel_mask;
                }
        }

-       // Some formats want stream headers to be separate
-       if ((fContext->oformat->flags & AVFMT_GLOBALHEADER) != 0)
-               fStream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
-
        TRACE("  stream->time_base: (%d/%d), codec->time_base: (%d/%d))\n",
                fStream->time_base.num, fStream->time_base.den,
-               fStream->codec->time_base.num,
fStream->codec->time_base.den);
+               fStream->codecpar->time_base.num,
fStream->codecpar->time_base.den);

 #if 0
        // Write the AVCodecContext pointer to the user data section of the
@@ -337,7 +321,7 @@ AVFormatWriter::StreamCookie::WriteChunk(const void*
chunkBuffer,
        TRACE_PACKET("  PTS: %lld (stream->time_base: (%d/%d), "
                "codec->time_base: (%d/%d))\n", fPacket.pts,
                fStream->time_base.num, fStream->time_base.den,
-               fStream->codec->time_base.num,
fStream->codec->time_base.den);
+               fStream->codecpar->time_base.num,
fStream->codecpar->time_base.den);

 #if 0
        // TODO: Eventually, we need to write interleaved packets, but
@@ -395,10 +379,13 @@ AVFormatWriter::~AVFormatWriter()
 #if OPEN_CODEC_CONTEXT
                // We only need to close the AVCodecContext when we opened
it.
                // This is experimental, see CommitHeader().
+
+               // NOTE: Since the introduction of AVCodecParameters
+               // I am not sure this logic is correct.
                if (fCodecOpened)
                        avcodec_close(fContext->streams[i]->codec);
 #endif
-               av_freep(&fContext->streams[i]->codec);
+               av_freep(&fContext->streams[i]->codecpar);
                av_freep(&fContext->streams[i]);
     }

diff --git a/src/add-ons/media/plugins/ffmpeg/Utilities.h
b/src/add-ons/media/plugins/ffmpeg/Utilities.h
index 39039482c3..956adb14b2 100644
--- a/src/add-ons/media/plugins/ffmpeg/Utilities.h
+++ b/src/add-ons/media/plugins/ffmpeg/Utilities.h
@@ -1,6 +1,7 @@
 /*
  * Copyright 2009, Stephan Aßmus <superstippi@xxxxxx>
  * Copyright 2014, Colin Günther <coling@xxxxxx>
+ * Copyright 2018, Dario Casalinuovo
  * All rights reserved. Distributed under the terms of the GNU L-GPL
license.
  */
 #ifndef UTILITIES_H
@@ -75,6 +76,42 @@
ConvertAVCodecContextToVideoAspectWidthAndHeight(AVCodecContext& contextIn,
 }


+inline void
+ConvertAVCodecParametersToVideoAspectWidthAndHeight(AVCodecParameters&
parametersIn,
+       uint16& pixelWidthAspectOut, uint16& pixelHeightAspectOut)
+{
+       assert(parametersIn.sample_aspect_ratio.num >= 0);
+       assert(parametersIn.width > 0);
+       assert(parametersIn.height > 0);
+
+       // The following code is based on code originally located in
+       // AVFormatReader::Stream::Init() and thus should be copyrighted
to Stephan
+       // Aßmus
+       AVRational pixelAspectRatio;
+
+       if (parametersIn.sample_aspect_ratio.num == 0
+               || parametersIn.sample_aspect_ratio.den == 0) {
+               // AVCodecContext doesn't contain a video aspect ratio, so
calculate it
+               // ourselve based solely on the video dimensions
+               av_reduce(&pixelAspectRatio.num, &pixelAspectRatio.den,
parametersIn.width,
+                       parametersIn.height, 1024 * 1024);
+
+               pixelWidthAspectOut =
static_cast<int16>(pixelAspectRatio.num);
+               pixelHeightAspectOut =
static_cast<int16>(pixelAspectRatio.den);
+               return;
+       }
+
+       // AVCodecContext contains a video aspect ratio, so use it
+       av_reduce(&pixelAspectRatio.num, &pixelAspectRatio.den,
+               parametersIn.width * parametersIn.sample_aspect_ratio.num,
+               parametersIn.height * parametersIn.sample_aspect_ratio.den,
+               1024 * 1024);
+
+       pixelWidthAspectOut = static_cast<int16>(pixelAspectRatio.num);
+       pixelHeightAspectOut = static_cast<int16>(pixelAspectRatio.den);
+}
+
+
 /*!    \brief Converts the Media Kits notation of video aspect ratio into
FFmpegs
                notation.



############################################################################

Commit:      ee9d0e02386b59729775990057b246d86f4a68f5
URL:         https://git.haiku-os.org/haiku/commit/?id=ee9d0e02386b
Author:      Barrett17 <b.vitruvio@xxxxxxxxx>
Date:        Sat Aug  4 09:31:54 2018 UTC

AVFormatWriter: Rename fContext to fFormatContext

* Seriously, this naming is bad.


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

diff --git a/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
b/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
index 0f66a31de5..47b1305d1e 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
@@ -76,7 +76,7 @@ public:

size_t size, uint32 flags);

 private:
-                       AVFormatContext*        fContext;
+                       AVFormatContext*        fFormatContext;
                        AVStream*                       fStream;
                        AVPacket                        fPacket;
                        // Since different threads may write to the target,
@@ -89,7 +89,7 @@ private:
 AVFormatWriter::StreamCookie::StreamCookie(AVFormatContext* context,
                BLocker* streamLock)
        :
-       fContext(context),
+       fFormatContext(context),
        fStream(NULL),
        fStreamLock(streamLock)
 {
@@ -110,8 +110,8 @@ AVFormatWriter::StreamCookie::Init(media_format*
format,

        BAutolock _(fStreamLock);

-       fPacket.stream_index = fContext->nb_streams;
-       fStream = avformat_new_stream(fContext, NULL);
+       fPacket.stream_index = fFormatContext->nb_streams;
+       fStream = avformat_new_stream(fFormatContext, NULL);
        fStream->id = fPacket.stream_index;

        if (fStream == NULL) {
@@ -329,11 +329,11 @@ AVFormatWriter::StreamCookie::WriteChunk(const void*
chunkBuffer,
        // more than one stream. For the moment, this crashes in AVPacket
        // shuffling inside libavformat. Maybe if we want to use this, we
        // need to allocate a separate AVPacket and copy the chunk buffer.
-       int result = av_interleaved_write_frame(fContext, &fPacket);
+       int result = av_interleaved_write_frame(fFormatContext, &fPacket);
        if (result < 0)
                TRACE("  av_interleaved_write_frame(): %d\n", result);
 #else
-       int result = av_write_frame(fContext, &fPacket);
+       int result = av_write_frame(fFormatContext, &fPacket);
        if (result < 0)
                TRACE("  av_write_frame(): %d\n", result);
 #endif
@@ -360,7 +360,7 @@ AVFormatWriter::StreamCookie::AddTrackInfo(uint32 code,

 AVFormatWriter::AVFormatWriter()
        :
-       fContext(avformat_alloc_context()),
+       fFormatContext(avformat_alloc_context()),
        fCodecOpened(false),
        fHeaderError(-1),
        fIOContext(NULL),
@@ -375,7 +375,7 @@ AVFormatWriter::~AVFormatWriter()
        TRACE("AVFormatWriter::~AVFormatWriter\n");

        // Free the streams and close the AVCodecContexts
-    for(unsigned i = 0; i < fContext->nb_streams; i++) {
+    for(unsigned i = 0; i < fFormatContext->nb_streams; i++) {
 #if OPEN_CODEC_CONTEXT
                // We only need to close the AVCodecContext when we opened
it.
                // This is experimental, see CommitHeader().
@@ -383,13 +383,13 @@ AVFormatWriter::~AVFormatWriter()
                // NOTE: Since the introduction of AVCodecParameters
                // I am not sure this logic is correct.
                if (fCodecOpened)
-                       avcodec_close(fContext->streams[i]->codec);
+                       avcodec_close(fFormatContext->streams[i]->codec);
 #endif
-               av_freep(&fContext->streams[i]->codecpar);
-               av_freep(&fContext->streams[i]);
+               av_freep(&fFormatContext->streams[i]->codecpar);
+               av_freep(&fFormatContext->streams[i]);
     }

-       av_free(fContext);
+       av_free(fFormatContext);
        av_free(fIOContext->buffer);
        av_free(fIOContext);
 }
@@ -417,19 +417,19 @@ AVFormatWriter::Init(const media_file_format*
fileFormat)
        }

        // Setup I/O hooks. This seems to be enough.
-       fContext->pb = fIOContext;
+       fFormatContext->pb = fIOContext;

        // Set the AVOutputFormat according to fileFormat...
-       fContext->oformat = av_guess_format(fileFormat->short_name,
+       fFormatContext->oformat = av_guess_format(fileFormat->short_name,
                fileFormat->file_extension, fileFormat->mime_type);
-       if (fContext->oformat == NULL) {
+       if (fFormatContext->oformat == NULL) {
                TRACE("  failed to find AVOuputFormat for %s\n",
                        fileFormat->short_name);
                return B_NOT_SUPPORTED;
        }

        TRACE("  found AVOuputFormat for %s: %s\n", fileFormat->short_name,
-               fContext->oformat->name);
+               fFormatContext->oformat->name);

        return B_OK;
 }
@@ -449,15 +449,15 @@ AVFormatWriter::CommitHeader()
 {
        TRACE("AVFormatWriter::CommitHeader\n");

-       if (fContext == NULL)
+       if (fFormatContext == NULL)
                return B_NO_INIT;

        if (fCodecOpened)
                return B_NOT_ALLOWED;

 #if OPEN_CODEC_CONTEXT
-       for (unsigned i = 0; i < fContext->nb_streams; i++) {
-               AVStream* stream = fContext->streams[i];
+       for (unsigned i = 0; i < fFormatContext->nb_streams; i++) {
+               AVStream* stream = fFormatContext->streams[i];
                // NOTE: Experimental, this should not be needed.
Especially, since
                // we have no idea (in the future) what CodecID some
encoder uses,
                // it may be an encoder from a different plugin.
@@ -476,14 +476,14 @@ AVFormatWriter::CommitHeader()
        // We need to close the codecs we opened, even in case of failure.
        fCodecOpened = true;

-       fHeaderError = avformat_write_header(fContext, NULL);
+       fHeaderError = avformat_write_header(fFormatContext, NULL);
        if (fHeaderError < 0)
                TRACE("  avformat_write_header(): %d\n", fHeaderError);

        #ifdef TRACE_AVFORMAT_WRITER
        TRACE("  wrote header\n");
-       for (unsigned i = 0; i < fContext->nb_streams; i++) {
-               AVStream* stream = fContext->streams[i];
+       for (unsigned i = 0; i < fFormatContext->nb_streams; i++) {
+               AVStream* stream = fFormatContext->streams[i];
                TRACE("  stream[%u] time_base: (%d/%d), codec->time_base:
(%d/%d)\n",
                        i, stream->time_base.num, stream->time_base.den,
                        stream->codec->time_base.num,
stream->codec->time_base.den);
@@ -508,7 +508,7 @@ AVFormatWriter::Close()
 {
        TRACE("AVFormatWriter::Close\n");

-       if (fContext == NULL)
+       if (fFormatContext == NULL)
                return B_NO_INIT;

        if (!fCodecOpened)
@@ -519,7 +519,7 @@ AVFormatWriter::Close()
        if (fHeaderError != 0)
                return B_ERROR;

-       int result = av_write_trailer(fContext);
+       int result = av_write_trailer(fFormatContext);
        if (result < 0)
                TRACE("  av_write_trailer(): %d\n", result);
        return result == 0 ? B_OK : B_ERROR;
@@ -540,7 +540,7 @@ AVFormatWriter::AllocateCookie(void** _cookie,
media_format* format,
        if (_cookie == NULL)
                return B_BAD_VALUE;

-       StreamCookie* cookie = new(std::nothrow) StreamCookie(fContext,
+       StreamCookie* cookie = new(std::nothrow)
StreamCookie(fFormatContext,
                &fStreamLock);

        status_t ret = cookie->Init(format, codecInfo);
diff --git a/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.h
b/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.h
index b720a66c4c..06ef048661 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.h
+++ b/src/add-ons/media/plugins/ffmpeg/AVFormatWriter.h
@@ -51,7 +51,7 @@ private:
 private:
                        class StreamCookie;

-                       AVFormatContext*        fContext;
+                       AVFormatContext*        fFormatContext;
                        bool                            fCodecOpened;
                        int
 fHeaderError;



############################################################################

Commit:      e302fe32cf4b41823a6dc153ce49f2bff70a290b
URL:         https://git.haiku-os.org/haiku/commit/?id=e302fe32cf4b
Author:      Barrett17 <b.vitruvio@xxxxxxxxx>
Date:        Sat Aug  4 09:36:34 2018 UTC

AVCodecDecoder: Rename fContext to fCodecContext


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

diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
index f3a2880369..8eb21e1547 100644
--- a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
+++ b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp
@@ -86,7 +86,7 @@ AVCodecDecoder::AVCodecDecoder()
        fFrame(0),
        fIsAudio(false),
        fCodec(NULL),
-       fContext(avcodec_alloc_context3(NULL)),
+       fCodecContext(avcodec_alloc_context3(NULL)),
        fResampleContext(NULL),
        fDecodedData(NULL),
        fDecodedDataSizeInBytes(0),
@@ -129,9 +129,9 @@ AVCodecDecoder::AVCodec

Other related posts: