/*
 * libkysdk-system's Library
 *
 * Copyright (C) 2025, KylinSoft Co., Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: ShaoShuai Tian <tianshaoshuai@kylinos.cn>
 *
 */

/* From the mplayer project (www.mplayerhq.hu) */

#ifndef __BSWAP_H__
#define __BSWAP_H__

//#include "config-k3b.h"

#define ENABLE_DVD_RIPPING
/* #undef ENABLE_MUSICBRAINZ */
#define ENABLE_TAGLIB
#define HAVE_SYS_VFS_H
#define HAVE_SYS_STATVFS_H
#define HAVE_STAT64
#define K3B_VERSION_STRING "19.12.3"
/* #undef ENABLE_HAL_SUPPORT */
/* #undef ENABLE_AUDIO_PLAYER */
#define HAVE_BYTESWAP_H
/* #undef WORDS_BIGENDIAN */
#define HAVE_ICONV
/* #undef ICONV_SECOND_ARGUMENT_IS_CONST */
#define HAVE_FFMPEG_AVFORMAT_OPEN_INPUT
#define HAVE_FFMPEG_AV_DUMP_FORMAT
#define HAVE_FFMPEG_AVFORMAT_FIND_STREAM_INFO
#define HAVE_FFMPEG_AVFORMAT_CLOSE_INPUT
#define HAVE_FFMPEG_AVCODEC_OPEN2
/* #undef HAVE_FFMPEG_AVCODEC_DECODE_AUDIO2 */
/* #undef HAVE_FFMPEG_AVCODEC_DECODE_AUDIO3 */
#define HAVE_FFMPEG_AVCODEC_DECODE_AUDIO4
#define HAVE_FFMPEG_AVMEDIA_TYPE
/* #undef HAVE_FFMPEG_CODEC_MP3 */


#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#else

#ifdef ARCH_X86
inline static unsigned short ByteSwap16(unsigned short x)
{
  __asm("xchgb %b0,%h0"	:
        "=q" (x)	:
        "0" (x));
    return x;
}
#define bswap_16(x) ByteSwap16(x)

inline static unsigned int ByteSwap32(unsigned int x)
{
#if defined(__CPU__) && (__CPU__ > 386)
 __asm("bswap	%0":
      "=r" (x)     :
#else
 __asm("xchgb	%b0,%h0\n"
      "	rorl	$16,%0\n"
      "	xchgb	%b0,%h0":
      "=q" (x)		:
#endif
      "0" (x));
  return x;
}
#define bswap_32(x) ByteSwap32(x)

inline static unsigned long long int ByteSwap64(unsigned long long int x)
{
  register union { __extension__ unsigned long long int __ll;
          unsigned int __l[2]; } __x;
  asm("xchgl	%0,%1":
      "=r"(__x.__l[0]),"=r"(__x.__l[1]):
      "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
  return __x.__ll;
}
#define bswap_64(x) ByteSwap64(x)

#else

#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
			

/* code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc. */
#define bswap_32(x) \
     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))

#define bswap_64(x) \
     (__extension__						\
      ({ union { __extension__ unsigned long long int __ll;	\
                 unsigned int __l[2]; } __w, __r;		\
         __w.__ll = (x);					\
         __r.__l[0] = bswap_32 (__w.__l[1]);			\
         __r.__l[1] = bswap_32 (__w.__l[0]);			\
         __r.__ll; }))
#endif	/* !ARCH_X86 */

#endif	/* !HAVE_BYTESWAP_H */

/*
 be2me ... BigEndian to MachineEndian
 le2me ... LittleEndian to MachineEndian
*/

#ifdef WORDS_BIGENDIAN
#define be2me_16(x) (x)
#define be2me_32(x) (x)
#define be2me_64(x) (x)
#define le2me_16(x) bswap_16(x)
#define le2me_32(x) bswap_32(x)
#define le2me_64(x) bswap_64(x)
#else
#define be2me_16(x) bswap_16(x)
#define be2me_32(x) bswap_32(x)
#define be2me_64(x) bswap_64(x)
#define le2me_16(x) (x)
#define le2me_32(x) (x)
#define le2me_64(x) (x)
#endif

#endif
