[FFmpeg-devel] [PATCH v2 6/6] avformat/subtitles: guess UTF-16 if no BOM present

Clément Bœsch u at pkh.me
Fri Sep 5 23:24:52 CEST 2014


On Thu, Sep 04, 2014 at 10:40:27PM +0200, wm4 wrote:
> ---
> Follows reimar's suggestion for detecting UTF-16. If the detection goes
> wrong, probing the format won't succeed, so this should not break
> anything.
> 
> I didn't use ffio_ensure_seekback), because this apparently reallocates
> the buffer - which we certainly don't want, because the probe API
> doesn't allow this.
> 
> Probably too messy, feel free to drop.
> ---
>  libavformat/subtitles.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
> index 3069477..7d6a93c 100644
> --- a/libavformat/subtitles.c
> +++ b/libavformat/subtitles.c
> @@ -26,6 +26,7 @@
>  
>  void ff_text_init_avio(FFTextReader *r, AVIOContext *pb)
>  {
> +    int can_seekback_2_bytes = pb->buf_end - pb->buf_ptr >= 2;
>      int i;
>      r->pb = pb;
>      r->buf_pos = r->buf_len = 0;
> @@ -38,6 +39,14 @@ void ff_text_init_avio(FFTextReader *r, AVIOContext *pb)
>      } else if (strncmp("\xFE\xFF", r->buf, 2) == 0) {
>          r->type = FF_UTF16BE;
>          r->buf_pos += 2;
> +    } else if (can_seekback_2_bytes && r->buf[0] && !r->buf[1]) {
> +        r->type = FF_UTF16LE; // with high probability
> +        r->pb->buf_ptr -= 2;
> +        r->buf_pos = r->buf_len = 0;
> +    } else if (can_seekback_2_bytes && !r->buf[0] && r->buf[1]) {
> +        r->type = FF_UTF16BE; // with high probability
> +        r->pb->buf_ptr -= 2;
> +        r->buf_pos = r->buf_len = 0;
>      } else {
>          r->buf[r->buf_len++] = avio_r8(r->pb);
>          if (strncmp("\xEF\xBB\xBF", r->buf, 3) == 0) {

I left this one for Reimar or someone else to review. I'm not familiar
enough with the avio internals.

Note: that change is pretty useful for testing since iconv doesn't look to
be able to add a BOM by itself.

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 473 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140905/6959d918/attachment.asc>


More information about the ffmpeg-devel mailing list