filter.cpp 41.2 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3
/*****************************************************************************
 * filter.c : DirectShow access module for vlc
 *****************************************************************************
4
 * Copyright (C) 2002 the VideoLAN team
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6
 *
7
 * Author: Gildas Bazin <gbazin@videolan.org>
Gildas Bazin's avatar
 
Gildas Bazin committed
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
 
Gildas Bazin committed
22 23 24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdio.h>

#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
30
#include <vlc_vout.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
31

32 33 34 35 36
#ifndef _MSC_VER
    /* Work-around a bug in w32api-2.5 */
#   define QACONTAINERFLAGS QACONTAINERFLAGS_SOMETHINGELSE
#endif

Gildas Bazin's avatar
Gildas Bazin committed
37
#include "common.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
38 39 40 41
#include "filter.h"

#define DEBUG_DSHOW 1

42 43 44
#define FILTER_NAME  L"VideoLAN Capture Filter"
#define PIN_NAME     L"Capture"

Gildas Bazin's avatar
 
Gildas Bazin committed
45 46
/*****************************************************************************
 * DirectShow GUIDs.
47
 * Easier to define them here as mingw doesn't provide them all.
Gildas Bazin's avatar
 
Gildas Bazin committed
48 49 50
 *****************************************************************************/
const GUID CLSID_SystemDeviceEnum = {0x62be5d10, 0x60eb, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
const GUID CLSID_VideoInputDeviceCategory = {0x860BB310,0x5D01,0x11d0,{0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86}};
Gildas Bazin's avatar
 
Gildas Bazin committed
51
const GUID CLSID_AudioInputDeviceCategory = {0x33d9a762, 0x90c8, 0x11d0, {0xbd, 0x43, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
52 53
//const GUID IID_IPropertyBag = {0x55272A00, 0x42CB, 0x11CE, {0x81, 0x35, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51}};
extern const GUID IID_IPropertyBag;
Gildas Bazin's avatar
 
Gildas Bazin committed
54 55 56 57 58
const GUID IID_ICreateDevEnum = {0x29840822, 0x5b84, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
const GUID IID_IFilterGraph = {0x56a8689f, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID IID_IMediaControl = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID CLSID_FilterGraph = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};

59 60 61 62
//const GUID IID_IUnknown = {0x00000000, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
extern const GUID IID_IUnknown;
//const GUID IID_IPersist = {0x0000010c, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
extern const GUID IID_IPersist;
Gildas Bazin's avatar
 
Gildas Bazin committed
63 64 65 66
const GUID IID_IMediaFilter = {0x56a86899, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
const GUID IID_IBaseFilter = {0x56a86895, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID IID_IPin = {0x56a86891, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
const GUID IID_IMemInputPin = {0x56a8689d, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
67
extern const GUID IID_IMemInputPin;
Gildas Bazin's avatar
 
Gildas Bazin committed
68 69 70 71

const GUID IID_IEnumPins = {0x56a86892, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
const GUID IID_IEnumMediaTypes = {0x89c31040, 0x846b, 0x11ce, {0x97,0xd3, 0x00,0xaa,0x00,0x55,0x59,0x5a}};

72 73
const GUID IID_IAMBufferNegotiation = {0x56ed71a0, 0xaf5f, 0x11d0, {0xb3, 0xf0, 0x00, 0xaa, 0x00, 0x37, 0x61, 0xc5}};

74 75
//const GUID IID_ISpecifyPropertyPages = {0xb196b28b, 0xbab4, 0x101a, {0xb6, 0x9c, 0x00, 0xaa, 0x00, 0x34, 0x1d, 0x07}};
extern const GUID IID_ISpecifyPropertyPages;
Gildas Bazin's avatar
 
Gildas Bazin committed
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
const GUID IID_IQualityControl = {0x56a868a5, 0x0ad4, 0x11ce, {0xb, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};

const GUID CLSID_CaptureGraphBuilder2 = {0xBF87B6E1, 0x8C27, 0x11d0, {0xB3, 0xF0, 0x0, 0xAA, 0x00, 0x37, 0x61, 0xC5}};

const GUID IID_IGraphBuilder = {0x56a868a9, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};

const GUID IID_ICaptureGraphBuilder2 = {0x93E5A4E0, 0x2D50, 0x11d2, {0xAB, 0xFA, 0x00, 0xA0, 0xC9, 0xC6, 0xE3, 0x8D}};

const GUID IID_IAMTVAudio = {0x83EC1C30, 0x23D1, 0x11d1, {0x99, 0xE6, 0x00, 0xA0, 0xC9, 0x56, 0x02, 0x66}};
const GUID IID_IAMStreamConfig = {0xC6E13340, 0x30AC, 0x11d0, {0xA1, 0x8C, 0x00, 0xA0, 0xC9, 0x11, 0x89, 0x56}};
const GUID IID_IAMCrossbar = {0xC6E13380, 0x30AC, 0x11d0, {0xA1, 0x8C, 0x00, 0xA0, 0xC9, 0x11, 0x89, 0x56}};
const GUID IID_IAMTVTuner = {0x211A8766, 0x03AC, 0x11d1, {0x8D, 0x13, 0x00, 0xAA, 0x00, 0xBD, 0x83, 0x39}};

const GUID IID_IKsPropertySet = {0x31EFAC30, 0x515C, 0x11d0, {0xA9, 0xAA, 0x00, 0xAA, 0x00, 0x61, 0xBE, 0x93}};

92
/* Video Format */
93

94
const GUID FORMAT_VideoInfo  = {0x05589f80, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
Gildas Bazin's avatar
 
Gildas Bazin committed
95 96 97 98
/*
 * MEDIATYPEs and MEDIASUBTYPEs
 */
const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
99
const GUID MEDIATYPE_Interleaved = {0x73766169, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
Gildas Bazin's avatar
 
Gildas Bazin committed
100
const GUID MEDIATYPE_Stream = {0xe436eb83, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
101
const GUID MEDIASUBTYPE_PREVIEW_VIDEO = {0x2859e1da, 0xb81f, 0x4fbd, {0x94, 0x3b, 0xe2, 0x37, 0x24, 0xa1, 0xab, 0xb3}};
Gildas Bazin's avatar
 
Gildas Bazin committed
102

Gildas Bazin's avatar
 
Gildas Bazin committed
103 104 105
/* Packed RGB formats */
const GUID MEDIASUBTYPE_RGB1 = {0xe436eb78, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID MEDIASUBTYPE_RGB4 = {0xe436eb79, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
Gildas Bazin's avatar
 
Gildas Bazin committed
106 107 108 109 110 111 112
const GUID MEDIASUBTYPE_RGB8 = {0xe436eb7a, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID MEDIASUBTYPE_RGB555 = {0xe436eb7c, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID MEDIASUBTYPE_RGB32 = {0xe436eb7e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID MEDIASUBTYPE_ARGB32 = {0x773c9ac0, 0x3274, 0x11d0, {0xb7, 0x24, 0x0, 0xaa, 0x0, 0x6c, 0x1a, 0x1}};

Gildas Bazin's avatar
 
Gildas Bazin committed
113
/* Packed YUV formats */
Gildas Bazin's avatar
 
Gildas Bazin committed
114 115
const GUID MEDIASUBTYPE_YUYV = {0x56595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_Y411 = {0x31313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
Gildas Bazin's avatar
 
Gildas Bazin committed
116
const GUID MEDIASUBTYPE_Y211 = {0x31313259, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
Gildas Bazin's avatar
 
Gildas Bazin committed
117 118 119
const GUID MEDIASUBTYPE_YUY2 = {0x32595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_YVYU = {0x55595659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_UYVY = {0x59565955, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
Gildas Bazin's avatar
 
Gildas Bazin committed
120 121 122

/* Planar YUV formats */
const GUID MEDIASUBTYPE_YVU9 = {0x39555659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
Gildas Bazin's avatar
 
Gildas Bazin committed
123
const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
Gildas Bazin's avatar
 
Gildas Bazin committed
124 125 126
const GUID MEDIASUBTYPE_IYUV = {0x56555949, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}}; /* identical to YV12 */
const GUID MEDIASUBTYPE_Y41P = {0x50313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
Gildas Bazin's avatar
 
Gildas Bazin committed
127

Gildas Bazin's avatar
 
Gildas Bazin committed
128 129 130
const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
131
const GUID MEDIASUBTYPE_IEEE_FLOAT = {0x00000003, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
Gildas Bazin's avatar
 
Gildas Bazin committed
132

Gildas Bazin's avatar
 
Gildas Bazin committed
133 134 135 136 137
/* DV formats */
const GUID MEDIASUBTYPE_dvsd = {0x64737664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_dvhd = {0x64687664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_dvsl = {0x6c737664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};

Gildas Bazin's avatar
 
Gildas Bazin committed
138 139 140 141 142 143
/* MPEG2 formats */
const GUID MEDIASUBTYPE_MPEG2_VIDEO = {0xe06d8026, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
const GUID MEDIASUBTYPE_MPEG2_PROGRAM = {0xe06d8022, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
const GUID MEDIASUBTYPE_MPEG2_TRANSPORT = {0xe06d8023, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
const GUID FORMAT_MPEG2Video = {0xe06d80e3, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};

144 145 146
/* MJPG format */
const GUID MEDIASUBTYPE_MJPG = {0x47504A4D, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};

147 148 149
/* DivX formats */
const GUID MEDIASUBTYPE_DIVX = {0x58564944, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/* Analog Video */
const GUID FORMAT_AnalogVideo = {0x482dde0, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};

const GUID MEDIATYPE_AnalogVideo = {0x482dde1, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xab, 0x0, 0x6e, 0xcb, 0x65}};

const GUID MEDIASUBTYPE_AnalogVideo_NTSC_M = {0x482dde2, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_PAL_B = {0x482dde5, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_PAL_D = {0x482dde6, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_PAL_G = {0x482dde7, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_PAL_H = {0x482dde8, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_PAL_I = {0x482dde9, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_PAL_M = {0x482ddea, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_PAL_N = {0x482ddeb, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_PAL_N_COMBO = {0x482ddec, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_SECAM_B = {0x482ddf0, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_SECAM_D = {0x482ddf1, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_SECAM_G = {0x482ddf2, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_SECAM_H = {0x482ddf3, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_SECAM_K = {0x482ddf4, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_SECAM_K1 = {0x482ddf5, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
const GUID MEDIASUBTYPE_AnalogVideo_SECAM_L = {0x482ddf6, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};

const GUID AMPROPSETID_Pin= {0x9b00f101, 0x1567, 0x11d1, {0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5}};
const GUID PIN_CATEGORY_ANALOGVIDEOIN= {0xfb6c4283, 0x0353, 0x11d1, {0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba}};
const GUID PIN_CATEGORY_CAPTURE= {0xfb6c4281, 0x0353, 0x11d1, {0x90, 0x5f, 0x0, 0x0, 0xc0, 0xcc, 0x16, 0xba}};
const GUID LOOK_UPSTREAM_ONLY= {0xac798be0, 0x98e3, 0x11d1, {0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc}};

177 178
//const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
extern const GUID GUID_NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
179

Gildas Bazin's avatar
 
Gildas Bazin committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
void WINAPI FreeMediaType( AM_MEDIA_TYPE& mt )
{
    if( mt.cbFormat != 0 )
    {
        CoTaskMemFree( (PVOID)mt.pbFormat );
        mt.cbFormat = 0;
        mt.pbFormat = NULL;
    }
    if( mt.pUnk != NULL )
    {
        mt.pUnk->Release();
        mt.pUnk = NULL;
    }
}

HRESULT WINAPI CopyMediaType( AM_MEDIA_TYPE *pmtTarget,
                              const AM_MEDIA_TYPE *pmtSource )
{
    *pmtTarget = *pmtSource;
199 200 201 202

    if( !pmtSource || !pmtTarget ) return S_FALSE;

    if( pmtSource->cbFormat && pmtSource->pbFormat )
Gildas Bazin's avatar
 
Gildas Bazin committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    {
        pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc( pmtSource->cbFormat );
        if( pmtTarget->pbFormat == NULL )
        {
            pmtTarget->cbFormat = 0;
            return E_OUTOFMEMORY;
        }
        else
        {
            CopyMemory( (PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
                        pmtTarget->cbFormat );
        }
    }
    if( pmtTarget->pUnk != NULL )
    {
        pmtTarget->pUnk->AddRef();
    }

    return S_OK;
}

Gildas Bazin's avatar
Gildas Bazin committed
224
int GetFourCCFromMediaType( const AM_MEDIA_TYPE &media_type )
225 226 227 228 229
{
    int i_fourcc = 0;

    if( media_type.majortype == MEDIATYPE_Video )
    {
230
        /* currently only support this type of video info format */
231
        if( 1 /* media_type.formattype == FORMAT_VideoInfo */ )
Gildas Bazin's avatar
Gildas Bazin committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        {
            /* Packed RGB formats */
            if( media_type.subtype == MEDIASUBTYPE_RGB1 )
               i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
            else if( media_type.subtype == MEDIASUBTYPE_RGB4 )
               i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
            else if( media_type.subtype == MEDIASUBTYPE_RGB8 )
               i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
            else if( media_type.subtype == MEDIASUBTYPE_RGB555 )
               i_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
            else if( media_type.subtype == MEDIASUBTYPE_RGB565 )
               i_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
            else if( media_type.subtype == MEDIASUBTYPE_RGB24 )
               i_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
            else if( media_type.subtype == MEDIASUBTYPE_RGB32 )
               i_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
            else if( media_type.subtype == MEDIASUBTYPE_ARGB32 )
               i_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );

            /* Planar YUV formats */
            else if( media_type.subtype == MEDIASUBTYPE_I420 )
               i_fourcc = VLC_FOURCC( 'I', '4', '2', '0' );
            else if( media_type.subtype == MEDIASUBTYPE_Y41P )
               i_fourcc = VLC_FOURCC( 'I', '4', '1', '1' );
            else if( media_type.subtype == MEDIASUBTYPE_YV12 )
               i_fourcc = VLC_FOURCC( 'Y', 'V', '1', '2' );
            else if( media_type.subtype == MEDIASUBTYPE_IYUV )
               i_fourcc = VLC_FOURCC( 'Y', 'V', '1', '2' );
            else if( media_type.subtype == MEDIASUBTYPE_YVU9 )
               i_fourcc = VLC_FOURCC( 'Y', 'V', 'U', '9' );

            /* Packed YUV formats */
            else if( media_type.subtype == MEDIASUBTYPE_YVYU )
               i_fourcc = VLC_FOURCC( 'Y', 'V', 'Y', 'U' );
            else if( media_type.subtype == MEDIASUBTYPE_YUYV )
               i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
            else if( media_type.subtype == MEDIASUBTYPE_Y411 )
               i_fourcc = VLC_FOURCC( 'I', '4', '1', 'N' );
            else if( media_type.subtype == MEDIASUBTYPE_Y211 )
               i_fourcc = VLC_FOURCC( 'Y', '2', '1', '1' );
            else if( media_type.subtype == MEDIASUBTYPE_YUY2 )
               i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
            else if( media_type.subtype == MEDIASUBTYPE_UYVY )
               i_fourcc = VLC_FOURCC( 'U', 'Y', 'V', 'Y' );

            /* MPEG2 video elementary stream */
            else if( media_type.subtype == MEDIASUBTYPE_MPEG2_VIDEO )
               i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'v' );

281 282 283 284
	    /* DivX video */
            else if( media_type.subtype == MEDIASUBTYPE_DIVX )
               i_fourcc = VLC_FOURCC( 'D', 'I', 'V', 'X' );

Gildas Bazin's avatar
Gildas Bazin committed
285 286 287 288 289 290 291
            /* DV formats */
            else if( media_type.subtype == MEDIASUBTYPE_dvsl )
               i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'l' );
            else if( media_type.subtype == MEDIASUBTYPE_dvsd )
               i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'd' );
            else if( media_type.subtype == MEDIASUBTYPE_dvhd )
               i_fourcc = VLC_FOURCC( 'd', 'v', 'h', 'd' );
292 293 294 295 296

            /* MJPEG format */
            else if( media_type.subtype == MEDIASUBTYPE_MJPG )
                i_fourcc = VLC_FOURCC( 'M', 'J', 'P', 'G' );

Gildas Bazin's avatar
Gildas Bazin committed
297
        }
298 299 300
    }
    else if( media_type.majortype == MEDIATYPE_Audio )
    {
301
        /* currently only support this type of audio info format */
302 303 304 305 306 307 308 309 310
        if( media_type.formattype == FORMAT_WaveFormatEx )
        {
            if( media_type.subtype == MEDIASUBTYPE_PCM )
                i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
            else if( media_type.subtype == MEDIASUBTYPE_IEEE_FLOAT )
                i_fourcc = VLC_FOURCC( 'f', 'l', '3', '2' );
        }
    }
    else if( media_type.majortype == MEDIATYPE_Stream )
Gildas Bazin's avatar
Gildas Bazin committed
311
    {
312
        if( media_type.subtype == MEDIASUBTYPE_MPEG2_PROGRAM )
Gildas Bazin's avatar
Gildas Bazin committed
313
            i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'p' );
314
        else if( media_type.subtype == MEDIASUBTYPE_MPEG2_TRANSPORT )
Gildas Bazin's avatar
Gildas Bazin committed
315
            i_fourcc = VLC_FOURCC( 'm', 'p', '2', 't' );
316
    }
Gildas Bazin's avatar
Gildas Bazin committed
317

318 319 320
    return i_fourcc;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
321 322 323 324
/****************************************************************************
 * Implementation of our dummy directshow filter pin class
 ****************************************************************************/

Gildas Bazin's avatar
Gildas Bazin committed
325 326
CapturePin::CapturePin( vlc_object_t *_p_input, access_sys_t *_p_sys,
                        CaptureFilter *_p_filter,
327
                        AM_MEDIA_TYPE *mt, size_t mt_count )
Gildas Bazin's avatar
Gildas Bazin committed
328 329 330
  : p_input( _p_input ), p_sys( _p_sys ), p_filter( _p_filter ),
    p_connected_pin( NULL ),  media_types(mt), media_type_count(mt_count),
    i_ref( 1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
331
{
332 333 334
    cx_media_type.majortype = mt[0].majortype;
    cx_media_type.subtype   = GUID_NULL;
    cx_media_type.pbFormat  = NULL;
335
    cx_media_type.cbFormat  = 0;
336
    cx_media_type.pUnk      = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
337 338 339 340
}

CapturePin::~CapturePin()
{
341 342 343
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::~CapturePin" );
#endif
344 345
    for( size_t c=0; c<media_type_count; c++ )
    {
346
        FreeMediaType(media_types[c]);
347 348
    }
    FreeMediaType(cx_media_type);
Gildas Bazin's avatar
 
Gildas Bazin committed
349 350 351 352
}

HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
{
353 354 355 356 357
#if 0 //def DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::CustomGetSample" );
#endif

    vlc_mutex_lock( &p_sys->lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
358 359 360 361
    if( samples_queue.size() )
    {
        *vlc_sample = samples_queue.back();
        samples_queue.pop_back();
362
        vlc_mutex_unlock( &p_sys->lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
363 364
        return S_OK;
    }
365
    vlc_mutex_unlock( &p_sys->lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
366 367 368
    return S_FALSE;
}

369
AM_MEDIA_TYPE &CapturePin::CustomGetMediaType()
Gildas Bazin's avatar
 
Gildas Bazin committed
370
{
371
    return cx_media_type;
Gildas Bazin's avatar
 
Gildas Bazin committed
372 373 374 375 376
}

/* IUnknown methods */
STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
377
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
378 379 380 381 382 383
    msg_Dbg( p_input, "CapturePin::QueryInterface" );
#endif

    if( riid == IID_IUnknown ||
        riid == IID_IPin )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
384
        AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
385 386 387 388 389
        *ppv = (IPin *)this;
        return NOERROR;
    }
    if( riid == IID_IMemInputPin )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
390
        AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
391 392 393 394 395
        *ppv = (IMemInputPin *)this;
        return NOERROR;
    }
    else
    {
Damien Fouilleul's avatar
Damien Fouilleul committed
396
#ifdef DEBUG_DSHOW_L1
397 398 399 400 401 402 403 404
        msg_Dbg( p_input, "CapturePin::QueryInterface() failed for: "
                 "%04X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X",
                 (int)riid.Data1, (int)riid.Data2, (int)riid.Data3,
                 static_cast<int>(riid.Data4[0]), (int)riid.Data4[1],
                 (int)riid.Data4[2], (int)riid.Data4[3],
                 (int)riid.Data4[4], (int)riid.Data4[5],
                 (int)riid.Data4[6], (int)riid.Data4[7] );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
405 406 407 408
        *ppv = NULL;
        return E_NOINTERFACE;
    }
}
409

Gildas Bazin's avatar
 
Gildas Bazin committed
410 411
STDMETHODIMP_(ULONG) CapturePin::AddRef()
{
412
#ifdef DEBUG_DSHOW_L1
413
    msg_Dbg( p_input, "CapturePin::AddRef (ref: %i)", i_ref );
Gildas Bazin's avatar
 
Gildas Bazin committed
414 415
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
416
    return i_ref++;
Gildas Bazin's avatar
 
Gildas Bazin committed
417 418 419
};
STDMETHODIMP_(ULONG) CapturePin::Release()
{
420
#ifdef DEBUG_DSHOW_L1
421
    msg_Dbg( p_input, "CapturePin::Release (ref: %i)", i_ref );
Gildas Bazin's avatar
 
Gildas Bazin committed
422 423
#endif

424
    if( !InterlockedDecrement(&i_ref) ) delete this;
Gildas Bazin's avatar
 
Gildas Bazin committed
425

426
    return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
427 428 429 430 431 432
};

/* IPin methods */
STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
                                  const AM_MEDIA_TYPE *pmt )
{
Gildas Bazin's avatar
Gildas Bazin committed
433
    if( State_Running == p_filter->state )
434
    {
Gildas Bazin's avatar
Gildas Bazin committed
435 436 437 438 439 440 441 442 443 444 445
        msg_Dbg( p_input, "CapturePin::Connect [not stopped]" );
        return VFW_E_NOT_STOPPED;
    }

    if( p_connected_pin )
    {
        msg_Dbg( p_input, "CapturePin::Connect [already connected]" );
        return VFW_E_ALREADY_CONNECTED;
    }

    if( !pmt ) return S_OK;
446
                
Gildas Bazin's avatar
Gildas Bazin committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    if( GUID_NULL != pmt->majortype &&
        media_types[0].majortype != pmt->majortype )
    {
        msg_Dbg( p_input, "CapturePin::Connect [media major type mismatch]" );
        return S_FALSE;
    }

    if( GUID_NULL != pmt->subtype && !GetFourCCFromMediaType(*pmt) )
    {
        msg_Dbg( p_input, "CapturePin::Connect [media subtype type "
                 "not supported]" );
        return S_FALSE;
    }

    if( pmt->pbFormat && pmt->majortype == MEDIATYPE_Video  )
    {
        if( !((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight ||
            !((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth )
        {
            msg_Dbg( p_input, "CapturePin::Connect "
                     "[video width/height == 0 ]" );
            return S_FALSE;
469
        }
470
    }
Gildas Bazin's avatar
Gildas Bazin committed
471 472 473

    msg_Dbg( p_input, "CapturePin::Connect [OK]" );
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
474 475 476 477
}
STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
                                            const AM_MEDIA_TYPE *pmt )
{
478
    if( State_Stopped != p_filter->state )
479
    {
Gildas Bazin's avatar
Gildas Bazin committed
480
        msg_Dbg( p_input, "CapturePin::ReceiveConnection [not stopped]" );
481
        return VFW_E_NOT_STOPPED;
482
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
483

484 485
    if( !pConnector || !pmt )
    {
Gildas Bazin's avatar
Gildas Bazin committed
486
        msg_Dbg( p_input, "CapturePin::ReceiveConnection [null pointer]" );
487
        return E_POINTER;
488
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
489

490
    if( p_connected_pin )
491
    {
Gildas Bazin's avatar
Gildas Bazin committed
492
        msg_Dbg( p_input, "CapturePin::ReceiveConnection [already connected]");
493
        return VFW_E_ALREADY_CONNECTED;
494
    }
495 496

    if( S_OK != QueryAccept(pmt) )
497
    {
Gildas Bazin's avatar
Gildas Bazin committed
498 499
        msg_Dbg( p_input, "CapturePin::ReceiveConnection "
                 "[media type not accepted]" );
500
        return VFW_E_TYPE_NOT_ACCEPTED;
501 502 503
    }

    msg_Dbg( p_input, "CapturePin::ReceiveConnection [OK]" );
Gildas Bazin's avatar
 
Gildas Bazin committed
504

Gildas Bazin's avatar
 
Gildas Bazin committed
505 506
    p_connected_pin = pConnector;
    p_connected_pin->AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
507

508 509
    FreeMediaType( cx_media_type );
    return CopyMediaType( &cx_media_type, pmt );
Gildas Bazin's avatar
 
Gildas Bazin committed
510 511 512
}
STDMETHODIMP CapturePin::Disconnect()
{
513 514
    if( ! p_connected_pin )
    {
Gildas Bazin's avatar
Gildas Bazin committed
515 516
        msg_Dbg( p_input, "CapturePin::Disconnect [not connected]" );
        return S_FALSE;
517
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
518

519
    msg_Dbg( p_input, "CapturePin::Disconnect [OK]" );
520

521
    /* samples_queue was already flushed in EndFlush() */
522

523
    p_connected_pin->Release();
Gildas Bazin's avatar
 
Gildas Bazin committed
524
    p_connected_pin = NULL;
525 526
    //FreeMediaType( cx_media_type );
    //cx_media_type.subtype = GUID_NULL;
527

Gildas Bazin's avatar
 
Gildas Bazin committed
528 529 530 531
    return S_OK;
}
STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
{
532 533
    if( !p_connected_pin )
    {
Gildas Bazin's avatar
Gildas Bazin committed
534 535
        msg_Dbg( p_input, "CapturePin::ConnectedTo [not connected]" );
        return VFW_E_NOT_CONNECTED;
536
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
537

Gildas Bazin's avatar
 
Gildas Bazin committed
538 539 540
    p_connected_pin->AddRef();
    *pPin = p_connected_pin;

541 542
    msg_Dbg( p_input, "CapturePin::ConnectedTo [OK]" );

Gildas Bazin's avatar
 
Gildas Bazin committed
543 544 545 546
    return S_OK;
}
STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
{
547 548
    if( !p_connected_pin )
    {
Gildas Bazin's avatar
Gildas Bazin committed
549 550
        msg_Dbg( p_input, "CapturePin::ConnectionMediaType [not connected]" );
        return VFW_E_NOT_CONNECTED;
551
    }
552 553

    return CopyMediaType( pmt, &cx_media_type );
Gildas Bazin's avatar
 
Gildas Bazin committed
554 555 556 557 558 559 560 561 562 563
}
STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
#endif

    pInfo->pFilter = p_filter;
    if( p_filter ) p_filter->AddRef();

564
    memcpy(pInfo->achName, PIN_NAME, sizeof(PIN_NAME));
Gildas Bazin's avatar
 
Gildas Bazin committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    pInfo->dir = PINDIR_INPUT;

    return NOERROR;
}
STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::QueryDirection" );
#endif

    *pPinDir = PINDIR_INPUT;
    return NOERROR;
}
STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::QueryId" );
#endif
583 584 585 586

    *Id = L"VideoLAN Capture Pin";

    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
587 588 589
}
STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
{
Gildas Bazin's avatar
Gildas Bazin committed
590 591 592 593 594 595 596
    if( State_Stopped != p_filter->state )
    {
        msg_Dbg( p_input, "CapturePin::QueryAccept [not stopped]" );
        return S_FALSE;
    }

    if( media_types[0].majortype != pmt->majortype )
597
    {
Gildas Bazin's avatar
Gildas Bazin committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
        msg_Dbg( p_input, "CapturePin::QueryAccept [media type mismatch]" );
        return S_FALSE;
    }

    int i_fourcc = GetFourCCFromMediaType(*pmt);
    if( !i_fourcc )
    {   
        msg_Dbg( p_input, "CapturePin::QueryAccept "
                 "[media type not supported]" );
        return S_FALSE;
    }

    if( pmt->majortype == MEDIATYPE_Video )
    {
        if( pmt->pbFormat &&
            ( (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight == 0) ||
              (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth == 0) ) )
615
        {
Gildas Bazin's avatar
Gildas Bazin committed
616 617
            msg_Dbg( p_input, "CapturePin::QueryAccept [video size wxh == 0]");
            return S_FALSE;
618
        }
Gildas Bazin's avatar
Gildas Bazin committed
619 620

        msg_Dbg( p_input, "CapturePin::QueryAccept [OK] "
621
                 "(width=%ld, height=%ld, chroma=%4.4s, fps=%f)",
Gildas Bazin's avatar
Gildas Bazin committed
622 623
                 ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth,
                 ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight,
624 625
                 (char *)&i_fourcc,
		 10000000.0f/((float)((VIDEOINFOHEADER *)pmt->pbFormat)->AvgTimePerFrame) );
626
    }
627
    else if( pmt->majortype == MEDIATYPE_Audio )
Gildas Bazin's avatar
Gildas Bazin committed
628 629 630 631 632 633 634 635
    {
        msg_Dbg( p_input, "CapturePin::QueryAccept [OK] (channels=%d, "
                 "samples/sec=%lu, bits/samples=%d, format=%4.4s)",
                 ((WAVEFORMATEX *)pmt->pbFormat)->nChannels,
                 ((WAVEFORMATEX *)pmt->pbFormat)->nSamplesPerSec,
                 ((WAVEFORMATEX *)pmt->pbFormat)->wBitsPerSample,
                 (char *)&i_fourcc );
    }   
636 637 638 639 640
    else
    {
        msg_Dbg( p_input, "CapturePin::QueryAccept [OK] (stream format=%4.4s)",
                 (char *)&i_fourcc );
    }
Gildas Bazin's avatar
Gildas Bazin committed
641 642 643 644 645

    if( p_connected_pin )
    {
        FreeMediaType( cx_media_type );
        CopyMediaType( &cx_media_type, pmt );
646
    }
Gildas Bazin's avatar
Gildas Bazin committed
647 648

    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
649 650 651
}
STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
{
Damien Fouilleul's avatar
Damien Fouilleul committed
652
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
653 654 655 656 657 658 659 660 661 662 663
    msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
#endif

    *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );

    if( *ppEnum == NULL ) return E_OUTOFMEMORY;

    return NOERROR;
}
STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
{
Damien Fouilleul's avatar
Damien Fouilleul committed
664
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
665 666 667 668 669 670 671 672 673
    msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
#endif
    return E_NOTIMPL;
}
STDMETHODIMP CapturePin::EndOfStream( void )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::EndOfStream" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
674
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
675 676 677 678 679 680
}
STDMETHODIMP CapturePin::BeginFlush( void )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::BeginFlush" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
681
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
682 683 684 685 686 687
}
STDMETHODIMP CapturePin::EndFlush( void )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::EndFlush" );
#endif
688 689 690 691 692 693 694 695 696 697 698 699

    VLCMediaSample vlc_sample;

    vlc_mutex_lock( &p_sys->lock );
    while( samples_queue.size() )
    {
        vlc_sample = samples_queue.back();
        samples_queue.pop_back();
        vlc_sample.p_sample->Release();
    }
    vlc_mutex_unlock( &p_sys->lock );

Gildas Bazin's avatar
 
Gildas Bazin committed
700
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
701 702 703 704 705 706 707 708
}
STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
                                     REFERENCE_TIME tStop,
                                     double dRate )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::NewSegment" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
709
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
}

/* IMemInputPin methods */
STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::GetAllocator" );
#endif

    return VFW_E_NO_ALLOCATOR;
}
STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
                                          BOOL bReadOnly )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
#endif

    return S_OK;
}
STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
#endif
735

Gildas Bazin's avatar
 
Gildas Bazin committed
736 737 738 739
    return E_NOTIMPL;
}
STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
{
740 741
#if 0 //def DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::Receive" );
Gildas Bazin's avatar
 
Gildas Bazin committed
742 743
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
744
    pSample->AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
745 746
    mtime_t i_timestamp = mdate() * 10;
    VLCMediaSample vlc_sample = {pSample, i_timestamp};
747 748

    vlc_mutex_lock( &p_sys->lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
749 750 751 752 753 754 755 756
    samples_queue.push_front( vlc_sample );

    /* Make sure we don't cache too many samples */
    if( samples_queue.size() > 10 )
    {
        vlc_sample = samples_queue.back();
        samples_queue.pop_back();
        msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
Gildas Bazin's avatar
 
Gildas Bazin committed
757
        vlc_sample.p_sample->Release();
Gildas Bazin's avatar
 
Gildas Bazin committed
758 759
    }

760 761 762
    vlc_cond_signal( &p_sys->wait );
    vlc_mutex_unlock( &p_sys->lock );

Gildas Bazin's avatar
 
Gildas Bazin committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
    return S_OK;
}
STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
                                          long nSamples,
                                          long *nSamplesProcessed )
{
    HRESULT hr = S_OK;

    *nSamplesProcessed = 0;
    while( nSamples-- > 0 )
    {
         hr = Receive( pSamples[*nSamplesProcessed] );
         if( hr != S_OK ) break;
         (*nSamplesProcessed)++;
    }
    return hr;
}
STDMETHODIMP CapturePin::ReceiveCanBlock( void )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
#endif

    return S_FALSE; /* Thou shalt not block */
}

/****************************************************************************
 * Implementation of our dummy directshow filter class
 ****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
792 793 794 795 796
CaptureFilter::CaptureFilter( vlc_object_t *_p_input, access_sys_t *p_sys,
                              AM_MEDIA_TYPE *mt, size_t mt_count )
  : p_input( _p_input ),
    p_pin( new CapturePin( _p_input, p_sys, this, mt, mt_count ) ),
    state( State_Stopped ), i_ref( 1 ) 
Gildas Bazin's avatar
 
Gildas Bazin committed
797 798 799 800 801
{
}

CaptureFilter::~CaptureFilter()
{
802 803 804
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::~CaptureFilter" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
805 806 807 808 809 810
    p_pin->Release();
}

/* IUnknown methods */
STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
{
Damien Fouilleul's avatar
Damien Fouilleul committed
811
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
812 813 814 815 816
    msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
#endif

    if( riid == IID_IUnknown )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
817
        AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
818 819 820 821 822
        *ppv = (IUnknown *)this;
        return NOERROR;
    }
    if( riid == IID_IPersist )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
823
        AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
824 825 826 827 828
        *ppv = (IPersist *)this;
        return NOERROR;
    }
    if( riid == IID_IMediaFilter )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
829
        AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
830 831 832 833 834
        *ppv = (IMediaFilter *)this;
        return NOERROR;
    }
    if( riid == IID_IBaseFilter )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
835
        AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
836 837 838 839 840
        *ppv = (IBaseFilter *)this;
        return NOERROR;
    }
    else
    {
Damien Fouilleul's avatar
Damien Fouilleul committed
841
#ifdef DEBUG_DSHOW_L1
842 843 844 845 846 847 848 849
        msg_Dbg( p_input, "CaptureFilter::QueryInterface() failed for: "
                 "%04X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X",
                 (int)riid.Data1, (int)riid.Data2, (int)riid.Data3,
                 static_cast<int>(riid.Data4[0]), (int)riid.Data4[1],
                 (int)riid.Data4[2], (int)riid.Data4[3],
                 (int)riid.Data4[4], (int)riid.Data4[5],
                 (int)riid.Data4[6], (int)riid.Data4[7] );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
850 851 852 853 854 855
        *ppv = NULL;
        return E_NOINTERFACE;
    }
};
STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
{
856
#ifdef DEBUG_DSHOW_L1
857
    msg_Dbg( p_input, "CaptureFilter::AddRef (ref: %i)", i_ref );
Gildas Bazin's avatar
 
Gildas Bazin committed
858 859
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
860
    return i_ref++;
Gildas Bazin's avatar
 
Gildas Bazin committed
861 862 863
};
STDMETHODIMP_(ULONG) CaptureFilter::Release()
{
864
#ifdef DEBUG_DSHOW_L1
865
    msg_Dbg( p_input, "CaptureFilter::Release (ref: %i)", i_ref );
Gildas Bazin's avatar
 
Gildas Bazin committed
866 867
#endif

868
    if( !InterlockedDecrement(&i_ref) ) delete this;
Gildas Bazin's avatar
 
Gildas Bazin committed
869

870
    return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
};

/* IPersist method */
STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::GetClassID" );
#endif
    return E_NOTIMPL;
};

/* IMediaFilter methods */
STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
{
#ifdef DEBUG_DSHOW
Gildas Bazin's avatar
 
Gildas Bazin committed
886
    msg_Dbg( p_input, "CaptureFilter::GetState %i", state );
Gildas Bazin's avatar
 
Gildas Bazin committed
887
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
888 889 890

    *State = state;
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
891 892 893 894 895 896 897
};
STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
898
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
899 900 901 902 903 904
};
STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
905 906 907

    *pClock = NULL;
    return NOERROR;
Gildas Bazin's avatar
 
Gildas Bazin committed
908 909 910 911 912 913
};
STDMETHODIMP CaptureFilter::Stop()
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::Stop" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
914

915 916
    p_pin->EndFlush();

Gildas Bazin's avatar
 
Gildas Bazin committed
917
    state = State_Stopped;
Gildas Bazin's avatar
 
Gildas Bazin committed
918
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
919 920 921 922 923 924
};
STDMETHODIMP CaptureFilter::Pause()
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::Pause" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
925 926

    state = State_Paused;
Gildas Bazin's avatar
 
Gildas Bazin committed
927
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
928 929 930 931 932 933
};
STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::Run" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
934 935

    state = State_Running;
Gildas Bazin's avatar
 
Gildas Bazin committed
936
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
};

/* IBaseFilter methods */
STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::EnumPins" );
#endif

    /* Create a new ref counted enumerator */
    *ppEnum = new CaptureEnumPins( p_input, this, NULL );
    return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
};
STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::FindPin" );
#endif
    return E_NOTIMPL;
};
STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
#endif

963
    memcpy(pInfo->achName, FILTER_NAME, sizeof(FILTER_NAME));
Gildas Bazin's avatar
 
Gildas Bazin committed
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

    pInfo->pGraph = p_graph;
    if( p_graph ) p_graph->AddRef();

    return NOERROR;
};
STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
                                             LPCWSTR pName )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
#endif

    p_graph = pGraph;

    return NOERROR;
};
STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
{
#ifdef DEBUG_DSHOW
    msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
#endif
    return E_NOTIMPL;
};

/* Custom methods */
CapturePin *CaptureFilter::CustomGetPin()
{
    return p_pin;
}

/****************************************************************************
 * Implementation of our dummy directshow enumpins class
 ****************************************************************************/

Gildas Bazin's avatar
Gildas Bazin committed
999
CaptureEnumPins::CaptureEnumPins( vlc_object_t *_p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
                                  CaptureFilter *_p_filter,
                                  CaptureEnumPins *pEnumPins )
  : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
{
    /* Hold a reference count on our filter */
    p_filter->AddRef();

    /* Are we creating a new enumerator */

    if( pEnumPins == NULL )
    {
        i_position = 0;
    }
    else
    {
        i_position = pEnumPins->i_position;
    }
}

CaptureEnumPins::~CaptureEnumPins()
{
Damien Fouilleul's avatar
Damien Fouilleul committed
1021
#ifdef DEBUG_DSHOW_L1
1022 1023
    msg_Dbg( p_input, "CaptureEnumPins::~CaptureEnumPins" );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
1024 1025 1026 1027 1028 1029
    p_filter->Release();
}

/* IUnknown methods */
STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
{
Damien Fouilleul's avatar
Damien Fouilleul committed
1030
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
1031 1032 1033 1034 1035 1036
    msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
#endif

    if( riid == IID_IUnknown ||
        riid == IID_IEnumPins )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1037
        AddRef();
Gildas Bazin's avatar
 
Gildas Bazin committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
        *ppv = (IEnumPins *)this;
        return NOERROR;
    }
    else
    {
        *ppv = NULL;
        return E_NOINTERFACE;
    }
};
STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
{
1049
#ifdef DEBUG_DSHOW_L1
1050
    msg_Dbg( p_input, "CaptureEnumPins::AddRef (ref: %i)", i_ref );
Gildas Bazin's avatar
 
Gildas Bazin committed
1051 1052
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
1053
    return i_ref++;
Gildas Bazin's avatar
 
Gildas Bazin committed
1054 1055 1056
};
STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
{
1057
#ifdef DEBUG_DSHOW_L1
1058
    msg_Dbg( p_input, "CaptureEnumPins::Release (ref: %i)", i_ref );
Gildas Bazin's avatar
 
Gildas Bazin committed
1059 1060
#endif

1061
    if( !InterlockedDecrement(&i_ref) ) delete this;
Gildas Bazin's avatar
 
Gildas Bazin committed
1062

1063
    return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1064 1065 1066 1067 1068 1069
};

/* IEnumPins */
STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
                                    ULONG * pcFetched )
{
Damien Fouilleul's avatar
Damien Fouilleul committed
1070
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
1071 1072 1073
    msg_Dbg( p_input, "CaptureEnumPins::Next" );
#endif

1074
    unsigned int i_fetched = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1075 1076 1077 1078 1079 1080

    if( i_position < 1 && cPins > 0 )
    {
        IPin *pPin = p_filter->CustomGetPin();
        *ppPins = pPin;
        pPin->AddRef();
1081
        i_fetched = 1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1082 1083 1084
        i_position++;
    }

1085 1086 1087
    if( pcFetched ) *pcFetched = i_fetched;

    return (i_fetched == cPins) ? S_OK : S_FALSE;
Gildas Bazin's avatar
 
Gildas Bazin committed
1088 1089 1090
};
STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
{
Damien Fouilleul's avatar
Damien Fouilleul committed
1091
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
1092 1093 1094
    msg_Dbg( p_input, "CaptureEnumPins::Skip" );
#endif

1095 1096 1097
    i_position += cPins;

    if( i_position > 1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
1098 1099 1100 1101
    {
        return S_FALSE;
    }

1102
    return S_OK;
Gildas Bazin's avatar
 
Gildas Bazin committed
1103 1104 1105
};
STDMETHODIMP CaptureEnumPins::Reset()
{
Damien Fouilleul's avatar
Damien Fouilleul committed
1106
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
1107 1108 1109 1110 1111 1112 1113 1114
    msg_Dbg( p_input, "CaptureEnumPins::Reset" );
#endif

    i_position = 0;
    return S_OK;
};
STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
{
Damien Fouilleul's avatar
Damien Fouilleul committed
1115
#ifdef DEBUG_DSHOW_L1
Gildas Bazin's avatar
 
Gildas Bazin committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    msg_Dbg( p_input, "CaptureEnumPins::Clone" );
#endif

    *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
    if( *ppEnum == NULL ) return E_OUTOFMEMORY;

    return NOERROR;
};

/****************************************************************************
 * Implementation of our dummy directshow enummediatypes class
 ****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
1128
CaptureEnumMediaTypes::CaptureEnumMediaTypes( vlc_object_t *_p_input,
1129
    CapturePin *_p_pin, CaptureEnumMediaTypes *pEnumMediaTypes )
Gildas Bazin's avatar
 
Gildas Bazin committed
1130 1131 1132 1133 1134 1135 1136 1137
  : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
{
    /* Hold a reference count on our filter */
    p_pin->AddRef();

    /* Are we creating a new enumerator */
    if( pEnumMediaTypes == NULL )
    {
Gildas Bazin's avatar
Gildas Bazin committed
1138
        CopyMediaType(&cx_media_type, &p_pin->cx_media_type); 
Gildas Bazin's avatar
 
Gildas Bazin committed
1139 1140 1141 1142
        i_position = 0;
    }
    else
    {
Gildas Bazin's avatar
Gildas Bazin committed
1143
        CopyMediaType(&cx_media_type, &pEnumMediaTypes->cx_media_type); 
Gildas Bazin's avatar
 
Gildas Bazin committed
1144 1145 1146 1147 1148 1149
        i_position = pEnumMediaTypes->i_position;
    }
}

CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
{
Damien Fouilleul's avatar
Damien Fouilleul committed
1150
#ifdef DEBUG_DSHOW_L1
1151 1152
    msg_Dbg( p_input, "CaptureEnumMediaTypes::~CaptureEnumMediaTypes" );
#endif
1153
    FreeMediaType(cx_media_type);
Gildas Bazin's avatar
 
Gildas Bazin committed