From patchwork Fri Nov 18 19:37:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vittorio Giovara X-Patchwork-Id: 1478 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.90.1 with SMTP id o1csp274348vsb; Fri, 18 Nov 2016 11:37:44 -0800 (PST) X-Received: by 10.28.105.78 with SMTP id e75mr72473wmc.55.1479497863918; Fri, 18 Nov 2016 11:37:43 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id az4si8854870wjb.37.2016.11.18.11.37.43; Fri, 18 Nov 2016 11:37:43 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4FF70689A79; Fri, 18 Nov 2016 21:37:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk0-f195.google.com (mail-qk0-f195.google.com [209.85.220.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 23D3668987F for ; Fri, 18 Nov 2016 21:37:33 +0200 (EET) Received: by mail-qk0-f195.google.com with SMTP id h201so34267939qke.3 for ; Fri, 18 Nov 2016 11:37:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references; bh=NuPmwbfgHmQndOaIG8PEtyIny9E5wFkyHrBdTtDqCko=; b=Xg56q71ex+N0L6U+Vmd5SxbMVwQ4fhgLeH8v6yURoF6gsAnq0k1FZWWVzAqKHisJN+ UCHAkysr27WhFdQ6CSpxfbDRYvhwQymvO9YXjmtyybpiK3ds7nz+9J/+LhCTjxnPmsH1 EeJNfFL5tkn+MsaXDN7A3mSPhZbFxjV9q1s3kVd65+gXVCKYD57P4nTQkLvPq6MmrLEL rWcNnL2YXKxtvWAO4sD1Tm0JfFJy7wN3PHx+7A2DRA60lMEYzPbkzL0v79vnTd/h3m// 5mhC0Oj5a19m53fR/XTYXa4o0bvN6lWK+M5ujpYXYK616kqxX2kbYRcP3/1SEhUp43fV q/zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=NuPmwbfgHmQndOaIG8PEtyIny9E5wFkyHrBdTtDqCko=; b=mquYOlJw94nfvBGcGZZtargC8UJGOWyltTpmh3a+8mvZ6wHR4vadpmRuAJoQ5OpZcS eKuwPdETdimGJUZesHr1J+eyqCVztbCF2PFXOEpDZ8Tvgz7bpmal/45GcJ6+hqWICSkH V5IU5nzqLdgwqzu5+/WUqRuacYeUzRftBT7gjERdqtwuFBKyrJcBHVGQ45hzMSlXnFSc bwpnS+9Oz4NnW3gT/3D6ZxAcQEhy9ECpv8i0KtNbQLbzc95ba9P4EoZLGt/RHUMDmsZq y5lDC5sUOITjdcziKXobdq3Tb73y1TnU34m4xWG/pYdqswt44pGKUKGQInfaljs4JJlJ O2Tw== X-Gm-Message-State: AKaTC03T2POhTzqh9skhRaHHHQ5e88c5Iynm0rFcO5xVb0QLmjMOQzmLH5NTaij8s03gGA== X-Received: by 10.55.19.97 with SMTP id d94mr2034880qkh.200.1479497853258; Fri, 18 Nov 2016 11:37:33 -0800 (PST) Received: from vimacbookpro.vimeows.com (nyv-exweb.iac.com. [216.112.252.10]) by smtp.gmail.com with ESMTPSA id 16sm4761344qtt.38.2016.11.18.11.37.32 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 18 Nov 2016 11:37:32 -0800 (PST) From: Vittorio Giovara To: ffmpeg-devel@ffmpeg.org Date: Fri, 18 Nov 2016 14:37:29 -0500 Message-Id: <20161118193731.35334-1-vittorio.giovara@gmail.com> X-Mailer: git-send-email 2.10.0 In-Reply-To: References: Subject: [FFmpeg-devel] [PATCH 1/3] mov: Evaluate the movie display matrix X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This matrix needs to be applied after all others have (currently only display matrix from trak), but cannot be handled in movie box, since streams are not allocated yet. So store it in main context, and apply it when appropriate, that is after parsing the tkhd one. Signed-off-by: Vittorio Giovara --- No changes, except I moved tests to a separate commit. Please CC. Vittorio libavformat/isom.h | 1 + libavformat/mov.c | 48 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/libavformat/isom.h b/libavformat/isom.h index d684502..02bfedd 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -240,6 +240,7 @@ typedef struct MOVContext { uint8_t *decryption_key; int decryption_key_len; int enable_drefs; + int32_t movie_display_matrix[3][3]; ///< display matrix from mvhd } MOVContext; int ff_mp4_read_descr_len(AVIOContext *pb); diff --git a/libavformat/mov.c b/libavformat/mov.c index 8d6cc12..b7d0b12 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -1239,6 +1239,7 @@ static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) { + int i; int64_t creation_time; int version = avio_r8(pb); /* version */ avio_rb24(pb); /* flags */ @@ -1269,7 +1270,12 @@ static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) avio_skip(pb, 10); /* reserved */ - avio_skip(pb, 36); /* display matrix */ + /* movie display matrix, store it in main context and use it later on */ + for (i = 0; i < 3; i++) { + c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point + c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point + c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point + } avio_rb32(pb); /* preview time */ avio_rb32(pb); /* preview duration */ @@ -3847,12 +3853,22 @@ static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +// return 1 when matrix is identity, 0 otherwise +#define IS_MATRIX_IDENT(matrix) \ + ( (matrix)[0][0] == (1 << 16) && \ + (matrix)[1][1] == (1 << 16) && \ + (matrix)[2][2] == (1 << 30) && \ + !(matrix)[0][1] && !(matrix)[0][2] && \ + !(matrix)[1][0] && !(matrix)[1][2] && \ + !(matrix)[2][0] && !(matrix)[2][1]) + static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) { - int i; + int i, j, e; int width; int height; int display_matrix[3][3]; + int res_display_matrix[3][3] = { { 0 } }; AVStream *st; MOVStreamContext *sc; int version; @@ -3902,15 +3918,20 @@ static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) sc->width = width >> 16; sc->height = height >> 16; - // save the matrix and add rotate metadata when it is not the default - // identity - if (display_matrix[0][0] != (1 << 16) || - display_matrix[1][1] != (1 << 16) || - display_matrix[2][2] != (1 << 30) || - display_matrix[0][1] || display_matrix[0][2] || - display_matrix[1][0] || display_matrix[1][2] || - display_matrix[2][0] || display_matrix[2][1]) { - int i, j; + // apply the moov display matrix (after the tkhd one) + for (i = 0; i < 3; i++) { + const int sh[3] = { 16, 16, 30 }; + for (j = 0; j < 3; j++) { + for (e = 0; e < 3; e++) { + res_display_matrix[i][j] += + ((int64_t) display_matrix[i][e] * + c->movie_display_matrix[e][j]) >> sh[e]; + } + } + } + + // save the matrix when it is not the default identity + if (!IS_MATRIX_IDENT(res_display_matrix)) { double rotate; av_freep(&sc->display_matrix); @@ -3920,7 +3941,7 @@ static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) - sc->display_matrix[i * 3 + j] = display_matrix[i][j]; + sc->display_matrix[i * 3 + j] = res_display_matrix[i][j]; rotate = av_display_rotation_get(sc->display_matrix); if (!isnan(rotate)) { @@ -3939,7 +3960,8 @@ static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) double disp_transform[2]; for (i = 0; i < 2; i++) - disp_transform[i] = hypot(display_matrix[i][0], display_matrix[i][1]); + disp_transform[i] = hypot(sc->display_matrix[0 + i], + sc->display_matrix[3 + i]); if (disp_transform[0] > 0 && disp_transform[1] > 0 && disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&