/* * vlc.h * Copyright (C) 2000-2003 Michel Lespinasse * Copyright (C) 1999-2000 Aaron Holtzman * * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. * See http://libmpeg2.sourceforge.net/ for updates. * * mpeg2dec is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * mpeg2dec 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id$ * libmpeg2 sync history: * 2008-07-01 - CVS revision 1.12 */ #define GETWORD(bit_buf, shift, bit_ptr) \ do { \ bit_buf |= ((bit_ptr[0] << 8) | bit_ptr[1]) << (shift); \ bit_ptr += 2; \ } while (0) static inline void bitstream_init (mpeg2_decoder_t * decoder, const uint8_t * start) { decoder->bitstream_buf = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3]; decoder->bitstream_ptr = start + 4; decoder->bitstream_bits = -16; } /* make sure that there are at least 16 valid bits in bit_buf */ #define NEEDBITS(bit_buf, bits, bit_ptr) \ do { \ if (unlikely (bits > 0)) { \ GETWORD (bit_buf, bits, bit_ptr); \ bits -= 16; \ } \ } while (0) /* remove num valid bits from bit_buf */ #define DUMPBITS(bit_buf, bits, num) \ do { \ bit_buf <<= (num); \ bits += (num); \ } while (0) /* take num bits from the high part of bit_buf and zero extend them */ #define UBITS(bit_buf,num) (((uint32_t)(bit_buf)) >> (32 - (num))) /* take num bits from the high part of bit_buf and sign extend them */ #define SBITS(bit_buf,num) (((int32_t)(bit_buf)) >> (32 - (num))) typedef struct { uint8_t modes; uint8_t len; } MBtab; typedef struct { uint8_t delta; uint8_t len; } MVtab; typedef struct { int8_t dmv; uint8_t len; } DMVtab; typedef struct { uint8_t cbp; uint8_t len; } CBPtab; typedef struct { uint8_t size; uint8_t len; } DCtab; typedef struct { uint8_t run; uint8_t level; uint8_t len; } DCTtab; typedef struct { uint8_t mba; uint8_t len; } MBAtab; #define INTRA MACROBLOCK_INTRA #define QUANT MACROBLOCK_QUANT static const MBtab MB_I [] ICONST_ATTR = { {INTRA|QUANT, 2}, {INTRA, 1} }; #define MC MACROBLOCK_MOTION_FORWARD #define CODED MACROBLOCK_PATTERN static const MBtab MB_P [] ICONST_ATTR = { {INTRA|QUANT, 6}, {CODED|QUANT, 5}, {MC|CODED|QUANT, 5}, {INTRA, 5}, {MC, 3}, {MC, 3}, {MC, 3}, {MC, 3}, {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1} }; #define FWD MACROBLOCK_MOTION_FORWARD #define BWD MACROBLOCK_MOTION_BACKWARD #define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD static const MBtab MB_B [] ICONST_ATTR = { {0, 6}, {INTRA|QUANT, 6}, {BWD|CODED|QUANT, 6}, {FWD|CODED|QUANT, 6}, {INTER|CODED|QUANT, 5}, {INTER|CODED|QUANT, 5}, {INTRA, 5}, {INTRA, 5}, {FWD, 4}, {FWD, 4}, {FWD, 4}, {FWD, 4}, {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4}, {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2} }; #undef INTRA #undef QUANT #undef MC #undef CODED #undef FWD #undef BWD #undef INTER static const MVtab MV_4 [] ICONST_ATTR = { { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2} }; static const MVtab MV_10 [] ICONST_ATTR = { { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10}, {11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7} }; static const DMVtab DMV_2 [] ICONST_ATTR = { { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2} }; static const CBPtab CBP_7 [] ICONST_ATTR = { {0x11, 7}, {0x12, 7}, {0x14, 7}, {0x18, 7}, {0x21, 7}, {0x22, 7}, {0x24, 7}, {0x28, 7}, {0x3f, 6}, {0x3f, 6}, {0x30, 6}, {0x30, 6}, {0x09, 6}, {0x09, 6}, {0x06, 6}, {0x06, 6}, {0x1f, 5}, {0x1f, 5}, {0x1f, 5}, {0x1f, 5}, {0x10, 5}, {0x10, 5}, {0x10, 5}, {0x10, 5}, {0x2f, 5}, {0x2f, 5}, {0x2f, 5}, {0x2f, 5}, {0x20, 5}, {0x20, 5}, {0x20, 5}, {0x20, 5}, {0x07, 5}, {0x07, 5}, {0x07, 5}, {0x07, 5}, {0x0b, 5}, {0x0b, 5}, {0x0b, 5}, {0x0b, 5}, {0x0d, 5}, {0x0d, 5}, {0x0d, 5}, {0x0d, 5}, {0x0e, 5}, {0x0e, 5}, {0x0e, 5}, {0x0e, 5}, {0x05, 5}, {0x05, 5}, {0x05, 5}, {0x05, 5}, {0x0a, 5}, {0x0a, 5}, {0x0a, 5}, {0x0a, 5}, {0x03, 5}, {0x03, 5}, {0x03, 5}, {0x03, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x01, 4}, {0x01, 4}, {0x01, 4}, {0x01, 4}, {0x01, 4}, {0x01, 4}, {0x01, 4}, {0x01, 4}, {0x02, 4}, {0x02, 4}, {0x02, 4}, {0x02, 4}, {0x02, 4}, {0x02, 4}, {0x02, 4}, {0x02, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3}, {0x0f, 3} }; static const CBPtab CBP_9 [] ICONST_ATTR = { {0, 9}, {0x00, 9}, {0x39, 9}, {0x36, 9}, {0x37, 9}, {0x3b, 9}, {0x3d, 9}, {0x3e, 9}, {0x17, 8}, {0x17, 8}, {0x1b, 8}, {0x1b, 8}, {0x1d, 8}, {0x1d, 8}, {0x1e, 8}, {0x1e, 8}, {0x27, 8}, {0x27, 8}, {0x2b, 8}, {0x2b, 8}, {0x2d, 8}, {0x2d, 8}, {0x2e, 8}, {0x2e, 8}, {0x19, 8}, {0x19, 8}, {0x16, 8}, {0x16, 8}, {0x29, 8}, {0x29, 8}, {0x26, 8}, {0x26, 8}, {0x35, 8}, {0x35, 8}, {0x3a, 8}, {0x3a, 8}, {0x33, 8}, {0x33, 8}, {0x3c, 8}, {0x3c, 8}, {0x15, 8}, {0x15, 8}, {0x1a, 8}, {0x1a, 8}, {0x13, 8}, {0x13, 8}, {0x1c, 8}, {0x1c, 8}, {0x25, 8}, {0x25, 8}, {0x2a, 8}, {0x2a, 8}, {0x23, 8}, {0x23, 8}, {0x2c, 8}, {0x2c, 8}, {0x31, 8}, {0x31, 8}, {0x32, 8}, {0x32, 8}, {0x34, 8}, {0x34, 8}, {0x38, 8}, {0x38, 8} }; static const DCtab DC_lum_5 [] ICONST_ATTR = { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5} }; static const DCtab DC_chrom_5 [] ICONST_ATTR = { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5} }; static const DCtab DC_long [] ICONST_ATTR = { {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6}, {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9} }; static const DCTtab DCT_16 [] ICONST_ATTR = { {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, { 2,18, 0}, { 2,17, 0}, { 2,16, 0}, { 2,15, 0}, { 7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0}, { 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0}, { 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0} }; static const DCTtab DCT_15 [] ICONST_ATTR = { { 1,40,15}, { 1,39,15}, { 1,38,15}, { 1,37,15}, { 1,36,15}, { 1,35,15}, { 1,34,15}, { 1,33,15}, { 1,32,15}, { 2,14,15}, { 2,13,15}, { 2,12,15}, { 2,11,15}, { 2,10,15}, { 2, 9,15}, { 2, 8,15}, { 1,31,14}, { 1,31,14}, { 1,30,14}, { 1,30,14}, { 1,29,14}, { 1,29,14}, { 1,28,14}, { 1,28,14}, { 1,27,14}, { 1,27,14}, { 1,26,14}, { 1,26,14}, { 1,25,14}, { 1,25,14}, { 1,24,14}, { 1,24,14}, { 1,23,14}, { 1,23,14}, { 1,22,14}, { 1,22,14}, { 1,21,14}, { 1,21,14}, { 1,20,14}, { 1,20,14}, { 1,19,14}, { 1,19,14}, { 1,18,14}, { 1,18,14}, { 1,17,14}, { 1,17,14}, { 1,16,14}, { 1,16,14} }; static const DCTtab DCT_13 [] ICONST_ATTR = { { 11, 2,13}, { 10, 2,13}, { 6, 3,13}, { 4, 4,13}, { 3, 5,13}, { 2, 7,13}, { 2, 6,13}, { 1,15,13}, { 1,14,13}, { 1,13,13}, { 1,12,13}, { 27, 1,13}, { 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13}, { 1,11,12}, { 1,11,12}, { 9, 2,12}, { 9, 2,12}, { 5, 3,12}, { 5, 3,12}, { 1,10,12}, { 1,10,12}, { 3, 4,12}, { 3, 4,12}, { 8, 2,12}, { 8, 2,12}, { 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12}, { 1, 9,12}, { 1, 9,12}, { 20, 1,12}, { 20, 1,12}, { 19, 1,12}, { 19, 1,12}, { 2, 5,12}, { 2, 5,12}, { 4, 3,12}, { 4, 3,12}, { 1, 8,12}, { 1, 8,12}, { 7, 2,12}, { 7, 2,12}, { 18, 1,12}, { 18, 1,12} }; static const DCTtab DCT_B14_10 [] ICONST_ATTR = { { 17, 1,10}, { 6, 2,10}, { 1, 7,10}, { 3, 3,10}, { 2, 4,10}, { 16, 1,10}, { 15, 1,10}, { 5, 2,10} }; static const DCTtab DCT_B14_8 [] ICONST_ATTR = { { 65, 0, 12}, { 65, 0, 12}, { 65, 0, 12}, { 65, 0, 12}, { 3, 2, 7}, { 3, 2, 7}, { 10, 1, 7}, { 10, 1, 7}, { 1, 4, 7}, { 1, 4, 7}, { 9, 1, 7}, { 9, 1, 7}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 14, 1, 8}, { 1, 6, 8}, { 13, 1, 8}, { 12, 1, 8}, { 4, 2, 8}, { 2, 3, 8}, { 1, 5, 8}, { 11, 1, 8} }; static const DCTtab DCT_B14AC_5 [] ICONST_ATTR = { { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5}, { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2} }; static const DCTtab DCT_B14DC_5 [] ICONST_ATTR = { { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5}, { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1} }; static const DCTtab DCT_B15_10 [] ICONST_ATTR = { { 6, 2, 9}, { 6, 2, 9}, { 15, 1, 9}, { 15, 1, 9}, { 3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9} }; static const DCTtab DCT_B15_8 [] ICONST_ATTR = { { 65, 0, 12}, { 65, 0, 12}, { 65, 0, 12}, { 65, 0, 12}, { 8, 1, 7}, { 8, 1, 7}, { 9, 1, 7}, { 9, 1, 7}, { 7, 1, 7}, { 7, 1, 7}, { 3, 2, 7}, { 3, 2, 7}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 2, 5, 8}, { 12, 1, 8}, { 1,11, 8}, { 1,10, 8}, { 14, 1, 8}, { 13, 1, 8}, { 4, 2, 8}, { 2, 4, 8}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 10, 1, 7}, { 10, 1, 7}, { 2, 3, 7}, { 2, 3, 7}, { 11, 1, 7}, { 11, 1, 7}, { 1, 8, 7}, { 1, 8, 7}, { 1, 9, 7}, { 1, 9, 7}, { 1,12, 8}, { 1,13, 8}, { 3, 3, 8}, { 5, 2, 8}, { 1,14, 8}, { 1,15, 8} }; static const MBAtab MBA_5 [] ICONST_ATTR = { {6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1} }; static const MBAtab MBA_11 [] ICONST_ATTR = { {32, 11}, {31, 11}, {30, 11}, {29, 11}, {28, 11}, {27, 11}, {26, 11}, {25, 11}, {24, 11}, {23, 11}, {22, 11}, {21, 11}, {20, 10}, {20, 10}, {19, 10}, {19, 10}, {18, 10}, {18, 10}, {17, 10}, {17, 10}, {16, 10}, {16, 10}, {15, 10}, {15, 10}, {14, 8}, {14, 8}, {14, 8}, {14, 8}, {14, 8}, {14, 8}, {14, 8}, {14, 8}, {13, 8}, {13, 8}, {13, 8}, {13, 8}, {13, 8}, {13, 8}, {13, 8}, {13, 8}, {12, 8}, {12, 8}, {12, 8}, {12, 8}, {12, 8}, {12, 8}, {12, 8}, {12, 8}, {11, 8}, {11, 8}, {11, 8}, {11, 8}, {11, 8}, {11, 8}, {11, 8}, {11, 8}, {10, 8}, {10, 8}, {10, 8}, {10, 8}, {10, 8}, {10, 8}, {10, 8}, {10, 8}, { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7} }; 49 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 Jonathan Gordon
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "plugin.h"
#include "file.h"



static bool cancel;
static int fd;
static int dirs_count;
static int lasttick;
#define RFA_FILE ROCKBOX_DIR "/folder_advance_list.dat"
#define RFADIR_FILE ROCKBOX_DIR "/folder_advance_dir.txt"
#define RFA_FILE_TEXT ROCKBOX_DIR "/folder_advance_list.txt"
#define MAX_REMOVED_DIRS 10

char *buffer = NULL;
size_t buffer_size;
int num_replaced_dirs = 0;
char removed_dirs[MAX_REMOVED_DIRS][MAX_PATH];
struct file_format {
    int count;
    char folder[][MAX_PATH];
};
struct file_format *list = NULL;

void update_screen(bool clear)
{
    char buf[15];
    int i;

    rb->snprintf(buf,sizeof(buf),"Folders: %d",dirs_count);
    FOR_NB_SCREENS(i)
    {
        if(clear)
            rb->screens[i]->clear_display();
        rb->screens[i]->putsxy(0,0,buf);
        rb->screens[i]->update();
    }
}

void traversedir(char* location, char* name)
{
    struct dirent *entry;
    DIR* dir;
    char fullpath[MAX_PATH], path[MAX_PATH];
    bool check = false;
    int i;

    rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
    dir = rb->opendir(fullpath);
    if (dir) {
        entry = rb->readdir(dir);
        while (entry) {
            if (cancel)
                break;
            /* Skip .. and . */
            if (entry->d_name[0] == '.')
            {
                if (    !rb->strcmp(entry->d_name,".")
                     || !rb->strcmp(entry->d_name,"..")
                     || !rb->strcmp(entry->d_name,".rockbox"))
                    check = false;
                else check = true;
            }
            else check = true;

        /* check if path is removed directory, if so dont enter it */
        rb->snprintf(path, MAX_PATH, "%s/%s", fullpath, entry->d_name);
        while(path[0] == '/')
            rb->strlcpy(path, path + 1, sizeof(path));
        for(i = 0; i < num_replaced_dirs; i++)
        {
            if(!rb->strcmp(path, removed_dirs[i]))
            {
                check = false;
                break;
            }
        }

            if (check)
            {
                struct dirinfo info = rb->dir_get_info(dir, entry);
                if (info.attribute & ATTR_DIRECTORY) {
                    char *start;
                    dirs_count++;
                    rb->snprintf(path,MAX_PATH,"%s/%s",fullpath,entry->d_name);
                    start = &path[rb->strlen(path)];
                    rb->memset(start,0,&path[MAX_PATH-1]-start);
                    rb->write(fd,path,MAX_PATH);
                    traversedir(fullpath, entry->d_name);
                }
            }
            if (*rb->current_tick - lasttick > (HZ/2)) {
                update_screen(false);
                lasttick = *rb->current_tick;
                if (rb->action_userabort(TIMEOUT_NOBLOCK))
                {
                    cancel = true;
                    break;
                }
            }

            entry = rb->readdir(dir);
        }
        rb->closedir(dir);
    }
}

bool custom_dir(void)
{
    DIR* dir_check;
    char *starts, line[MAX_PATH], formatted_line[MAX_PATH];
    static int fd2;
    char buf[11];
    int i, errors = 0;

    /* populate removed dirs array */
    if((fd2 = rb->open(RFADIR_FILE,O_RDONLY)) >= 0)
    {
        while ((rb->read_line(fd2, line, MAX_PATH - 1)) > 0)
        {
            if ((line[0] == '-') && (line[1] == '/') &&
                     (num_replaced_dirs < MAX_REMOVED_DIRS))
            {
                num_replaced_dirs ++;
                rb->strlcpy(removed_dirs[num_replaced_dirs - 1], line + 2,
                                sizeof(line));
            }
        }
        rb->close(fd2);
    }

    if((fd2 = rb->open(RFADIR_FILE,O_RDONLY)) >= 0)
    {
        while ((rb->read_line(fd2, line, MAX_PATH - 1)) > 0)
        {
            /* blank lines and removed dirs ignored */
            if (rb->strlen(line) && ((line[0] != '-') || (line[1] != '/')))
            {
                /* remove preceeding '/'s from the line */
                while(line[0] == '/')
                    rb->strlcpy(line, line + 1, sizeof(line));

                rb->snprintf(formatted_line, MAX_PATH, "/%s", line);

                dir_check = rb->opendir(formatted_line);

                if (dir_check)
                {
                    rb->closedir(dir_check);
                    starts = &formatted_line[rb->strlen(formatted_line)];
                    rb->memset(starts, 0, &formatted_line[MAX_PATH-1]-starts);
                    bool write_line = true;

                    for(i = 0; i < num_replaced_dirs; i++)
                    {
                        if(!rb->strcmp(line, removed_dirs[i]))
                        {
                             write_line = false;
                             break;
                        }
                    }

                    if(write_line)
                    {
                        dirs_count++;
                        rb->write(fd, formatted_line, MAX_PATH);
                    }

                    traversedir("", line);
                }
                else
                {
                     errors ++;
                     rb->snprintf(buf,sizeof(buf),"Not found:");
                     FOR_NB_SCREENS(i)
                     {
                         rb->screens[i]->puts(0,0,buf);
                         rb->screens[i]->puts(0, errors, line);
                     }
                     update_screen(false);
                }
            }
        }
        rb->close(fd2);
        if(errors)
            /* Press button to continue */
            rb->get_action(CONTEXT_STD, TIMEOUT_BLOCK); 
    }
    else
        return false;
    return true;
}

void generate(void)
{
    dirs_count = 0;
    cancel = false;
    fd = rb->open(RFA_FILE,O_CREAT|O_WRONLY, 0666);
    rb->write(fd,&dirs_count,sizeof(int));
    if (fd < 0)
    {
        rb->splashf(HZ, "Couldnt open %s", RFA_FILE);
        return;
    }
#ifndef HAVE_LCD_CHARCELLS
    update_screen(true);
#endif
    lasttick = *rb->current_tick;

    if(!custom_dir())
        traversedir("", "");

    rb->lseek(fd,0,SEEK_SET);
    rb->write(fd,&dirs_count,sizeof(int));
    rb->close(fd);
    rb->splash(HZ, "Done");
}

static const char* list_get_name_cb(int selected_item, void* data,
                                    char* buf, size_t buf_len)
{
    (void)data;
    rb->strlcpy(buf, list->folder[selected_item], buf_len);
    return buf;
}

int load_list(void)
{
    int myfd = rb->open(RFA_FILE,O_RDONLY);
    if (myfd < 0)
        return -1;
    buffer = rb->plugin_get_audio_buffer(&buffer_size);
    if (!buffer)
    {
        return -2;
    }
    
    rb->read(myfd,buffer,buffer_size);
    rb->close(myfd);
    list = (struct file_format *)buffer;
    
    return 0;
}

int save_list(void)
{
    int myfd = rb->creat(RFA_FILE, 0666);
    if (myfd < 0)
    {
        rb->splash(HZ, "Could Not Open " RFA_FILE);
        return -1;
    }
    int dirs_count = 0, i = 0;
    rb->write(myfd,&dirs_count,sizeof(int));
    for ( ;i<list->count;i++)
    {
        if (list->folder[i][0] != ' ')
        {
            dirs_count++;
            rb->write(myfd,list->folder[i],MAX_PATH);
        }
    }
    rb->lseek(myfd,0,SEEK_SET);
    rb->write(myfd,&dirs_count,sizeof(int));
    rb->close(myfd);
    
    return 1;
}

int edit_list(void)
{
    struct gui_synclist lists;
    bool exit = false;
    int button,i;
    int selection, ret = 0;
    
    /* load the dat file if not already done */
    if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
    {
        rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
        return -1;
    }
    
    dirs_count = list->count;
    
    rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1, NULL);
    rb->gui_synclist_set_icon_callback(&lists,NULL);
    rb->gui_synclist_set_nb_items(&lists,list->count);
    rb->gui_synclist_limit_scroll(&lists,true);
    rb->gui_synclist_select_item(&lists, 0);
    
    while (!exit)
    {
        rb->gui_synclist_draw(&lists);
        button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
        if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
            continue;
        selection = rb->gui_synclist_get_sel_pos(&lists);
        switch (button)
        {
            case ACTION_STD_OK:
                list->folder[selection][0] = ' ';
                list->folder[selection][1] = '\0';
                break;
            case ACTION_STD_CONTEXT:
            {
                int len;
                MENUITEM_STRINGLIST(menu, "Remove Menu", NULL,
                                    "Remove Folder", "Remove Folder Tree");

                switch (rb->do_menu(&menu, NULL, NULL, false))
                {
                    case 0:
                        list->folder[selection][0] = ' ';
                        list->folder[selection][1] = '\0';
                        break;
                    case 1:
                    {
                        char temp[MAX_PATH];
                        rb->strcpy(temp,list->folder[selection]);
                        len = rb->strlen(temp);
                        for (i=0;i<list->count;i++)
                        {
                            if (!rb->strncmp(list->folder[i],temp,len))
                            {
                                list->folder[i][0] = ' ';
                                list->folder[i][1] = '\0';
                            }
                        }
                    }
                    break;
                }
            }
            break;
            case ACTION_STD_CANCEL:
            {
                MENUITEM_STRINGLIST(menu, "Exit Menu", NULL,
                                    "Save and Exit", "Ignore Changes and Exit");

                switch (rb->do_menu(&menu, NULL, NULL, false))
                {
                    case 0:
                        save_list();
                    case 1:
                        exit = true;
                        ret = -2;
                }
            }
            break;
        }
    }
    return ret;
}

int export_list_to_file_text(void)
{
    int i = 0;
    /* load the dat file if not already done */
    if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
    {
        rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
        return 0;
    }
        
    if (list->count <= 0)
    {
        rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE);
        return 0;
    }
        
    /* create and open the file */
    int myfd = rb->creat(RFA_FILE_TEXT, 0666);
    if (myfd < 0)
    {
        rb->splashf(HZ*4, "failed to open: fd = %d, file = %s", 
            myfd, RFA_FILE_TEXT);
        return -1;
    }
    
    /* write each directory to file */
    for (i = 0; i < list->count; i++)
    {
        if (list->folder[i][0] != ' ')
        {
            rb->fdprintf(myfd, "%s\n", list->folder[i]);
        }
    }
    
    rb->close(myfd);
    rb->splash(HZ, "Done");
    return 1;
}

int import_list_from_file_text(void)
{
    char line[MAX_PATH];
    
    buffer = rb->plugin_get_audio_buffer(&buffer_size);
    if (buffer == NULL)
    {
        rb->splash(HZ*2, "failed to get audio buffer");
        return -1;
    }

    int myfd = rb->open(RFA_FILE_TEXT, O_RDONLY);
    if (myfd < 0)
    {
        rb->splashf(HZ*2, "failed to open: %s", RFA_FILE_TEXT);
        return -1;
    }
    
    /* set the list structure, and initialize count */
    list = (struct file_format *)buffer;
    list->count = 0;
        
    while ((rb->read_line(myfd, line, MAX_PATH - 1)) > 0)
    {
        /* copy the dir name, and skip the newline */
        int len = rb->strlen(line);
        /* remove CRs */
        if (len > 0)
        {
            if (line[len-1] == 0x0A || line[len-1] == 0x0D)
                line[len-1] = 0x00;
            if (len > 1 && 
                (line[len-2] == 0x0A || line[len-2] == 0x0D))
                line[len-2] = 0x00;
        }
        
        rb->strcpy(list->folder[list->count++], line);
    }
    
    rb->close(myfd);
    
    if (list->count == 0)
    {
        load_list();
    }
    else
    {
        save_list();
    }
    rb->splash(HZ, "Done");
    return list->count;
}

int start_shuffled_play(void)
{
    int *order;
    size_t max_shuffle_size;
    int i = 0;

    /* get memory for shuffling */
    order=rb->plugin_get_buffer(&max_shuffle_size);
    max_shuffle_size/=sizeof(int);
    if (order==NULL || max_shuffle_size==0)
    {
        rb->splashf(HZ*2, "Not enough memory for shuffling");
        return 0;
    }
 
    /* load the dat file if not already done */
    if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
    {
        rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
        return 0;
    }
        
    if (list->count <= 0)
    {
        rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE);
        return 0;
    }

    /* shuffle the thing */
    rb->srand(*rb->current_tick);
    if(list->count>(int)max_shuffle_size)
    {
        rb->splashf(HZ*2, "Too many folders: %d (room for %d)", list->count,(int)max_shuffle_size);
        return 0;
    }
    for(i=0;i<list->count;i++)
        order[i]=i;
    
    for(i = list->count - 1; i >= 0; i--)
    {
        /* the rand is from 0 to RAND_MAX, so adjust to our value range */
        int candidate = rb->rand() % (i + 1);

        /* now swap the values at the 'i' and 'candidate' positions */
        int store = order[candidate];
        order[candidate] = order[i];
        order[i] = store;
    }
        
    /* We don't want whatever is playing */
    if (!(rb->playlist_remove_all_tracks(NULL) == 0
          && rb->playlist_create(NULL, NULL) == 0))
    {
        rb->splashf(HZ*2, "Could not clear playlist");
        return 0;
    }

    /* add the lot to the playlist */
    for (i = 0; i < list->count; i++)
    {
        if (list->folder[order[i]][0] != ' ')
        {
            rb->playlist_insert_directory(NULL,list->folder[order[i]],PLAYLIST_INSERT_LAST,false,false);
        }
        if (rb->action_userabort(TIMEOUT_NOBLOCK))
        {
           break;
        }
    }
    rb->splash(HZ, "Done");
    rb->playlist_start(0,0);
    return 1;
}

enum plugin_status main_menu(void)
{
    bool exit = false;
    MENUITEM_STRINGLIST(menu, "Main Menu", NULL,
                        "Generate Folder List",
                        "Edit Folder List",
                        "Export List To Textfile",
                        "Import List From Textfile",
                        "Play Shuffled",
                        "Quit");

    while (!exit)
    {
        switch (rb->do_menu(&menu, NULL, NULL, false))
        {
            case 0: /* generate */
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
                rb->cpu_boost(true);
#endif
                generate();
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
                rb->cpu_boost(false);
#endif
#ifdef HAVE_REMOTE_LCD
                rb->remote_backlight_on();
#endif
                rb->backlight_on();
                break;
            case 1:
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
                rb->cpu_boost(true);
#endif
                if (edit_list() < 0)
                    exit = true;
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
                rb->cpu_boost(false);
#endif
#ifdef HAVE_REMOTE_LCD
                rb->remote_backlight_on();
#endif
                rb->backlight_on();
                break;
            case 2: /* export to textfile */
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
                rb->cpu_boost(true);
#endif
                export_list_to_file_text();
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
                rb->cpu_boost(false);
#endif
#ifdef HAVE_REMOTE_LCD
                rb->remote_backlight_on();
#endif
                rb->backlight_on();
                break;
            case 3: /* import from textfile */
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
                rb->cpu_boost(true);
#endif
                import_list_from_file_text();
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
                rb->cpu_boost(false);
#endif
#ifdef HAVE_REMOTE_LCD
                rb->remote_backlight_on();
#endif
                rb->backlight_on();
                break;
            case 4:
                if (!start_shuffled_play())
                    return PLUGIN_ERROR;
                else
                    return PLUGIN_GOTO_WPS;
            case 5:
                return PLUGIN_OK;
        }
    }
    return PLUGIN_OK;
}

enum plugin_status plugin_start(const void* parameter)
{
    (void)parameter;
#ifdef HAVE_TOUCHSCREEN
    rb->touchscreen_set_mode(rb->global_settings->touch_mode);
#endif

    cancel = false;
    
    return main_menu();
}