Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LAVFUtils.cpp « Demuxers « demuxer - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b6c5a6dfecfbfad46402d81f9fffe44e9c246e8 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 *      Copyright (C) 2010-2013 Hendrik Leppkes
 *      http://www.1f0.de
 *
 *  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 Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "stdafx.h"
#include "lavfutils.h"

extern "C" {
#include "libavcodec/audioconvert.h"
}

#include <sstream>

static int get_bit_rate(AVCodecContext *ctx)
{
  int bit_rate;
  int bits_per_sample;

  switch(ctx->codec_type) {
  case AVMEDIA_TYPE_VIDEO:
  case AVMEDIA_TYPE_DATA:
  case AVMEDIA_TYPE_SUBTITLE:
  case AVMEDIA_TYPE_ATTACHMENT:
    bit_rate = ctx->bit_rate;
    break;
  case AVMEDIA_TYPE_AUDIO:
    bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
    bit_rate = ctx->bit_rate ? ctx->bit_rate : ctx->sample_rate * ctx->channels * bits_per_sample;
    break;
  default:
    bit_rate = 0;
    break;
  }
  return bit_rate;
}

const char *get_stream_language(const AVStream *pStream)
{
  char *lang = NULL;
  if (av_dict_get(pStream->metadata, "language", NULL, 0)) {
    lang = av_dict_get(pStream->metadata, "language", NULL, 0)->value;
  }
  // Don't bother with undetermined languages (fallback value in some containers)
  if(lang && strncmp(lang, "und", 3))
    return lang;
  return NULL;
}

struct s_id_map {
  int id;
  const char *name;
};

struct s_id_map nice_codec_names[] = {
  // Video
  { AV_CODEC_ID_H264, "h264" }, // XXX: Do not remove, required for custom profile/level formatting
  { AV_CODEC_ID_VC1, "vc-1" },  // XXX: Do not remove, required for custom profile/level formatting
  { AV_CODEC_ID_MPEG2VIDEO, "mpeg2" },
  // Audio
  { AV_CODEC_ID_DTS, "dts" },
  { AV_CODEC_ID_AAC_LATM, "aac (latm)" },
  // Subs
  { AV_CODEC_ID_TEXT, "txt" },
  { AV_CODEC_ID_MOV_TEXT, "tx3g" },
  { AV_CODEC_ID_SRT, "srt" },
  { AV_CODEC_ID_HDMV_PGS_SUBTITLE, "pgs" },
  { AV_CODEC_ID_DVD_SUBTITLE, "vobsub" },
  { AV_CODEC_ID_DVB_SUBTITLE, "dvbsub" },
  { AV_CODEC_ID_SSA, "ssa/ass" },
  { AV_CODEC_ID_XSUB, "xsub" },
};

// Uppercase the given string
static std::string tolower(const char *str) {
  size_t len = strlen(str);
  std::string ret(len, char());
  for (size_t i = 0; i < len; ++i) {
    ret[i] = (str[i] <= 'Z' && str[i] >= 'A') ? str[i]+('a'-'A') : str[i];
  }
  return ret;
}

std::string get_codec_name(AVCodecContext *pCodecCtx)
{
  AVCodecID id = pCodecCtx->codec_id;

  // Grab the codec
  AVCodec *p = avcodec_find_decoder(id);
  const char *profile = p ? av_get_profile_name(p, pCodecCtx->profile) : NULL;

  std::ostringstream codec_name;

  const char *nice_name = NULL;
  for (int i = 0; i < countof(nice_codec_names); ++i)
  {
    if (nice_codec_names[i].id == id) {
      nice_name = nice_codec_names[i].name;
      break;
    }
  }

  if (id == AV_CODEC_ID_DTS && pCodecCtx->codec_tag == 0xA2) {
    profile = "DTS Express";
  }

  if (id == AV_CODEC_ID_H264 && profile) {
    codec_name << nice_name << " " << tolower(profile);
    if (pCodecCtx->level && pCodecCtx->level != FF_LEVEL_UNKNOWN && pCodecCtx->level < 1000) {
      char l_buf[5];
      sprintf_s(l_buf, "%.1f", pCodecCtx->level / 10.0);
      codec_name << " L" << l_buf;
    }
  } else if (id == AV_CODEC_ID_VC1 && profile) {
    codec_name << nice_name << " " << tolower(profile);
    if (pCodecCtx->level != FF_LEVEL_UNKNOWN) {
      codec_name << " L" << pCodecCtx->level;
    }
  } else if (id == AV_CODEC_ID_DTS && profile) {
    codec_name << tolower(profile);
  } else if (nice_name) {
    codec_name << nice_name;
    if (profile)
      codec_name << " " << tolower(profile);
  } else if (p && p->name) {
    codec_name << p->name;
    if (profile)
      codec_name << " " << tolower(profile);
  } else if (pCodecCtx->codec_name[0] != '\0') {
    codec_name << pCodecCtx->codec_name;
  } else {
    /* output avi tags */
    char buf[32];
    av_get_codec_tag_string(buf, sizeof(buf), pCodecCtx->codec_tag);
    codec_name << buf;
    sprintf_s(buf, "0x%04X", pCodecCtx->codec_tag);
    codec_name  << " / " << buf;
  }
  return codec_name.str();
}

#define SUPPORTED_FLAGS (AV_DISPOSITION_FORCED|AV_DISPOSITION_DEFAULT|AV_DISPOSITION_HEARING_IMPAIRED|AV_DISPOSITION_VISUAL_IMPAIRED|LAVF_DISPOSITION_SUB_STREAM|LAVF_DISPOSITION_SECONDARY_AUDIO)

static std::string format_flags(int flags){
  std::ostringstream out;

#define FLAG_TAG(f, s) if(flags & f) { if(!first) out << ","; out << s; first = false; }

  if (flags & SUPPORTED_FLAGS) {
    bool first = true;
    out << " [";
    FLAG_TAG(AV_DISPOSITION_DEFAULT, "default");
    FLAG_TAG(AV_DISPOSITION_FORCED, "forced");
    FLAG_TAG(AV_DISPOSITION_HEARING_IMPAIRED, "hearing impaired");
    FLAG_TAG(AV_DISPOSITION_VISUAL_IMPAIRED, "visual impaired");
    FLAG_TAG(LAVF_DISPOSITION_SUB_STREAM, "sub");
    FLAG_TAG(LAVF_DISPOSITION_SECONDARY_AUDIO, "secondary");
    out << "]";
  }
  return out.str();
}

static bool show_sample_fmt(AVCodecID codec_id) {
  // PCM Codecs
  if (codec_id >= 0x10000 && codec_id < 0x12000) {
    return true;
  }
  // Lossless Codecs
  if (codec_id == AV_CODEC_ID_MLP
   || codec_id == AV_CODEC_ID_TRUEHD
   || codec_id == AV_CODEC_ID_FLAC
   || codec_id == AV_CODEC_ID_WMALOSSLESS
   || codec_id == AV_CODEC_ID_WAVPACK
   || codec_id == AV_CODEC_ID_MP4ALS
   || codec_id == AV_CODEC_ID_ALAC) {
     return true;
  }
  return false;
}

std::string lavf_get_stream_description(AVStream *pStream)
{
  AVCodecContext *enc = pStream->codec;

  std::string codec_name = get_codec_name(enc);

  const char *lang = get_stream_language(pStream);
  std::string sLanguage;

  if(lang) {
    sLanguage = ProbeLangForLanguage(lang);
    if (sLanguage.empty()) {
      sLanguage = lang;
    }
  }

  char *title = NULL;
  if (av_dict_get(pStream->metadata, "title", NULL, 0)) {
    title = av_dict_get(pStream->metadata, "title", NULL, 0)->value;
  } else if (av_dict_get(pStream->metadata, "handler_name", NULL, 0)) {
    title = av_dict_get(pStream->metadata, "handler_name", NULL, 0)->value;
    if (strcmp(title, "GPAC ISO Video Handler") == 0 || strcmp(title, "VideoHandler") == 0|| strcmp(title, "GPAC ISO Audio Handler") == 0 || strcmp(title, "GPAC Streaming Text Handler") == 0)
      title = NULL;
  }

  // Empty titles are rather useless
  if (title && strlen(title) == 0)
    title = NULL;

  int bitrate = get_bit_rate(enc);

  std::ostringstream buf;
  switch(enc->codec_type) {
  case AVMEDIA_TYPE_VIDEO:
    buf << "V: ";
    // Title/Language
    if (title && lang) {
      buf << title << " [" << lang << "] (";
    } else if (title) {
      // Print either title or lang
      buf << title << " (";
    } else if (lang) {
      buf << sLanguage << " [" << lang << "] (";
    }
    // Codec
    buf << codec_name;
    // Pixel Format
    if (enc->pix_fmt != PIX_FMT_NONE) {
      buf << ", " << av_get_pix_fmt_name(enc->pix_fmt);
    }
    // Dimensions
    if (enc->width) {
      buf << ", " << enc->width << "x" << enc->height;
    }
    // Bitrate
    if (bitrate > 0) {
      buf << ", " << (bitrate / 1000) << " kb/s";
    }
    // Closing tag
    if (title || lang) {
      buf << ")";
    }
    buf << format_flags(pStream->disposition);
    break;
  case AVMEDIA_TYPE_AUDIO:
    buf << "A: ";
    // Title/Language
    if (title && lang) {
      buf << title << " [" << lang << "] (";
    } else if (title) {
      // Print either title or lang
      buf << title << " (";
    } else if (lang) {
      buf << sLanguage << " [" << lang << "] (";
    }
    // Codec
    buf << codec_name;
    // Sample Rate
    if (enc->sample_rate) {
      buf << ", " << enc->sample_rate << " Hz";
    }
    if (enc->channels) {
      // Get channel layout
      char channel[32];
      av_get_channel_layout_string(channel, 32, enc->channels, enc->channel_layout);
      buf << ", " << channel;
    }
    // Sample Format
    if (show_sample_fmt(enc->codec_id) && get_bits_per_sample(enc, true)) {
      if (enc->sample_fmt == AV_SAMPLE_FMT_FLT || enc->sample_fmt == AV_SAMPLE_FMT_DBL) {
        buf << ", fp";
      } else {
        buf << ", s";
      }
      buf << get_bits_per_sample(enc, true);
    }
    // Bitrate
    if (bitrate > 0) {
      buf << ", " << (bitrate / 1000) << " kb/s";
    }
    // Closing tag
    if (title || lang) {
      buf << ")";
    }
    // Flags
    buf << format_flags(pStream->disposition);
    break;
  case AVMEDIA_TYPE_SUBTITLE:
    buf << "S: ";
    // Title/Language
    if (title && lang) {
      buf << title << " [" << lang << "] (";
    } else if (title) {
      // Print either title or lang
      buf << title << " (";
    } else if (lang) {
      buf << sLanguage << " [" << lang << "] (";
    }
    // Codec
    buf << codec_name;
    if (title || lang) {
      buf << ")";
    }
    // Subtitle flags
    buf << format_flags(pStream->disposition);
    break;
  default:
    buf << "Unknown: Stream #" << pStream->index;
    break;
  }

  return buf.str();
}

#ifdef DEBUG

#define LAVF_PARSE_TYPE(x) case x: return #x;
const char *lavf_get_parsing_string(enum AVStreamParseType parsing)
{
  switch (parsing) {
    LAVF_PARSE_TYPE(AVSTREAM_PARSE_NONE);
    LAVF_PARSE_TYPE(AVSTREAM_PARSE_FULL);
    LAVF_PARSE_TYPE(AVSTREAM_PARSE_HEADERS);
    LAVF_PARSE_TYPE(AVSTREAM_PARSE_TIMESTAMPS);
    LAVF_PARSE_TYPE(AVSTREAM_PARSE_FULL_ONCE);
  };
  return "unknown";
}
#endif