libdcp
mpeg2_transcode.cc
1 /*
2  Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
3 
4  This file is part of libdcp.
5 
6  libdcp is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  libdcp is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with libdcp. If not, see <http://www.gnu.org/licenses/>.
18 
19  In addition, as a special exception, the copyright holders give
20  permission to link the code of portions of this program with the
21  OpenSSL library under certain conditions as described in each
22  individual source file, and distribute linked combinations
23  including the two.
24 
25  You must obey the GNU General Public License in all respects
26  for all of the code used other than OpenSSL. If you modify
27  file(s) with this exception, you may extend this exception to your
28  version of the file(s), but you are not obligated to do so. If you
29  do not wish to do so, delete this exception statement from your
30  version. If you delete this exception statement from all source
31  files in the program, then also delete it here.
32 */
33 
34 
35 #include "compose.hpp"
36 #include "exceptions.h"
37 #include "mono_mpeg2_picture_frame.h"
38 #include "mpeg2_transcode.h"
39 #include "scope_guard.h"
40 extern "C" {
41 #include <libavcodec/avcodec.h>
42 }
43 
44 
45 using std::make_shared;
46 using std::shared_ptr;
47 using std::vector;
48 using boost::optional;
49 using namespace dcp;
50 
51 
52 MPEG2Codec::~MPEG2Codec()
53 {
54  avcodec_free_context(&_context);
55 }
56 
57 
58 
59 MPEG2Decompressor::MPEG2Decompressor()
60 {
61  _codec = avcodec_find_decoder_by_name("mpeg2video");
62  if (!_codec) {
63  throw MPEG2CodecError("could not find codec");
64  }
65 
66  _context = avcodec_alloc_context3(_codec);
67  if (!_context) {
68  throw MPEG2CodecError("could not allocate codec context");
69  }
70 
71  int const r = avcodec_open2(_context, _codec, nullptr);
72  if (r < 0) {
73  avcodec_free_context(&_context);
74  throw MPEG2CodecError("could not open codec");
75  }
76 
77  _decompressed_frame = av_frame_alloc();
78  if (!_decompressed_frame) {
79  throw std::bad_alloc();
80  }
81 }
82 
83 
84 MPEG2Decompressor::~MPEG2Decompressor()
85 {
86  av_frame_free(&_decompressed_frame);
87 }
88 
89 
90 vector<FFmpegImage>
91 MPEG2Decompressor::decompress_frame(shared_ptr<const MonoMPEG2PictureFrame> frame)
92 {
93  /* XXX: can we avoid this? */
94  auto copy = av_malloc(frame->size() + AV_INPUT_BUFFER_PADDING_SIZE);
95  if (!copy) {
96  throw std::bad_alloc();
97  }
98  memcpy(copy, frame->data(), frame->size());
99 
100  AVPacket packet;
101  av_init_packet(&packet);
102  av_packet_from_data(&packet, reinterpret_cast<uint8_t*>(copy), frame->size());
103 
104  auto images = decompress_packet(&packet);
105 
106  av_packet_unref(&packet);
107 
108  return images;
109 }
110 
111 
112 vector<FFmpegImage>
113 MPEG2Decompressor::flush()
114 {
115  return decompress_packet(nullptr);
116 }
117 
118 
119 vector<FFmpegImage>
120 MPEG2Decompressor::decompress_packet(AVPacket* packet)
121 {
122  int const r = avcodec_send_packet(_context, packet);
123  if (r < 0) {
124  throw MPEG2DecompressionError(String::compose("avcodec_send_packet failed (%1)", r));
125  }
126 
127  vector<FFmpegImage> images;
128  while (true) {
129  int const r = avcodec_receive_frame(_context, _decompressed_frame);
130  if (r == AVERROR(EAGAIN) || r == AVERROR_EOF) {
131  break;
132  } else if (r < 0) {
133  throw MPEG2DecompressionError("avcodec_receive_frame failed");
134  }
135 
136  auto clone = av_frame_clone(_decompressed_frame);
137  if (!clone) {
138  throw std::bad_alloc();
139  }
140 
141  images.push_back(FFmpegImage(clone));
142  }
143 
144  return images;
145 }
146 
147 
148 MPEG2Compressor::MPEG2Compressor(dcp::Size size, int video_frame_rate, int64_t bit_rate)
149 {
150  _codec = avcodec_find_encoder_by_name("mpeg2video");
151  if (!_codec) {
152  throw MPEG2CodecError("could not find codec");
153  }
154 
155  _context = avcodec_alloc_context3(_codec);
156  if (!_context) {
157  throw MPEG2CodecError("could not allocate codec context");
158  }
159 
160  _context->width = size.width;
161  _context->height = size.height;
162  _context->time_base = AVRational{1, video_frame_rate};
163  _context->pix_fmt = AV_PIX_FMT_YUV420P;
164  _context->bit_rate = bit_rate;
165 
166  int const r = avcodec_open2(_context, _codec, nullptr);
167  if (r < 0) {
168  avcodec_free_context(&_context);
169  throw MPEG2CodecError("could not open codec");
170  }
171 }
172 
173 
174 optional<MPEG2Compressor::IndexedFrame>
175 MPEG2Compressor::send_and_receive(AVFrame const* frame)
176 {
177  int r = avcodec_send_frame(_context, frame);
178  if (r < 0) {
179  throw MPEG2CompressionError(String::compose("avcodec_send_frame failed (%1", r));
180  }
181 
182  auto packet = av_packet_alloc();
183  if (!packet) {
184  throw MPEG2CompressionError("could not allocate packet");
185  }
186 
187  r = avcodec_receive_packet(_context, packet);
188  if (r < 0 && r != AVERROR(EAGAIN)) {
189  throw MPEG2CompressionError(String::compose("avcodec_receive_packet failed (%1)", r));
190  }
191 
192  ScopeGuard sg = [&packet]() {
193  av_packet_free(&packet);
194  };
195 
196  if (packet->size == 0) {
197  return {};
198  }
199 
200  DCP_ASSERT(_context->time_base.num == 1);
201  return IndexedFrame{make_shared<MonoMPEG2PictureFrame>(packet->data, packet->size), std::round(static_cast<double>(packet->pts) / _context->time_base.den)};
202 }
203 
204 
205 optional<MPEG2Compressor::IndexedFrame>
206 MPEG2Compressor::compress_frame(FFmpegImage const& image)
207 {
208  return send_and_receive(image.frame());
209 }
210 
211 
212 optional<MPEG2Compressor::IndexedFrame>
213 MPEG2Compressor::flush()
214 {
215  return send_and_receive(nullptr);
216 }
std::pair< std::shared_ptr< MonoMPEG2PictureFrame >, int64_t > IndexedFrame
Exceptions thrown by libdcp.
Namespace for everything in libdcp.
Definition: array_data.h:50
The integer, two-dimensional size of something.
Definition: types.h:71