/* Gstreamer
 * Copyright (C) 2020 Yeongjin Jeong <yeongjin.jeong@navercorp.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <gst/check/gstcheck.h>
#include <gst/codecparsers/gstvp9parser.h>

/* A super frame data */
static const guint8 vp9_frame_data[] = {
  0x84, 0x00, 0x40, 0x84, 0x60, 0x1f, 0xe0, 0x11, 0xfc, 0x86, 0x23, 0x30, 0x00,
  0x02, 0x80, 0x70, 0x69, 0x60, 0x00, 0x00, 0x7e, 0x3f, 0x01, 0x60, 0xce,
  0xa0, 0x11, 0xbc, 0x02, 0x76, 0xf0, 0xd0, 0xb8, 0x2d, 0xb6, 0x1a, 0xc1,
  0xff, 0x36, 0x8d, 0xad, 0x1d, 0xde, 0x8e, 0x29, 0x47, 0xdd, 0x39, 0x65,
  0xf3, 0xf9, 0x45, 0xbe, 0xdb, 0x5b, 0xee, 0xe7, 0x36, 0x83, 0xe6, 0xaa,
  0xba, 0xf3, 0x2b, 0xe7, 0xab, 0xda, 0x07, 0xa3, 0xf5, 0x06, 0x7a, 0x19,
  0xdf, 0x37, 0x0f, 0x59, 0xae, 0x01, 0x63, 0x41, 0xf3, 0x48, 0x8a, 0x8c,
  0xb4, 0x47, 0x99, 0xe5, 0xf0, 0x48, 0x31, 0xd5, 0x2d, 0xd3, 0x01, 0x9d,
  0x87, 0xf1, 0x19, 0x63, 0xf7, 0x7e, 0xc4, 0x12, 0xa8, 0x85, 0xf3, 0x1d,
  0xd9, 0xc6, 0xcd, 0x61, 0x31, 0xec, 0x09, 0x6d, 0xfc, 0x96, 0x57, 0x26,
  0x70, 0xcc, 0xe4, 0x9e, 0x31, 0x05, 0x40, 0xa6, 0xc0, 0x2b, 0x44, 0x7b,
  0x80, 0xf7, 0x30, 0xdc, 0xa3, 0xcc, 0x88, 0xf1, 0x0b, 0x0f, 0x6c, 0xe9,
  0x85, 0xe8, 0x1c, 0xf9, 0x3f, 0xff, 0x46, 0xa3, 0x39, 0xa5, 0xab, 0x03,
  0x20, 0xad, 0x42, 0xdb, 0xb0, 0x84, 0x39, 0xff, 0xc6, 0xf7, 0xe8, 0x3e,
  0xe4, 0x0e, 0x03, 0x29, 0xc0, 0x8d, 0xbf, 0xeb, 0xfd, 0x8c, 0x6b, 0x17,
  0x49, 0x66, 0xfa, 0xab, 0xfc, 0xb3, 0x23, 0xae, 0xad, 0x92, 0xa2, 0x8b,
  0xff, 0xec, 0x22, 0x7c, 0xc4, 0x08, 0xdc, 0x85, 0xad, 0x9f, 0x63, 0x62,
  0x75, 0x05, 0x32, 0x84, 0x19, 0x61, 0xc0, 0x8f, 0x1f, 0x25, 0xd4, 0x3c,
  0x7a, 0x4d, 0x03, 0x0c, 0xfa, 0xb6, 0x04, 0xad, 0x3f, 0xa7, 0x0f, 0x5a,
  0x08, 0xda, 0xc0, 0xd4, 0x1b, 0xf3, 0x64, 0xc9, 0xaa, 0xe6, 0x97, 0x49,
  0x17, 0xe6, 0x16, 0xaa, 0x7f, 0x0a, 0x35, 0xed, 0xac, 0x87, 0x9e, 0x16,
  0xea, 0x08, 0x05, 0xa4, 0xd5, 0xdd, 0x19, 0xc5, 0x8c, 0x3c, 0x6d, 0x79,
  0xe2, 0x05, 0x0f, 0x08, 0x54, 0x6d, 0x10, 0xda, 0x07, 0x92, 0x9b, 0xb5,
  0x29, 0xec, 0x8c, 0xeb, 0xc6, 0xcb, 0xd7, 0xff, 0x66, 0x37, 0x18, 0x81,
  0x53, 0x4d, 0x1c, 0xc3, 0x13, 0xdd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f,
  0xd8, 0x8c, 0xac, 0xd1, 0x4d, 0x79, 0xc6, 0x45, 0xa6, 0xb9, 0x69, 0x29,
  0x4a, 0xd3, 0xa5, 0x8d, 0xd5, 0xed, 0x3f, 0x78, 0xe8, 0x9b, 0x4e, 0xff,
  0xda, 0xb1, 0x9f, 0x13, 0x53, 0x11, 0x00, 0xb5, 0x00, 0x36, 0x00, 0xc0,
  0xe2, 0x45, 0x29, 0x4c, 0xe9, 0xd6, 0x7f, 0xd6, 0xc6, 0x2e, 0x12, 0x6f,
  0xea, 0xeb, 0xcc, 0xae, 0x89, 0x05, 0xbe, 0x6b, 0x6e, 0x6a, 0x8a, 0x73,
  0xd5, 0xba, 0x28, 0x66, 0x3e, 0x35, 0xdc, 0x1f, 0x77, 0x30, 0x74, 0x52,
  0x80, 0x51, 0x6e, 0xaa, 0x69, 0x2c, 0x4a, 0x5c, 0x69, 0x3d, 0x40, 0x7b,
  0x34, 0x1e, 0xe0, 0xd7, 0xf2, 0xf8, 0x8e, 0x27, 0xa1, 0xb2, 0x49, 0x82,
  0x46, 0x4b, 0x61, 0x13, 0xf3, 0xab, 0x4a, 0xf7, 0x59, 0xbb, 0x03, 0xe9,
  0xcf, 0x4d, 0x6b, 0x19, 0xa4, 0x6f, 0xba, 0x23, 0xf2, 0x5e, 0xb9, 0x8b,
  0x75, 0xf4, 0x56, 0x48, 0x42, 0x64, 0xed, 0x78, 0xc7, 0xb3, 0x41, 0x60,
  0x86, 0x00, 0x41, 0x0c, 0xc0, 0x3f, 0xc0, 0x23, 0xf8, 0x14, 0x4e, 0x00,
  0x00, 0x05, 0x70, 0x59, 0x00, 0x80, 0x00, 0x56, 0xfe, 0x01, 0x95, 0x4b,
  0x59, 0xf5, 0xee, 0xf0, 0x12, 0xf1, 0xd9, 0x43, 0xcb, 0xa2, 0xcb, 0x49,
  0x63, 0xa2, 0x30, 0xea, 0xd6, 0xc6, 0x38, 0x34, 0x29, 0x26, 0x4d, 0xfa,
  0x69, 0xf5, 0x31, 0xe1, 0x35, 0x24, 0x42, 0x18, 0x38, 0x46, 0x4c, 0xad,
  0x1e, 0x2f, 0x99, 0x2f, 0x30, 0x5c, 0x6f, 0x8a, 0xdc, 0xf6, 0x72, 0xb4,
  0x23, 0xde, 0xf6, 0x7c, 0x8a, 0x1a, 0x4d, 0xa8, 0x0c, 0xa3, 0x49, 0x10,
  0xf6, 0x94, 0xcf, 0x21, 0x21, 0x4d, 0xcf, 0x0a, 0x9b, 0xd0, 0x54, 0xd5,
  0xa3, 0xdf, 0x2f, 0x26, 0x17, 0xfd, 0x79, 0xe8, 0x2d, 0x98, 0xcf, 0xb9,
  0x57, 0x24, 0xa3, 0x0e, 0x7f, 0xaf, 0x3f, 0x15, 0xb9, 0x33, 0xdd, 0xfd,
  0x8a, 0x7e, 0xb9, 0x1b, 0xda, 0xaa, 0x0c, 0xfe, 0xf9, 0xbf, 0xaf, 0x01,
  0x7a, 0x0e, 0x4b, 0xc3, 0x02, 0x7a, 0x54, 0xb4, 0x5d, 0x70, 0x90, 0xa0,
  0x83, 0xf0, 0x46, 0x2f, 0x38, 0x94, 0x1c, 0xb6, 0xb8, 0x7f, 0xfe, 0x3f,
  0xcd, 0xa5, 0xb6, 0xb7, 0x1b, 0x0f, 0x83, 0x4c, 0x83, 0xce, 0x44, 0xb0,
  0xb4, 0x06, 0xaa, 0x62, 0x1e, 0xd9, 0xf4, 0x4c, 0x9e, 0x8a, 0x80, 0x5e,
  0xe3, 0xd3, 0xf2, 0xb3, 0x12, 0xe7, 0xa3, 0xb9, 0xb4, 0xb0, 0xab, 0xdf,
  0x24, 0xd2, 0x4f, 0x4c, 0xc9, 0x1f, 0x16, 0x08, 0x84, 0x83, 0x02, 0x62,
  0x83, 0xb1, 0x92, 0x2a, 0x15, 0x5e, 0xda, 0xbe, 0x3b, 0x00, 0xc9, 0x35,
  0x05, 0xd6, 0x00, 0xc9
};

GST_START_TEST (test_vp9_parse_superframe)
{
  GstVp9Parser *parser;
  GstVp9SuperframeInfo superframe_info;

  parser = gst_vp9_parser_new ();

  memset (&superframe_info, 0, sizeof (superframe_info));

  assert_equals_int (gst_vp9_parser_parse_superframe_info (parser,
          &superframe_info, vp9_frame_data, sizeof (vp9_frame_data)),
      GST_VP9_PARSER_OK);

  assert_equals_int (superframe_info.bytes_per_framesize, 2);
  assert_equals_int (superframe_info.frames_in_superframe, 2);
  assert_equals_int (superframe_info.superframe_index_size, 6);

  assert_equals_int (superframe_info.frame_sizes[0], 1333);
  assert_equals_int (superframe_info.frame_sizes[1], 214);

  gst_vp9_parser_free (parser);
}

GST_END_TEST;

static Suite *
vp9parsers_suite (void)
{
  Suite *s = suite_create ("VP9 Parser library");

  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_vp9_parse_superframe);

  return s;
}

GST_CHECK_MAIN (vp9parsers);