xbmc
FFmpegImage.h
1 /*
2  * Copyright (C) 2012-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "iimage.h"
12 #include <memory>
13 
14 extern "C"
15 {
16 #include <libavutil/pixfmt.h>
17 }
18 
19 class Frame
20 {
21 public:
22  friend class CFFmpegImage;
23 
24  Frame() = default;
25  virtual ~Frame();
26 
27  int GetPitch() const { return m_pitch; }
28 
29  unsigned char* m_pImage = nullptr;
30  unsigned int m_delay = 0;
31 
32 private:
33  Frame(const Frame& src);
34 
35  int m_pitch = 0;
36  unsigned int m_imageSize = 0;
37  unsigned int m_height = 0;
38  unsigned int m_width = 0;
39 };
40 
41 
42 struct MemBuffer
43 {
44  uint8_t* data = nullptr;
45  size_t size = 0;
46  size_t pos = 0;
47 };
48 
49 struct AVFrame;
50 struct AVIOContext;
51 struct AVFormatContext;
52 struct AVCodecContext;
53 struct AVPacket;
54 
55 class CFFmpegImage : public IImage
56 {
57 public:
58  explicit CFFmpegImage(const std::string& strMimeType);
59  ~CFFmpegImage() override;
60 
61  bool LoadImageFromMemory(unsigned char* buffer, unsigned int bufSize,
62  unsigned int width, unsigned int height) override;
63  bool Decode(unsigned char * const pixels, unsigned int width, unsigned int height,
64  unsigned int pitch, unsigned int format) override;
65  bool CreateThumbnailFromSurface(unsigned char* bufferin, unsigned int width,
66  unsigned int height, unsigned int format,
67  unsigned int pitch, const std::string& destFile,
68  unsigned char* &bufferout,
69  unsigned int &bufferoutSize) override;
70  void ReleaseThumbnailBuffer() override;
71 
72  bool Initialize(unsigned char* buffer, size_t bufSize);
73 
74  std::shared_ptr<Frame> ReadFrame();
75 
76 private:
77  static void FreeIOCtx(AVIOContext** ioctx);
78  AVFrame* ExtractFrame();
79  bool DecodeFrame(AVFrame* m_pFrame, unsigned int width, unsigned int height, unsigned int pitch, unsigned char * const pixels);
80  static int EncodeFFmpegFrame(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *frame);
81  static int DecodeFFmpegFrame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt);
82  static AVPixelFormat ConvertFormats(AVFrame* frame);
83  std::string m_strMimeType;
84  void CleanupLocalOutputBuffer();
85 
86 
87  MemBuffer m_buf;
88 
89  AVIOContext* m_ioctx = nullptr;
90  AVFormatContext* m_fctx = nullptr;
91  AVCodecContext* m_codec_ctx = nullptr;
92 
93  AVFrame* m_pFrame;
94  uint8_t* m_outputBuffer;
95 };
Definition: FFmpegImage.h:55
Definition: iimage.h:13
Definition: LibInputPointer.h:13
Definition: FFmpegImage.h:19
Definition: FFmpegImage.h:42