aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_avi/ExtendedFileInfo.cpp
blob: 23262e66b9a8a2d6cfdeebd3f968a3301137efaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <bfc/platform/types.h>
#include <windows.h>
#include "api__in_avi.h"
#include "win32_avi_reader.h"
#include "../nsavi/metadata.h"
#include "../nu/ns_wc.h"
#include <strsafe.h>
#include "resource.h"

static void ReadMetadata(nsavi::Metadata &metadata, uint32_t id, wchar_t *dest, size_t destlen)
{
	nsavi::Info *info=0;
	const char *str = 0;
	if (metadata.GetInfo(&info) == nsavi::READ_OK && (str = info->GetMetadata(id)))
	{
		MultiByteToWideCharSZ(CP_ACP/*UTF8*/, 0, str, -1, dest, (int)destlen);
	}
	else
		dest[0]=0;
}

extern "C" __declspec(dllexport)
int winampGetExtendedFileInfoW(const wchar_t *fn, const char *data, wchar_t *dest, size_t destlen)
{
	if (!_stricmp(data, "type"))
	{
		dest[0]='1';
		dest[1]=0;
		return 1;
	}
	else if (!_stricmp(data, "family"))
	{
		int len;
		const wchar_t *p;
		if (!fn || !fn[0]) return 0;
		len = lstrlenW(fn);
		if (len < 4 || L'.' != fn[len - 4])  return 0;
		p = &fn[len - 3];
		if (!_wcsicmp(p, L"AVI") && S_OK == StringCchCopyW(dest, destlen, WASABI_API_LNGSTRINGW(IDS_FAMILY_STRING))) return 1;
		return 0;
	}
	else
	{
		AVIReaderWin32 reader;
		if (reader.Open(fn) == nsavi::READ_OK) 
		{
			nsavi::Metadata metadata(&reader); 
			uint32_t riff_type;
			metadata.GetRIFFType(&riff_type); // need to call this to get the party started
			// TODO: cache metadata object

			if (!_stricmp(data, "length"))
			{
				int time_ms;
				if (metadata.GetDuration(&time_ms) == nsavi::READ_OK)
					StringCchPrintf(dest, destlen, L"%d", time_ms);
				else
					dest[0]=0;
			}
			else if (!_stricmp(data, "height"))
			{
				nsavi::HeaderList header_list;
				if (metadata.GetHeaderList(&header_list) == nsavi::READ_OK && header_list.avi_header && header_list.avi_header->height)
					StringCchPrintf(dest, destlen, L"%d", header_list.avi_header->height);
				else
					dest[0]=0;
			}
			else if (!_stricmp(data, "width"))
			{
				nsavi::HeaderList header_list;
				if (metadata.GetHeaderList(&header_list) == nsavi::READ_OK && header_list.avi_header && header_list.avi_header->width)
					StringCchPrintf(dest, destlen, L"%d", header_list.avi_header->width);
				else
					dest[0]=0;
			}
			else if (!_stricmp(data, "bitrate"))
			{
				int time_ms = 0;
				uint64_t file_length = 0;
				if (metadata.GetDuration(&time_ms) == nsavi::READ_OK
					&& (file_length = reader.GetContentLength())
					&& time_ms > 0 && file_length > 0)
				{
					uint64_t bitrate = 8ULL * file_length / (uint64_t)time_ms;
					StringCchPrintf(dest, destlen, L"%I64u", bitrate);
				}
				else
					dest[0]=0;
			}
			else if (!_stricmp(data, "artist"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','A','R','T'), dest, destlen);
			}
			else if (!_stricmp(data, "publisher"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','P','U','B'), dest, destlen);
			}
			else if (!_stricmp(data, "album"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','A','L','B'), dest, destlen);
			}
			else if (!_stricmp(data, "composer"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','C','O','M'), dest, destlen);
			}
			else if (!_stricmp(data, "genre"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','G','N','R'), dest, destlen);
			}
			else if (!_stricmp(data, "comment"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','C','M','T'), dest, destlen);
			}
			else if (!_stricmp(data, "title"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','N','A','M'), dest, destlen);
			}
			else if (!_stricmp(data, "tool"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','S','F','T'), dest, destlen);
			}
			else if (!_stricmp(data, "copyright"))
			{
				ReadMetadata(metadata, nsaviFOURCC('I','C','O','P'), dest, destlen);
			}
			else
			{
				reader.Close();
				return 0;
			}

			reader.Close();
			return 1;
		}
	}

	return 0;
}