Replacing switch case with codec map

Prevent the occurrence of multiple case with same value
This commit is contained in:
xiongziliang 2023-12-31 22:14:58 +08:00
parent a106f8dfc0
commit f382f1fa95

View File

@ -69,12 +69,16 @@ CodecId getCodecByMovId(int object_id) {
if (object_id == MOV_OBJECT_NONE) { if (object_id == MOV_OBJECT_NONE) {
return CodecInvalid; return CodecInvalid;
} }
switch (object_id) {
#define XX(name, type, value, str, mpeg_id, mp4_id) case mp4_id : return name; #define XX(name, type, value, str, mpeg_id, mp4_id) { mp4_id, name },
CODEC_MAP(XX) static map<int, CodecId> s_map = { CODEC_MAP(XX) };
#undef XX #undef XX
default : WarnL << "Unsupported mov: " << object_id; return CodecInvalid; auto it = s_map.find(object_id);
if (it == s_map.end()) {
WarnL << "Unsupported mov: " << object_id;
return CodecInvalid;
} }
return it->second;
} }
#endif #endif
@ -89,17 +93,20 @@ int getMpegIdByCodec(CodecId codec) {
} }
CodecId getCodecByMpegId(int mpeg_id) { CodecId getCodecByMpegId(int mpeg_id) {
if (mpeg_id == PSI_STREAM_RESERVED) { if (mpeg_id == PSI_STREAM_RESERVED || mpeg_id == 0xBD) {
// 海康的 PS 流中会有0xBD 的包
return CodecInvalid; return CodecInvalid;
} }
switch (mpeg_id) {
#define XX(name, type, value, str, mpeg_id, mp4_id) case mpeg_id : return name; #define XX(name, type, value, str, mpeg_id, mp4_id) { mpeg_id, name },
CODEC_MAP(XX) static map<int, CodecId> s_map = { CODEC_MAP(XX) };
#undef XX #undef XX
// 海康的 PS 流中会有0xBD 的包 auto it = s_map.find(mpeg_id);
case 0xBD: return CodecInvalid; if (it == s_map.end()) {
default : WarnL << "Unsupported mpeg: " << mpeg_id; return CodecInvalid; WarnL << "Unsupported mpeg: " << mpeg_id;
return CodecInvalid;
} }
return it->second;
} }
#endif #endif