mp3: use mpg123 functions for detection

Signed-off-by: Mahyar Koshkouei <mk@deltabeard.com>
This commit is contained in:
Mahyar Koshkouei
2025-07-30 22:01:28 +01:00
parent 08ec6a7adb
commit e64472e689
3 changed files with 59 additions and 3 deletions

View File

@@ -2,3 +2,4 @@
#include "playback.h" #include "playback.h"
void setMp3(struct decoder_fn* decoder); void setMp3(struct decoder_fn* decoder);
int isMp3(const char *path);

View File

@@ -95,9 +95,7 @@ enum file_types getFileType(const char *file)
* MP3 without ID3 tag, ID3v1 tag is at the end of file, or MP3 * MP3 without ID3 tag, ID3v1 tag is at the end of file, or MP3
* with ID3 tag at the beginning of the file. * with ID3 tag at the beginning of the file.
*/ */
if((fileSig << 16) == 0xFBFF0000 || if(isMp3(file) == 0)
(fileSig << 16) == 0xFAFF0000 ||
(fileSig << 8) == 0x33444900)
{ {
file_type = FILE_TYPE_MP3; file_type = FILE_TYPE_MP3;
break; break;

View File

@@ -132,3 +132,60 @@ void exitMp3(void)
mpg123_delete(mh); mpg123_delete(mh);
mpg123_exit(); mpg123_exit();
} }
/**
* Check if a file is a valid MP3 file.
*
* \param path Path to the MP3 file.
* \return 0 if valid MP3, 1 if not valid.
*/
int isMp3(const char *path)
{
int err;
int result = 1;
mpg123_handle *mh = NULL;
long rate;
int channels, encoding;
// Initialise the library
if (mpg123_init() != MPG123_OK)
goto out;
// Create a decoder handle
mh = mpg123_new(NULL, &err);
if (!mh)
goto exit_init;
// Try opening the file
err = mpg123_open(mh, path);
if (err != MPG123_OK)
goto exit_handle;
// Query the decoded format
if (mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK)
goto close_handle;
// Parse first frame in file
err = mpg123_framebyframe_next(mh);
if (err != MPG123_OK)
{
// If we can't read the first frame, it's not a valid MP3
goto close_handle;
}
// All checks passed: valid MP3
result = 0;
close_handle:
mpg123_close(mh);
exit_handle:
mpg123_delete(mh);
exit_init:
mpg123_exit();
out:
return result;
}