Skip to content

Commit 665a288

Browse files
comdev1337john-preston
authored andcommitted
fix: embed portrait 4K
1 parent c967a78 commit 665a288

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

Telegram/SourceFiles/ffmpeg/ffmpeg_utility.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ constexpr auto kImageFormat = QImage::Format_ARGB32_Premultiplied;
4747
constexpr auto kMaxScaleByAspectRatio = 16;
4848
constexpr auto kAvioBlockSize = 4096;
4949
constexpr auto kMaxFrameStorageBytes = 64 * 1024 * 1024;
50+
constexpr auto kMaxPixelsPaddingRatio = 32;
51+
constexpr auto kMaxPixelsFixedPadding = 64 * 1024;
5052
constexpr auto kTimeUnknown = std::numeric_limits<crl::time>::min();
5153
constexpr auto kDurationMax = crl::time(std::numeric_limits<int>::max());
5254

@@ -417,6 +419,12 @@ const AVCodec *FindDecoder(not_null<AVCodecContext*> context) {
417419
: avcodec_find_decoder(context->codec_id);
418420
}
419421

422+
int64_t MaxPixelsForAreaLimit(int64_t area) {
423+
// Decoders may check internally padded frame dimensions against max_pixels,
424+
// not just the visible frame size. Leave room for alignment/cropping slack.
425+
return area + area / kMaxPixelsPaddingRatio + kMaxPixelsFixedPadding;
426+
}
427+
420428
CodecPointer MakeCodecPointer(CodecDescriptor descriptor) {
421429
auto error = AvErrorWrap();
422430

@@ -435,7 +443,8 @@ CodecPointer MakeCodecPointer(CodecDescriptor descriptor) {
435443
context->pkt_timebase = stream->time_base;
436444
if ((descriptor.videoMaxArea > 0)
437445
&& (context->codec_type == AVMEDIA_TYPE_VIDEO)) {
438-
context->max_pixels = descriptor.videoMaxArea;
446+
context->max_pixels = MaxPixelsForAreaLimit(
447+
descriptor.videoMaxArea);
439448
}
440449
av_opt_set(context, "threads", "auto", 0);
441450
av_opt_set_int(context, "refcounted_frames", 1, 0);

Telegram/SourceFiles/ffmpeg/ffmpeg_utility.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ void LogError(
214214
const QString &details = {});
215215

216216
[[nodiscard]] const AVCodec *FindDecoder(not_null<AVCodecContext*> context);
217+
[[nodiscard]] int64_t MaxPixelsForAreaLimit(int64_t area);
217218
[[nodiscard]] crl::time PtsToTime(int64_t pts, AVRational timeBase);
218219
// Used for full duration conversion.
219220
[[nodiscard]] crl::time PtsToTimeCeil(int64_t pts, AVRational timeBase);

Telegram/SourceFiles/media/clip/media_clip_ffmpeg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ bool FFMpegReaderImplementation::start(Mode mode, crl::time &positionMs) {
329329
const auto audioStreamId = av_find_best_stream(_fmtContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
330330
_hasAudioStream = (audioStreamId >= 0);
331331
}
332-
_codecContext->max_pixels = MaxAreaForMode(_mode);
332+
_codecContext->max_pixels = FFmpeg::MaxPixelsForAreaLimit(
333+
MaxAreaForMode(_mode));
333334

334335
if ((res = avcodec_open2(_codecContext, codec, nullptr)) < 0) {
335336
LOG(("Gif Error: Unable to avcodec_open2 %1, error %2, %3").arg(logData()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));

0 commit comments

Comments
 (0)