Author Topic: [Ndless] Strange crash on load  (Read 3117 times)

0 Members and 1 Guest are viewing this topic.

Offline pbfy0

  • LV2 Member (Next: 40)
  • **
  • Posts: 31
  • Rating: +7/-0
    • View Profile
[Ndless] Strange crash on load
« on: March 09, 2014, 05:23:32 pm »
I'm working on a vp8-based video player for the nSpire, but i'm getting very strange errors that seem to be unrelated to the vpx library. When I include the block
Code: [Select]
if(fread(frame, 1, frame_sz, infile) != frame_sz)
            die("Frame failed to read complete frame");
, the program crashes as soon as it loads, before that line is even executed. At first, I thought the error was because of libvpx, but the version below, which has everything related to libvpx commented out, still crashes in the same way. This occurs in both the kArmTi emulator and a physical calculator. What am I doing wrong? The full source is available at https://github.com/pbfy0/nvid
Code: [Select]
/*
  Copyright (c) 2010 The WebM project authors. All Rights Reserved.


  Use of this source code is governed by a BSD-style license
  that can be found in the LICENSE file in the root of the source
  tree. An additional intellectual property rights grant can be found
  in the file PATENTS.  All contributing project authors may
  be found in the AUTHORS file in the root of the source tree.
 */
 
 
#include <os.h>
#include <libndls.h>
#include <nspireio2.h>


FILE            *infile;
nio_console csl;
#define printf(...) nio_printf(&csl, __VA_ARGS__)


//#define VPX_CODEC_DISABLE_COMPAT 1
//#include "vpx/vpx_decoder.h"
//#include "vpx/vp8dx.h"
//#define vpx_interface (vpx_codec_vp8_dx())
//#define clamp(x) (x < 0 ? 0 : (x > 255 ? 255 : x))
 
 
#define IVF_FILE_HDR_SZ  (32)
#define IVF_FRAME_HDR_SZ (12)
 
static unsigned int mem_get_le32(const unsigned char *mem) {
    return (mem[3] << 24)|(mem[2] << 16)|(mem[1] <<|(mem[0]);
}
 
static void die(char *text) {
    uart_printf(text);
    if(text[strlen(text)-1] != '\n')
        uart_printf("\n");
      fclose(infile);
    exit(EXIT_FAILURE);
}
 
/*static void die_codec(vpx_codec_ctx_t *ctx, char *s) {
    const char *detail = vpx_codec_error_detail(ctx);
    uart_printf("%s: %s\n", s, vpx_codec_error(ctx));
    if(detail)
        uart_printf("    %s\n",detail);
      fclose(infile);
    exit(EXIT_FAILURE);
}*/
 
 
int main(int argc, char **argv) {
      uart_printf("asdasdasd\n");
    //vpx_codec_ctx_t  codec;
    int              flags = 0, frame_cnt = 0;
    unsigned char    file_hdr[IVF_FILE_HDR_SZ];
    unsigned char    frame_hdr[IVF_FRAME_HDR_SZ];
    unsigned char    frame[256*1024];
    //vpx_codec_err_t  res;
     
      nio_console csl;
      clrscr();
      // 53 columns, 29 rows. 0px offset for x/y. Background color 0 (black), foreground color 15 (white)
      nio_init(&csl, 53, 29, 0, 0, 0, 15, TRUE);
      nio_fflush(&csl);
      nio_set_default(&csl);
      printf("hello world!");
 
    //(void)res;
    /* Open files */
      if(argc!=2){
         die("Wrong number of arguments.");
         return 0;
      }
      if(!(infile = fopen(argv[1], "rb"))){
         die("Could not open input file");
         return 0;
      }
      //return 0;
 
    /* Read file header */
    if(!(fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile) == IVF_FILE_HDR_SZ
         && file_hdr[0]=='D' && file_hdr[1]=='K' && file_hdr[2]=='I'
         && file_hdr[3]=='F'))
        die("Not an IVF file!");
 
    //printf("Using %s\n",vpx_codec_iface_name(vpx_interface));
    /* Initialize codec */
    //if(vpx_codec_dec_init(&codec, vpx_interface, NULL, flags))
//        die_codec(&codec, "Failed to initialize decoder");
 
    /* Read each frame */
    while(fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile) == IVF_FRAME_HDR_SZ) {
        unsigned int               frame_sz = mem_get_le32(frame_hdr);
        //vpx_codec_iter_t  iter = NULL;
        //vpx_image_t      *img;
 
 
        frame_cnt++;
            uart_printf("x%u\n", frame_sz);
        if(frame_sz > sizeof(frame))
            die("Frame data too big for example code buffer");
           
        if(fread(frame, 1, frame_sz, infile) != frame_sz)
            die("Frame failed to read complete frame");
           
            //if(vpx_codec_decode(&codec, frame, frame_sz, NULL, 0))
        //    die_codec(&codec, "Failed to decode frame");
 
       
    }
     
    printf("Processed %d frames.\n",frame_cnt);
    //if(vpx_codec_destroy(&codec))
//        die_codec(&codec, "Failed to destroy codec");
 
    fclose(infile);
    return EXIT_SUCCESS;
}

Edit: I figured out what the problem was. The nspire apparently doesn't like large initializing large arrays. When that block was omitted, the initialization of the frame variable got optimized out, so the crash didn't happen. I replaced the array initialization with malloc, and now it works.
« Last Edit: March 23, 2014, 02:39:25 pm by pbfy0 »

Offline CiriousJoker

  • LV2 Member (Next: 40)
  • **
  • Posts: 34
  • Rating: +1/-0
    • View Profile
Re: [Ndless] Strange crash on load
« Reply #1 on: July 20, 2014, 09:30:31 am »
Ye i noticed the problem with the arrays :(
however malloc works fine :)