From df49f7938e10664ead4e809a8c15408c8dea9ac7 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Fri, 10 Apr 2009 18:00:34 -0500
Subject: mythtv: apply patch for lirc delays
 http://svn.mythtv.org/trac/ticket/6406 ref FS#318

---
 abs/core-testing/mythtv/stable/mythtv/PKGBUILD     |  7 ++-
 abs/core-testing/mythtv/stable/mythtv/mpegpspatch  | 66 ++++++++++++++++++++++
 abs/extra-testing/community/mythtv-vdpau/PKGBUILD  |  7 ++-
 .../community/mythtv-vdpau/mpegpspatch             | 66 ++++++++++++++++++++++
 4 files changed, 142 insertions(+), 4 deletions(-)
 create mode 100644 abs/core-testing/mythtv/stable/mythtv/mpegpspatch
 create mode 100644 abs/extra-testing/community/mythtv-vdpau/mpegpspatch

diff --git a/abs/core-testing/mythtv/stable/mythtv/PKGBUILD b/abs/core-testing/mythtv/stable/mythtv/PKGBUILD
index dc86f28..b638c61 100644
--- a/abs/core-testing/mythtv/stable/mythtv/PKGBUILD
+++ b/abs/core-testing/mythtv/stable/mythtv/PKGBUILD
@@ -1,13 +1,13 @@
 pkgname=mythtv
 pkgver=0.21
-pkgrel=58
+pkgrel=59
 pkgdesc="A Homebrew PVR project"
 arch=('i686' 'x86_64')
 depends=('bash' 'mysql-clients>=5.0' 'qt3' 'lame' 'lirc-utils' 'ffmpeg' \
 	'libxvmc' 'libavc1394' 'libdc1394' 'libiec61883')
 backup=(etc/mythtv/mythbackend.conf etc/mythtv/mysql.txt usr/share/mythtv/is.xmlusr/share/mythtv/media_settings.xml ) 
 source=(ftp://ftp.knoppmyth.net/R6/sources/${pkgname}-${pkgver}-fixes.tar.bz2 \
-	mythbackend myththemedmenu.cpp.patch myththemedmenu.h.patch  smolt_jump.patch pretty gnome_screensaver.patch)
+	mythbackend myththemedmenu.cpp.patch myththemedmenu.h.patch  smolt_jump.patch pretty gnome_screensaver.patch mpegpspatch)
 
 #md5sums=('e316ed18d7ac837cf8c4af54b1478793' '7ef6de58240e7aad389a0b13d91b1cf6'\
 #         'a0ecb7f476cb71c0c1ac90d349fc7695')
@@ -30,6 +30,9 @@ build() {
 	 patch -p0 < ../smolt_jump.patch
 #	 patch -p0 < ../2.6.28_dvb_api_version.diff
 	 patch -p0 < ../gnome_screensaver.patch
+	 cd libs/libavformat
+	 patch -p0 < $startdir/src/mpegpspatch
+	 cd -
    . /etc/profile.d/qt3.sh
 
    # use QT3 qmake
diff --git a/abs/core-testing/mythtv/stable/mythtv/mpegpspatch b/abs/core-testing/mythtv/stable/mythtv/mpegpspatch
new file mode 100644
index 0000000..eed08eb
--- /dev/null
+++ b/abs/core-testing/mythtv/stable/mythtv/mpegpspatch
@@ -0,0 +1,66 @@
+--- orig_mpeg.c	2009-03-23 11:26:22.000000000 -0700
++++ mpeg.c	2009-03-23 11:28:09.000000000 -0700
+@@ -66,35 +66,40 @@
+ 
+ static int mpegps_probe(AVProbeData *p)
+ {
++    uint32_t code= -1;
++    int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
+     int i;
+-    int size= FFMIN(2048, p->buf_size);
+-    uint32_t code=0xFF;
++    int score=0;
+ 
+-    /* we search the first start code. If it is a packet start code,
+-       then we decide it is mpeg ps. We do not send highest value to
+-       give a chance to mpegts */
+-    /* NOTE: the search range was restricted to avoid too many false
+-       detections */
+-
+-    for (i = 0; i < size; i++) {
+-        code = (code << 8) | p->buf[i];
++    for(i=0; i<p->buf_size; i++){
++        code = (code<<8) + p->buf[i];
+         if ((code & 0xffffff00) == 0x100) {
+-            if (code == PACK_START_CODE ||
+-                code == SYSTEM_HEADER_START_CODE ||
+-                (code >= 0x1e0 && code <= 0x1ef) ||
+-                (code >= 0x1c0 && code <= 0x1df) ||
+-                code == PRIVATE_STREAM_2 ||
+-                code == PROGRAM_STREAM_MAP ||
+-                code == PRIVATE_STREAM_1 ||
+-                code == PADDING_STREAM ||
+-                code >= 0x100 && code <= 0x1b0)
+-                return AVPROBE_SCORE_MAX - 2;
+-            else
+-                return 0;
++            int pes= check_pes(p->buf+i, p->buf+p->buf_size);
++
++            if(code == SYSTEM_HEADER_START_CODE) sys++;
++            else if(code == PRIVATE_STREAM_1)    priv1++;
++            else if(code == PACK_START_CODE)     pspack++;
++            else if((code & 0xf0) == VIDEO_ID &&  pes) vid++;
++            else if((code & 0xe0) == AUDIO_ID &&  pes) audio++;
++
++            else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
++            else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
+         }
+     }
+ 
+-    return 0;
++    if(vid+audio > invalid)     /* invalid VDR files nd short PES streams */
++        score= AVPROBE_SCORE_MAX/4;
++
++//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, p->buf_size);
++    if(sys>invalid && sys*9 <= pspack*10)
++        return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
++    if(priv1 + vid + audio > invalid && (priv1+vid+audio)*9 <= pspack*10)
++        return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
++    if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048) /* PES stream */
++        return AVPROBE_SCORE_MAX/2+2;
++
++    //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
++    return score;
+ }
+ 
+ typedef struct MpegDemuxContext {
diff --git a/abs/extra-testing/community/mythtv-vdpau/PKGBUILD b/abs/extra-testing/community/mythtv-vdpau/PKGBUILD
index e89a93b..59ef863 100644
--- a/abs/extra-testing/community/mythtv-vdpau/PKGBUILD
+++ b/abs/extra-testing/community/mythtv-vdpau/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=mythtv-vdpau
 pkgver=0.21
-pkgrel=9
+pkgrel=12
 pkgdesc="A Homebrew PVR project"
 arch=('i686' 'x86_64')
 depends=('bash' 'mysql-clients>=5.0' 'qt3' 'lame' 'lirc-utils' 'ffmpeg' \
@@ -9,7 +9,7 @@ backup=(etc/mythtv/mythbackend.conf etc/mythtv/mysql.txt usr/share/mythtv/is.xml
 source=(ftp://ftp.knoppmyth.net/R6/sources/mythtv-${pkgver}-fixes.tar.bz2 \
 	mythbackend myththemedmenu.cpp.patch myththemedmenu.h.patch  \
 	smolt_jump.patch pretty \
-	http://www.avenard.org/files/media/vdpau/mythtv-fixes-glvdpau20300.patch.gz)
+	http://www.avenard.org/files/media/vdpau/mythtv-fixes-glvdpau20300.patch.gz mpegpspatch)
 
 #md5sums=('e316ed18d7ac837cf8c4af54b1478793' '7ef6de58240e7aad389a0b13d91b1cf6'\
 #         'a0ecb7f476cb71c0c1ac90d349fc7695')
@@ -33,6 +33,9 @@ build() {
 	 patch -p0 < ../smolt_jump.patch
 #	 patch -p0 < ../2.6.28_dvb_api_version.diff
        	patch -p1 < ../mythtv-fixes-glvdpau20300.patch
+        cd libs/libavformat
+        patch -p0 < $startdir/src/mpegpspatch
+        cd -
 
    . /etc/profile.d/qt3.sh
 
diff --git a/abs/extra-testing/community/mythtv-vdpau/mpegpspatch b/abs/extra-testing/community/mythtv-vdpau/mpegpspatch
new file mode 100644
index 0000000..eed08eb
--- /dev/null
+++ b/abs/extra-testing/community/mythtv-vdpau/mpegpspatch
@@ -0,0 +1,66 @@
+--- orig_mpeg.c	2009-03-23 11:26:22.000000000 -0700
++++ mpeg.c	2009-03-23 11:28:09.000000000 -0700
+@@ -66,35 +66,40 @@
+ 
+ static int mpegps_probe(AVProbeData *p)
+ {
++    uint32_t code= -1;
++    int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
+     int i;
+-    int size= FFMIN(2048, p->buf_size);
+-    uint32_t code=0xFF;
++    int score=0;
+ 
+-    /* we search the first start code. If it is a packet start code,
+-       then we decide it is mpeg ps. We do not send highest value to
+-       give a chance to mpegts */
+-    /* NOTE: the search range was restricted to avoid too many false
+-       detections */
+-
+-    for (i = 0; i < size; i++) {
+-        code = (code << 8) | p->buf[i];
++    for(i=0; i<p->buf_size; i++){
++        code = (code<<8) + p->buf[i];
+         if ((code & 0xffffff00) == 0x100) {
+-            if (code == PACK_START_CODE ||
+-                code == SYSTEM_HEADER_START_CODE ||
+-                (code >= 0x1e0 && code <= 0x1ef) ||
+-                (code >= 0x1c0 && code <= 0x1df) ||
+-                code == PRIVATE_STREAM_2 ||
+-                code == PROGRAM_STREAM_MAP ||
+-                code == PRIVATE_STREAM_1 ||
+-                code == PADDING_STREAM ||
+-                code >= 0x100 && code <= 0x1b0)
+-                return AVPROBE_SCORE_MAX - 2;
+-            else
+-                return 0;
++            int pes= check_pes(p->buf+i, p->buf+p->buf_size);
++
++            if(code == SYSTEM_HEADER_START_CODE) sys++;
++            else if(code == PRIVATE_STREAM_1)    priv1++;
++            else if(code == PACK_START_CODE)     pspack++;
++            else if((code & 0xf0) == VIDEO_ID &&  pes) vid++;
++            else if((code & 0xe0) == AUDIO_ID &&  pes) audio++;
++
++            else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
++            else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
+         }
+     }
+ 
+-    return 0;
++    if(vid+audio > invalid)     /* invalid VDR files nd short PES streams */
++        score= AVPROBE_SCORE_MAX/4;
++
++//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, p->buf_size);
++    if(sys>invalid && sys*9 <= pspack*10)
++        return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
++    if(priv1 + vid + audio > invalid && (priv1+vid+audio)*9 <= pspack*10)
++        return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
++    if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048) /* PES stream */
++        return AVPROBE_SCORE_MAX/2+2;
++
++    //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
++    return score;
+ }
+ 
+ typedef struct MpegDemuxContext {
-- 
cgit v0.12