OGG: Support reading COVERART fields

This commit is contained in:
Serial 2023-09-30 12:35:28 -04:00 committed by Alex
parent 51517d3bd1
commit 15d9ca812c
3 changed files with 36 additions and 2 deletions

View file

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **MP4**:
- `Atom::into_data`
- `Atom::merge`
- **OGG**: Support for reading "COVERART" fields, an old deprecated image storage format.
## Changed
- **ID3v2**:

View file

@ -2,11 +2,13 @@ use super::tag::VorbisComments;
use super::verify_signature;
use crate::error::{ErrorKind, LoftyError, Result};
use crate::macros::{decode_err, err, parse_mode_choice};
use crate::picture::Picture;
use crate::picture::{MimeType, Picture, PictureInformation, PictureType};
use crate::probe::ParsingMode;
use std::borrow::Cow;
use std::io::{Read, Seek, SeekFrom};
use base64::Engine;
use byteorder::{LittleEndian, ReadBytesExt};
use ogg_pager::{Packets, PageHeader};
@ -104,6 +106,37 @@ where
},
}
},
k if k.eq_ignore_ascii_case(b"COVERART") => {
// `COVERART` is an old deprecated image storage format. We have to convert it
// to a `METADATA_BLOCK_PICTURE` for it to be useful.
//
// <https://wiki.xiph.org/VorbisComment#Conversion_to_METADATA_BLOCK_PICTURE>
let picture_data = base64::engine::general_purpose::STANDARD.decode(value);
match picture_data {
Ok(picture_data) => {
let mime_type = Picture::mimetype_from_bin(&picture_data)
.unwrap_or_else(|_| MimeType::Unknown(String::from("image/")));
let picture = Picture {
pic_type: PictureType::Other,
mime_type,
description: None,
data: Cow::from(picture_data),
};
tag.pictures.push((picture, PictureInformation::default()))
},
Err(_) => {
if parse_mode == ParsingMode::Strict {
return Err(LoftyError::new(ErrorKind::NotAPicture));
}
log::warn!("Failed to decode FLAC picture, discarding field");
continue;
},
}
},
// The valid range is 0x20..=0x7D not including 0x3D
k if k.iter().all(|c| (b' '..=b'}').contains(c) && *c != b'=') => {
// SAFETY: We just verified that all of the bytes fall within the subset of ASCII

View file

@ -784,7 +784,7 @@ impl Picture {
})
}
fn mimetype_from_bin(bytes: &[u8]) -> Result<MimeType> {
pub(crate) fn mimetype_from_bin(bytes: &[u8]) -> Result<MimeType> {
match bytes[..8] {
[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A] => Ok(MimeType::Png),
[0xFF, 0xD8, ..] => Ok(MimeType::Jpeg),