From patchwork Sat May 4 13:12:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: swarajhota353@gmail.com X-Patchwork-Id: 12983 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 137D94476B0 for ; Sat, 4 May 2019 16:39:18 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F310168ABBE; Sat, 4 May 2019 16:39:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B55AC68AAD0 for ; Sat, 4 May 2019 16:39:11 +0300 (EEST) Received: by mail-pf1-f193.google.com with SMTP id g3so4347343pfi.4 for ; Sat, 04 May 2019 06:39:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=nn2Zw4I3LiZtxHnSkLRk27cT0Dx2emzpYCMXxG2Pm4M=; b=kxjzSpC79MiCK2/KfYszRqSBBfLZNJ7jmM/jgT2UahK7MniqqH+4yj9p0mY3nrtBrm jjnmGnwa+A2jQDKt8x/aiGbzW21voT3xLtCvZvnjQMLmVgi+23Ks2ImAtgVrFhD37Q7H ZLzaNfsjku3pYPNrhZvHkrPvUw+VQRV0D9hxsRO6A7b4d5PoHNvmGaGd+jXKzrcRy+Bs eXHwVh9fXvLkf6/0obrPq0FMU43mE+Sm2QkLtCV2u0v9LAy0obWkU2JCX0kvOdhudx++ qe44aS3IBBSAcPkZ3MmMEEUIKQt/mqnEKOx+507YzXnYgcWISeYYkDdBs0wDuhlXQhq1 LtBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=nn2Zw4I3LiZtxHnSkLRk27cT0Dx2emzpYCMXxG2Pm4M=; b=LcTV5zQpCTUe75vswIY/N4zjOP7xqCF4T+d0S+4NamUM4TW3D4f8L43o/bQLos+ToQ 6Z9slJGoRyASJiImQsKBy2Ikz3b8Xwdhr0tYHTMDkZ4yJ/oGws3NdKcVgqn4BYHC6JCW AylXXhpQ/toHPq9+qMUm8ykWZiqXhHFic6kNgfn8lgOt8QXMmB/SQfAipe7uxj6BmYiN ALwOWiEc0O8WuVTFPr2s9coeAOqmX56PWz670WVZsdhjpKnSxzxm5GKmtOtBSQBnqOfk YtrKNxcaSq9W1h7zrVeftNEHAO09+9L1bLTdPc4Vfzqt5IUqOgG9ULkUsUgDi6cCtzh5 hL8g== X-Gm-Message-State: APjAAAX2EoojTNh8f6anpQirtvofIGfvNPeUVfwU4ij4bBo12tT5RzYQ RsXQ9lrgioL9vspJjLZWiUVO9Obd X-Google-Smtp-Source: APXvYqwKxYcXoTROpc9oKElc+mVMltVrcJdny9peij0+sBfNqS7B3B1tGCCWyKuMspfwYdSM0xtB9g== X-Received: by 2002:a62:5994:: with SMTP id k20mr19281110pfj.150.1556976680534; Sat, 04 May 2019 06:31:20 -0700 (PDT) Received: from exmachina.localdomain ([106.222.230.255]) by smtp.gmail.com with ESMTPSA id c127sm8801519pfc.132.2019.05.04.06.31.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 May 2019 06:31:19 -0700 (PDT) From: Swaraj Hota To: ffmpeg-devel@ffmpeg.org Date: Sat, 4 May 2019 18:42:40 +0530 Message-Id: <20190504131239.16252-1-swarajhota353@gmail.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avformat/ifv: added support for ifv cctv files 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 Cc: Swaraj Hota Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fixes ticket #2956. Signed-off-by: Swaraj Hota --- Revised patch. Made some minor changes based on original player: - Removed incorrect reading of frame_rate, instead frame rate is kept fixed at 25 (seems like this value is always same). - Added reading of frame width and height from input file. --- Changelog | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/ifv.c | 335 +++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 4 +- 5 files changed, 340 insertions(+), 2 deletions(-) create mode 100644 libavformat/ifv.c diff --git a/Changelog b/Changelog index a3fa0c14a2..03d2a9db01 100644 --- a/Changelog +++ b/Changelog @@ -26,6 +26,7 @@ version : - lscr decoder - lagfun filter - asoftclip filter +- IFV demuxer version 4.1: diff --git a/libavformat/Makefile b/libavformat/Makefile index 99be60d184..f68d41e4a5 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -231,6 +231,7 @@ OBJS-$(CONFIG_ICO_MUXER) += icoenc.o OBJS-$(CONFIG_IDCIN_DEMUXER) += idcin.o OBJS-$(CONFIG_IDF_DEMUXER) += bintext.o sauce.o OBJS-$(CONFIG_IFF_DEMUXER) += iff.o +OBJS-$(CONFIG_IFV_DEMUXER) += ifv.o OBJS-$(CONFIG_ILBC_DEMUXER) += ilbc.o OBJS-$(CONFIG_ILBC_MUXER) += ilbc.o OBJS-$(CONFIG_IMAGE2_DEMUXER) += img2dec.o img2.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index d316a0529a..cd00834807 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -188,6 +188,7 @@ extern AVOutputFormat ff_ico_muxer; extern AVInputFormat ff_idcin_demuxer; extern AVInputFormat ff_idf_demuxer; extern AVInputFormat ff_iff_demuxer; +extern AVInputFormat ff_ifv_demuxer; extern AVInputFormat ff_ilbc_demuxer; extern AVOutputFormat ff_ilbc_muxer; extern AVInputFormat ff_image2_demuxer; diff --git a/libavformat/ifv.c b/libavformat/ifv.c new file mode 100644 index 0000000000..f2d12b4fe7 --- /dev/null +++ b/libavformat/ifv.c @@ -0,0 +1,335 @@ +/* + * IFV demuxer + * + * Copyright (c) 2019 Swaraj Hota + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avformat.h" +#include "internal.h" +#include "avio_internal.h" + +#define IFV_MAGIC "\x11\xd2\xd3\xab\xba\xa9\xcf\x11\ +\x8e\xe6\x00\xc0\x0c\x20\x53\x65\x44" + +typedef struct IFVIndexEntry { + uint32_t frame_offset; + uint32_t frame_size; + enum AVMediaType frame_type; +} IFVIndexEntry; + +typedef struct IFVContext { + IFVIndexEntry *vid_index; + IFVIndexEntry *aud_index; + uint32_t index_pos; + uint32_t total_vframes; + uint32_t total_aframes; + + int width, height; + int is_audio_present; + int sample_rate; + + int video_stream_index; + int audio_stream_index; +} IFVContext; + +static int ifv_probe(const AVProbeData *p) +{ + if (!memcmp(p->buf, IFV_MAGIC, 17)) + return AVPROBE_SCORE_MAX; + return 0; +} + +static int read_next_index_entry(AVFormatContext *s, enum AVMediaType frame_type) +{ + IFVContext *ifv = s->priv_data; + IFVIndexEntry *e; + + if (frame_type == AVMEDIA_TYPE_VIDEO) { + if (ifv->index_pos >= ifv->total_vframes) + return 0; + + e = &ifv->vid_index[ifv->index_pos++]; + + e->frame_offset = avio_rl32(s->pb); + e->frame_size = avio_rl32(s->pb); + e->frame_type = frame_type; + + avio_skip(s->pb, 20); + + } else if (frame_type == AVMEDIA_TYPE_AUDIO) { + if (ifv->index_pos >= ifv->total_aframes) + return 0; + + e = &ifv->aud_index[ifv->index_pos++]; + + e->frame_offset = avio_rl32(s->pb); + e->frame_size = avio_rl32(s->pb); + e->frame_type = frame_type; + + avio_skip(s->pb, 16); + } + + return 1; +} + +static int read_index(AVFormatContext *s, enum AVMediaType frame_type) +{ + IFVContext *ifv = s->priv_data; + int ret; + + if (frame_type == AVMEDIA_TYPE_VIDEO) { + ret = av_reallocp_array(&ifv->vid_index, ifv->total_vframes, sizeof(IFVIndexEntry)); + if (ret < 0) + return ret; + + } else if (frame_type == AVMEDIA_TYPE_AUDIO) { + ret = av_reallocp_array(&ifv->aud_index, ifv->total_aframes, sizeof(IFVIndexEntry)); + if (ret < 0) + return ret; + } + + while (read_next_index_entry(s, frame_type)) + ; + + return 0; +} + +static int parse_header(AVFormatContext *s) +{ + IFVContext *ifv = s->priv_data; + uint32_t aud_magic; + uint32_t vid_magic; + + avio_skip(s->pb, 0x5c); + ifv->width = avio_rl16(s->pb); + ifv->height = avio_rl16(s->pb); + + avio_skip(s->pb, 0x8); + vid_magic = avio_rl32(s->pb); + + if (vid_magic != MKTAG('H','2','6','4')) + avpriv_request_sample(s, "Unknown video codec %x\n", vid_magic); + + avio_skip(s->pb, 0x2c); + ifv->sample_rate = avio_rl32(s->pb); + aud_magic = avio_rl32(s->pb); + + if (aud_magic == MKTAG('G','R','A','W')) { + ifv->is_audio_present = 1; + } else if (aud_magic == MKTAG('P','C','M','U')) { + ifv->is_audio_present = 0; + } else { + avpriv_request_sample(s, "Unknown audio codec %x\n", aud_magic); + } + + avio_skip(s->pb, 0x44); + ifv->total_vframes = avio_rl32(s->pb); + ifv->total_aframes = avio_rl32(s->pb); + + return 0; +} + +static int ifv_read_header(AVFormatContext *s) +{ + IFVContext *ifv = s->priv_data; + AVStream *st; + int ret; + + ret = parse_header(s); + if (ret < 0) + return ret; + + ifv->vid_index = NULL; + ifv->aud_index = NULL; + + /*read video index*/ + avio_seek(s->pb, 0xf8, SEEK_SET); + + ifv->index_pos = 0; + ret = read_index(s, AVMEDIA_TYPE_VIDEO); + if (ret < 0) + return ret; + + if (ifv->is_audio_present) { + /*read audio index*/ + avio_seek(s->pb, 0x14918, SEEK_SET); + + ifv->index_pos = 0; + ret = read_index(s, AVMEDIA_TYPE_AUDIO); + if (ret < 0) + return ret; + } + + + st = avformat_new_stream(s, NULL); + + if (!st) + return AVERROR(ENOMEM); + + st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; + st->codecpar->codec_id = AV_CODEC_ID_H264; + st->codecpar->width = ifv->width; + st->codecpar->height = ifv->height; + st->start_time = 0; + ifv->video_stream_index = st->index; + + avpriv_set_pts_info(st, 32, 1, 25); + + if (ifv->is_audio_present) { + st = avformat_new_stream(s, NULL); + + if (!st) + return AVERROR(ENOMEM); + + st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; + st->codecpar->channels = 1; + st->codecpar->channel_layout = AV_CH_LAYOUT_MONO; + st->codecpar->sample_rate = ifv->sample_rate; + ifv->audio_stream_index = st->index; + } + + avio_skip(s->pb, ifv->vid_index->frame_offset - avio_tell(s->pb)); + + return 0; +} + +static int ifv_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + IFVContext *ifv = s->priv_data; + IFVIndexEntry *e; + uint32_t pos, nb_new_vframes, nb_new_aframes, new_index_start; + int ret, i, j, vdiff, adiff; + + vdiff = adiff = INT_MAX; + + pos = avio_tell(s->pb); + + for (i = 0; i < ifv->total_vframes; i++) { + e = &ifv->vid_index[i]; + if (e->frame_offset >= pos) + break; + } + + vdiff = e->frame_offset - pos; + + if (ifv->is_audio_present && vdiff) { + for (j = 0; j < ifv->total_aframes; j++) { + e = &ifv->aud_index[j]; + if (e->frame_offset >= pos) + break; + } + + adiff = e->frame_offset - pos; + } + + if (vdiff >= 0 && adiff >= 0) { + /*skip to the boundary of next frame if needed*/ + avio_skip(s->pb, FFMIN(vdiff, adiff)); + if (vdiff && adiff) + return 0; + } + + if (i == ifv->total_vframes) { + if (ifv->is_audio_present && j == ifv->total_aframes) { + /*read new video and audio indexes*/ + + avio_skip(s->pb, 0x1c); + nb_new_vframes = avio_rl32(s->pb); + nb_new_aframes = avio_rl32(s->pb); + avio_skip(s->pb, 0xc); + + if (avio_feof(s->pb)) + return AVERROR_EOF; + + ifv->index_pos = new_index_start = ifv->total_vframes; + ifv->total_vframes += nb_new_vframes; + + ret = read_index(s, AVMEDIA_TYPE_VIDEO); + if (ret < 0) + return ret; + + ifv->index_pos = ifv->total_aframes; + ifv->total_aframes += nb_new_aframes; + + ret = read_index(s, AVMEDIA_TYPE_AUDIO); + if (ret < 0) + return ret; + + pos = avio_tell(s->pb); + avio_skip(s->pb, ifv->vid_index[new_index_start].frame_offset - pos); + + return 0; + + } else if (!ifv->is_audio_present) { + /*read new video index*/ + + avio_skip(s->pb, 0x1c); + nb_new_vframes = avio_rl32(s->pb); + avio_skip(s->pb, 0x10); + + if (avio_feof(s->pb)) + return AVERROR_EOF; + + ifv->index_pos = new_index_start = ifv->total_vframes; + ifv->total_vframes += nb_new_vframes; + + ret = read_index(s, AVMEDIA_TYPE_VIDEO); + if (ret < 0) + return ret; + + pos = avio_tell(s->pb); + avio_skip(s->pb, ifv->vid_index[new_index_start].frame_offset - pos); + + return 0; + } + } + + ret = av_get_packet(s->pb, pkt, e->frame_size); + if (ret < 0) + return ret; + + pkt->stream_index = e->frame_type == AVMEDIA_TYPE_VIDEO? + ifv->video_stream_index: ifv->audio_stream_index; + + return ret; +} + +static int ifv_read_close(AVFormatContext *s) +{ + IFVContext *ifv = s->priv_data; + + av_freep(&ifv->vid_index); + if (ifv->is_audio_present) + av_freep(&ifv->aud_index); + + return 0; +} + +AVInputFormat ff_ifv_demuxer = { + .name = "ifv", + .long_name = NULL_IF_CONFIG_SMALL("IFV CCTV DVR"), + .priv_data_size = sizeof(IFVContext), + .extensions = "ifv", + .read_probe = ifv_probe, + .read_header = ifv_read_header, + .read_packet = ifv_read_packet, + .read_close = ifv_read_close, + .flags = AVFMT_NOTIMESTAMPS, +}; diff --git a/libavformat/version.h b/libavformat/version.h index 150a72e27d..52dd95f5c6 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,8 +32,8 @@ // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium) // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 58 -#define LIBAVFORMAT_VERSION_MINOR 27 -#define LIBAVFORMAT_VERSION_MICRO 103 +#define LIBAVFORMAT_VERSION_MINOR 28 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \