Ejemplo n.º 1
0
/* Our nb input close callback */
static void f__input_close_cb(INT32 args) {
  DERR(fprintf(stderr, "Input close callback.\n"));
  pop_n_elems(args);
  if(THIS->inputs) {
    free_input(THIS->inputs);
  }
  if(!THIS->buf_len && THIS->inputs == NULL) {
    finished();
  }
}
Ejemplo n.º 2
0
/* Our nb input read callback */
static void f__input_read_cb(INT32 args)
{
  int avail_size = 0, len;
  struct pike_string *str;
  input *inp = THIS->inputs;
  if(inp == NULL) {
    Pike_error("Input read callback without inputs.");
  }    
  if(args != 2)
    Pike_error("Invalid number of arguments to read callback.");
  if(ARG(2).type != T_STRING) {
    SIMPLE_BAD_ARG_ERROR("_Caudium.nbio()->_input_read_cb", 2, "string");
  }
  str = ARG(2).u.string;
  len = str->len << str->size_shift;
  inp->pos += len;
  if(inp->len != -1 && inp->pos >= inp->len) {
    len -= inp->pos - inp->len; /* Don't "read" too much */
    DERR(fprintf(stderr, "Read all wanted input data.\n"));
    free_input(inp);
  }
  DERR(fprintf(stderr, "Input read callback (got %d bytes).\n", len));
  if(THIS->buf_size) {
    avail_size = THIS->buf_size - (THIS->buf_len + THIS->buf_pos);
  } 
  if(avail_size < len) {
    alloc_data_buf(THIS->buf_size + (len - avail_size));
  }
  DERR(fprintf(stderr, "Copying %d bytes to buf starting at 0x%x (pos %d).\n",
	       len, (int)(THIS->buf + THIS->buf_pos + THIS->buf_len), THIS->buf_pos + THIS->buf_len));
  memcpy(THIS->buf + THIS->buf_pos + THIS->buf_len, str->str, len);
  THIS->buf_len += len;
  if((THIS->buf_len + THIS->buf_pos) > READ_BUFFER_SIZE) {
    DERR(fprintf(stderr, "Read buffer full (%d bytes).\n", THIS->buf_size));
    push_int(0);   push_int(0);  push_int(0);
    apply_low(inp->u.file, inp->set_nb_off, 3);
    pop_stack();
    inp->mode = SLEEPING;
  }
  pop_n_elems(args);
  
  if(THIS->outp->mode == IDLE) {
    DERR(fprintf(stderr, "Waking up output.\n"));
    THIS->outp->mode = ACTIVE;
    f__output_write_cb(0);
  } else {
    DERR(fprintf(stderr, "Output is awake.\n"));
  }
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
    long count, total;
    input_t in;
    init_input(&in, argv[1]);

    switch (lex(&in, &count, &total)) {
        case 0: fprintf(stderr, "ok, parsed %ld HTTPs, peeked %ld chars\n", count, total); break;
        case 1: fprintf(stderr, "syntax error: %s\n", in.cur); break;
        case 2: fprintf(stderr, "fill failed\n"); break;
        default: fprintf(stderr, "panic!\n"); break;
    }

    free_input(&in);
    return 0;
}
Ejemplo n.º 4
0
/* Free any allocated data in the struct */
static void free_nb_struct(struct object *obj) {
  DERR(fprintf(stderr, "Freeing storage.\n"));
  while(THIS->inputs != NULL) {
    free_input(THIS->inputs);
  }

  if(THIS->outp != NULL) {
    free_output(THIS->outp);
    THIS->outp = NULL;
  }
  free_data_buf();
  free_svalue(&THIS->args);
  free_svalue(&THIS->cb);
  THIS->cb.type = T_INT; 
  THIS->args.type = T_INT; 
}
Ejemplo n.º 5
0
/* Let's guess what this function does....
 *
 */
static INLINE void input_finish(void)
{
  struct input *i;

  while(1)
  {
    /* Get the next input from the queue */
    i=THIS->firstinput->next;
    free_input(THIS->firstinput);
    THIS->firstinput=i;

    if(!i) break;

    switch(i->type)
    {
    case I_OBJ:
      THIS->sleeping=0;
      push_callback(offset_input_read_callback);
      push_int(0);
      push_callback(offset_input_close_callback);
      apply_low(i->u.obj,i->set_nonblocking_offset,3);
      pop_stack();
      return;

    case I_BLOCKING_OBJ:
      if (read_some_data())
	return;
      continue;

    case I_MMAP:
      if (THIS->fd==-1) return;
      continue;

    case I_STRING:
      append_buffer(i->u.str);

    case I_NONE: break;
    }
  }
  THIS->sleeping=0;

  low_start();
  finished_p();
}
Ejemplo n.º 6
0
/* Called when the sending is finished. Either due to broken connection
 * or no more data to send.
 */
static void finished(void)
{
  DERR(fprintf(stderr, "Done writing (%d sent)\n", (INT32)THIS->written));

  THIS->finished   = 1;
  while(THIS->inputs != NULL) {
    free_input(THIS->inputs);
  }

  if(THIS->outp != NULL) {
    free_output(THIS->outp);
    THIS->outp = NULL;
  }

  if(THIS->cb.type != T_INT)
  {
    DERR(fprintf(stderr, "Calling done callback\n"));
    push_svalue(&(THIS->args));
    apply_svalue(&(THIS->cb),1);
    pop_stack();
  }
}
Ejemplo n.º 7
0
static void
free_ui_elements(void)
{
  unregister_ui_elt(lower);
  unregister_ui_elt(higher);
  unregister_ui_elt(netinfo);
  unregister_ui_elt(uninfo);
  unregister_ui_elt(heartbeat);
  unregister_ui_elt(pw_input);
  free_input(pw_input);
  free_anic_default(heartbeat);
  free_statusbar(higher);
  free_statusbar(lower);
  free_statusbar(netinfo);
  free_statusbar(uninfo);
  free_txtwindow(busywnd);
  free_txtwindow(errwnd);
  free_ui();
  if (title) {
    free(title);
    title = NULL;
  }
}
Ejemplo n.º 8
0
/******************************************************************************
MODULE:  open_input

PURPOSE:  Sets up the input data structure, opens the input TOA file for
read access, opens the input brightness temp file for read access, allocates
space, and stores some of the metadata for later reference.

RETURN VALUE:
Type = Input_t*
Value      Description
-----      -----------
NULL       Error occurred opening or reading the files
non-NULL   Successful completion
 

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
at the USGS EROS

HISTORY:
Date        Programmer       Reason
--------    ---------------  -------------------------------------
1/2/2012    Gail Schmidt     Original Development (based on input routines
                             from the LEDAPS lndsr application)

NOTES:
  1. This routine opens the input TOA reflectance and brightness temperature
     files and associated SDSs.  It also allocates memory for pointers in the
     input structure.  It is up to the caller to use close_input and
     free_input to close the HDF files and free up the memory when done
     using the input data structure.
******************************************************************************/
Input_t *open_input
(
    char *refl_file_name,     /* I: input TOA reflectance filename */
    char *btemp_file_name     /* I: input brightness temp filename */
)
{
    char FUNC_NAME[] = "open_input";   /* function name */
    char errmsg[STR_SIZE];    /* error message */
    char sds_name[STR_SIZE];  /* name of the current SDS */
    int ib;                   /* index for bands */
    int ir;                   /* index for dimension rank */
    double dval[10];          /* double value for reading the attributes for
                                 the current band like fill, saturation
                                 value, and scale factor */
    Myhdf_dim_t *dim[2];      /* dimensions for the current SDS */
    Input_t *this = NULL;     /* input data structure to be initialized,
                                 populated, and returned to the caller */
    Myhdf_attr_t attr;        /* values for the SDS attributes */
    int16 *buf = NULL;        /* temporary buffer to allocate memory for
                                 the TOA reflectance bands */
  
    /* Create the Input data structure */
    this = (Input_t *) malloc (sizeof (Input_t));
    if (this == NULL) 
    {
        strcpy (errmsg, "Error allocating memory for Input data structure");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
  
    /* Populate the filenames in the data structure */
    this->refl_file_name = dup_string (refl_file_name);
    if (this->refl_file_name == NULL)
    {
        free (this);
        strcpy (errmsg, "Error duplicating the TOA reflectance filename");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }

    this->btemp_file_name = dup_string (btemp_file_name);
    if (this->btemp_file_name == NULL)
    {
        free (this);
        strcpy (errmsg, "Error duplicating the brightness temp filename");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
  
    /* Open the files for SD access */
    this->refl_sds_file_id = SDstart ((char *)refl_file_name, DFACC_RDONLY);
    if (this->refl_sds_file_id == HDF_ERROR)
    {
        free (this->refl_file_name);
        free (this);  
        sprintf (errmsg, "Error opening the input TOA reflectance file: %s",
            refl_file_name);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    this->refl_open = true;
  
    this->btemp_sds_file_id = SDstart ((char *)btemp_file_name, DFACC_RDONLY);
    if (this->btemp_sds_file_id == HDF_ERROR)
    {
        free (this->refl_file_name);
        free (this->btemp_file_name);
        free (this);  
        sprintf (errmsg, "Error opening the input brightness temperature "
            "file: %s", btemp_file_name);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    this->btemp_open = true;
  
    /* Get the global metadata from the input TOA reflectance file */
    if (get_input_meta (this) != SUCCESS)
    {
        free (this->refl_file_name);
        free (this->btemp_file_name);
        free (this);  
        sprintf (errmsg, "Error reading the input metadata from file: %s",
            refl_file_name);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
  
    /* Get SDS information and start SDS access */
    for (ib = 0; ib < this->nrefl_band; ib++)
    {
        this->refl_sds[ib].name = NULL;
        this->refl_sds[ib].dim[0].name = NULL;
        this->refl_sds[ib].dim[1].name = NULL;
        this->refl_buf[ib] = NULL;
    }
  
    this->btemp_sds.name = NULL;
    this->btemp_sds.dim[0].name = NULL;
    this->btemp_sds.dim[1].name = NULL;
    this->btemp_buf = NULL;
  
    /* Loop through the image bands and obtain the SDS information */
    strcpy (errmsg, "none");
    for (ib = 0; ib < this->nrefl_band; ib++)
    {
        /* Get the SDS name and information */
        sprintf (sds_name, "%s%d", SDS_PREFIX, this->meta.refl_band[ib]);
        this->refl_sds[ib].name = dup_string (sds_name);
        if (this->refl_sds[ib].name == NULL)
        {
            sprintf (errmsg, "Error getting the SDS name for TOA reflectance "
                "band %d", ib);
            break;
        }
    
        if (get_sds_info (this->refl_sds_file_id, &this->refl_sds[ib])
            != SUCCESS)
        {
            sprintf (errmsg, "Error getting the SDS info for TOA reflectance "
                "band %d", ib);
            break;
        }
    
        /* Check rank */
        if (this->refl_sds[ib].rank != 2)
        {
            sprintf (errmsg, "Invalid rank for the SDS for TOA reflectance "
                "band %d", ib);
            break;
        }
    
        /* Check SDS type */
        if (this->refl_sds[ib].type != DFNT_INT16)
        {
            sprintf (errmsg, "Invalid data type for the SDS for TOA "
                "reflectance band %d.  Should be INT16.", ib);
            break;
        }
    
        /* Get dimensions */
        for (ir = 0; ir < this->refl_sds[ib].rank; ir++)
        {
            dim[ir] = &this->refl_sds[ib].dim[ir];
            if (get_sds_dim_info (this->refl_sds[ib].id, ir, dim[ir])
                != SUCCESS)
            {
                sprintf (errmsg, "Error obtaining the dimensions of the SDS "
                    "for TOA reflectance band %d.", ib);
                break;
            }
        }
    
        /* Save and check line and sample dimensions */
        if (ib == 0)
        {
            this->nlines = dim[0]->nval;
            this->nsamps = dim[1]->nval;
        }
        else
        {
            if (this->nlines != dim[0]->nval)
            {
                sprintf (errmsg, "Dimensions for the number of lines in TOA "
                    "reflectance band %d does not match the previous "
                    "dimensions for band 0.", ib);
                break;
            }
            if (this->nsamps != dim[1]->nval)
            {
                sprintf (errmsg, "Dimensions for the number of samples in TOA "
                    "reflectance band %d does not match the previous "
                    "dimensions for band 0.", ib);
                break;
            }
        }
    
        /* If this is the first image band read the attribute metadata */
        if (ib == 0)
        {
            /* Fill value */
            attr.type = DFNT_INT16;
            attr.nval = 1;
            attr.name = INPUT_FILL_VALUE;
            if (get_attr_double (this->refl_sds[ib].id, &attr, dval) != SUCCESS)
            {
                sprintf (errmsg, "Error reading the fill value SDS attribute "
                    "for reflectance band %d.", ib);
                break;
            }
            if (attr.nval != 1) 
            {
                sprintf (errmsg, "Invalid number of values for the fill value "
                    "for reflectance band %d.", ib);
                break;
            }
            this->refl_fill = (int) dval[0];

            /* Scale factor */
            attr.type = DFNT_FLOAT32;
            attr.nval = 1;
            attr.name = INPUT_SCALE_FACTOR;
            if (get_attr_double (this->refl_sds[ib].id, &attr, dval) != SUCCESS)
            {
                sprintf (errmsg, "Error reading the scale factor SDS "
                    "attribute for reflectance band %d.", ib);
                break;
            }
            if (attr.nval != 1) 
            {
                sprintf (errmsg, "Invalid number of values for the scale "
                    "factor for reflectance band %d.", ib);
                break;
            }
            this->refl_scale_fact = dval[0];

            /* Saturation value */
            attr.type = DFNT_INT16;
            attr.nval = 1;
            attr.name = INPUT_SATURATE_VALUE;
            if (get_attr_double (this->refl_sds[ib].id, &attr, dval) != SUCCESS)
            {
                sprintf (errmsg, "Error reading the saturation value SDS "
                    "attribute for reflectance band %d.", ib);
                break;
            }
            if (attr.nval != 1) 
            {
                sprintf (errmsg, "Invalid number of values for the saturation "
                    "value for reflectance band %d.", ib);
                break;
            }
            this->refl_saturate_val = (int) dval[0];
        }  /* end if first band */
    }  /* for ib */
  
    /* Check for any errors processing the reflectance bands and use the
       error message already created */
    if (strcmp (errmsg, "none"))
    {
        close_input (this);
        free_input (this);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }

    /* For the single thermal band, obtain the SDS information */
    strcpy (sds_name, "band6");
    this->btemp_sds.name = dup_string (sds_name);
    if (this->btemp_sds.name == NULL)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Error getting the brightness temperature SDS name");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    
    if (get_sds_info (this->btemp_sds_file_id, &this->btemp_sds) != SUCCESS)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Error getting the SDS info for the brightness "
            "temperature band");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    
    /* Check rank */
    if (this->btemp_sds.rank != 2)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Invalid rank for the brightness temp SDS");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    
    /* Check SDS type */
    if (this->btemp_sds.type != DFNT_INT16)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Invalid data type for the brightness temp SDS.  "
            "Should be INT16.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    
    /* Get dimensions */
    for (ir = 0; ir < this->btemp_sds.rank; ir++)
    {
        dim[ir] = &this->btemp_sds.dim[ir];
        if (get_sds_dim_info (this->btemp_sds.id, ir, dim[ir]))
        {
            close_input (this);
            free_input (this);
            sprintf (errmsg, "Error obtaining the dimensions of the SDS "
                "for brightness temperature.");
            error_handler (true, FUNC_NAME, errmsg);
            return (NULL);
        }
    }
    
    /* Check line and sample dimensions with the reflectance dimensions */
    if (this->nlines != dim[0]->nval)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Dimensions for the number of lines in the "
            "brightness temp band does not match the dimensions for the "
            "reflectance TOA bands.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    if (this->nsamps != dim[1]->nval)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Dimensions for the number of samples in the "
            "brightness temp band does not match the dimensions for the "
            "reflectance TOA bands.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    
    /* Read the attribute metadata */
    /* Fill value */
    attr.type = DFNT_INT16;
    attr.nval = 1;
    attr.name = INPUT_FILL_VALUE;
    if (get_attr_double (this->btemp_sds.id, &attr, dval) != SUCCESS)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Error reading the fill value SDS attribute "
            "for the brightness temperature band.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    if (attr.nval != 1) 
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Invalid number of values for the fill value "
            "for the brightness temperature band.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    this->btemp_fill = (int) dval[0];

    /* Scale factor */
    attr.type = DFNT_FLOAT32;
    attr.nval = 1;
    attr.name = INPUT_SCALE_FACTOR;
    if (get_attr_double (this->btemp_sds.id, &attr, dval) != SUCCESS)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Error reading the scale factor SDS "
            "attribute for the brightness temperature band.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    if (attr.nval != 1) 
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Invalid number of values for the scale "
            "factor for the brightness temperature band.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    this->btemp_scale_fact = dval[0];

    /* Saturation value */
    attr.type = DFNT_FLOAT32;
    attr.nval = 1;
    attr.name = INPUT_SATURATE_VALUE;
    if (get_attr_double (this->btemp_sds.id, &attr, dval) != SUCCESS)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Error reading the saturation value SDS "
            "attribute for the brightness temperature band.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    if (attr.nval != 1) 
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Invalid number of values for the saturation "
            "value for the brightness temperature band.");
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    this->btemp_saturate_val = (int) dval[0];

    /* Allocate input buffers.  TOA reflectance buffer has multiple bands.
       Thermal band has one band.  Allocate PROC_NLINES of data for each
       band. */
    buf = (int16 *) calloc (PROC_NLINES * this->nsamps * this->nrefl_band,
        sizeof (int16));
    if (buf == NULL)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Error allocating memory for input TOA reflectance "
            "buffer containing %d lines.", PROC_NLINES);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
    else
    {
        /* Set up the memory buffers for each band */
        this->refl_buf[0] = buf;
        for (ib = 1; ib < this->nrefl_band; ib++)
            this->refl_buf[ib] = this->refl_buf[ib-1] +
                PROC_NLINES * this->nsamps;
    }
  
    this->btemp_buf = (int16 *) calloc (PROC_NLINES * this->nsamps,
        sizeof (int16));
    if (this->btemp_buf == NULL)
    {
        close_input (this);
        free_input (this);
        sprintf (errmsg, "Error allocating memory for input brightness temp "
            "buffer containing %d lines.", PROC_NLINES);
        error_handler (true, FUNC_NAME, errmsg);
        return (NULL);
    }
  
    return (this);
}
Ejemplo n.º 9
0
void close_and_free_everything(struct object *thisobj,struct pipe *p)
{
   struct buffer *b;
   struct input *i;
   struct output *o;
   struct object *obj;

   debug_malloc_touch(thisobj);
   debug_malloc_touch(p);
   
   if(p->done){
     return;
   }
   p->done=1;

   if (thisobj) 
      add_ref(thisobj); /* don't kill object during this */

   while (p->firstbuffer)
   {
      b=p->firstbuffer;
      p->firstbuffer=b->next;
      sbuffers-=b->s->len;
      nbuffers--;
      free_string(b->s);
      b->next=NULL;
      free((char *)b); /* Hubbe */
   }
   p->lastbuffer=NULL;


   while (p->firstinput)
   {
      i=p->firstinput;
      p->firstinput=i->next;
      free_input(i);
   }
   p->lastinput=NULL;

   while (p->firstoutput)
   {
     obj=p->firstoutput;
     o=(struct output *)(obj->storage);
     p->firstoutput=o->next;
     output_finish(obj);
     free_object(obj);
   }
   if (p->fd!=-1)
   {
     fd_close(p->fd);
     p->fd=-1;
   }

   p->living_outputs=0;
   
   if (thisobj)
     free_object(thisobj);

   free_svalue(& p->done_callback);
   free_svalue(& p->output_closed_callback);
   free_svalue(& p->id);

   p->done_callback.type=T_INT;
   p->output_closed_callback.type=T_INT;
   p->id.type=T_INT;

   /* p->done=0; */
}
int seg_transcode_main(Segment_U * seg_union){

	Output_Context *ptr_output_ctx = seg_union->output_ctx;

    if (!(ptr_output_ctx->fmt->flags & AVFMT_NOFILE)) {		//for mp4 or mpegts ,this must be performed
        if (avio_open(&(ptr_output_ctx->ptr_format_ctx->pb), seg_union->ts_name, AVIO_FLAG_WRITE) < 0) {
            fprintf(stderr, "Could not open '%s'\n", seg_union->ts_name);
            exit(OPEN_MUX_FILE_FAIL);
        }
    }
    // write the stream header, if any
    avformat_write_header(ptr_output_ctx->ptr_format_ctx ,NULL);
    write_m3u8_header(ptr_output_ctx);
    int i ;
    for(i = 0 ; i < seg_union->input_nb ; i ++){

    	/*initialize input file information*/
    	init_input( seg_union->input_ctx ,seg_union->input_file[i]);
    	Input_Context *ptr_input_ctx = seg_union->input_ctx;

		ptr_output_ctx->img_convert_ctx = sws_getContext(
				ptr_input_ctx->video_codec_ctx->width ,ptr_input_ctx->video_codec_ctx->height ,PIX_FMT_YUV420P,
				 ptr_output_ctx->video_stream->codec->width ,ptr_output_ctx->video_stream->codec->height ,PIX_FMT_YUV420P ,
				 SWS_BICUBIC ,NULL ,NULL ,NULL);


		printf("src_width = %d ,src_height = %d \n" ,ptr_input_ctx->video_codec_ctx->width ,ptr_input_ctx->video_codec_ctx->height);
		printf("dts_width = %d ,dts_height = %d \n" ,ptr_output_ctx->video_stream->codec->width ,
				ptr_output_ctx->video_stream->codec->height);

		printf("before av_read_frame ...\n");
		/*************************************************************************************/
		/*decoder loop*/
		//
		//
		//***********************************************************************************/
		while(av_read_frame(ptr_input_ctx->ptr_format_ctx ,&ptr_input_ctx->pkt) >= 0){

			if (ptr_input_ctx->pkt.stream_index == ptr_input_ctx->video_index) {

				//decode video packet
				int got_picture = 0;
				avcodec_decode_video2(ptr_input_ctx->video_codec_ctx,
						ptr_input_ctx->yuv_frame, &got_picture, &ptr_input_ctx->pkt);

				if (got_picture) {
					//encode video
					//input stream 的问题。
					ptr_output_ctx->sync_ipts = av_q2d(ptr_input_ctx->ptr_format_ctx->streams[ptr_input_ctx->video_index]->time_base) *
							(ptr_input_ctx->yuv_frame->best_effort_timestamp  )
							- (double)ptr_input_ctx->ptr_format_ctx->start_time / AV_TIME_BASE
							+	ptr_output_ctx->base_ipts;    //current packet time in second

					//printf("ptr_output_ctx->sync_ipts = %f \n" ,ptr_output_ctx->sync_ipts);

					//first swscale
					sws_scale(ptr_output_ctx->img_convert_ctx ,
							(const uint8_t* const*)ptr_input_ctx->yuv_frame->data ,ptr_input_ctx->yuv_frame->linesize ,
							0 ,
							ptr_input_ctx->video_codec_ctx->height ,
							ptr_output_ctx->encoded_yuv_pict->data ,ptr_output_ctx->encoded_yuv_pict->linesize);

					//second swscale
					encode_video_frame(ptr_output_ctx , ptr_output_ctx->encoded_yuv_pict ,ptr_input_ctx );
				}

			} else if (ptr_input_ctx->pkt.stream_index == ptr_input_ctx->audio_index) {
				//printf("audio ...\n");
				//decode audio packet
				uint8_t *tmp_data = ptr_input_ctx->pkt.data;
				int tmp_size = ptr_input_ctx->pkt.size;
				while (ptr_input_ctx->pkt.size > 0) {
					int got_frame = 0;
					int len = avcodec_decode_audio4(ptr_input_ctx->audio_codec_ctx,
							ptr_input_ctx->audio_decode_frame, &got_frame,
							&ptr_input_ctx->pkt);

					if (len < 0) { //decode failed ,skip frame
						fprintf(stderr, "Error while decoding audio frame\n");
						break;
					}

					if (got_frame) {
						//encode the audio data ,and write the data into the output
//						do_audio_out(ptr_output_ctx ,ptr_input_ctx ,ptr_input_ctx->audio_decode_frame);
					} else { //no data
						printf("======>avcodec_decode_audio4 ,no data ..\n");
						continue;
					}

					ptr_input_ctx->pkt.size -= len;
					ptr_input_ctx->pkt.data += len;

				}

				//renew
				ptr_input_ctx->pkt.size = tmp_size;
				ptr_input_ctx->pkt.data = tmp_data;

			}

			if(&ptr_input_ctx->pkt)
				av_free_packet(&ptr_input_ctx->pkt);

		}//endwhile

		double file_duration = ptr_input_ctx->ptr_format_ctx->duration / AV_TIME_BASE
					+ (double)( ptr_input_ctx->ptr_format_ctx->duration % AV_TIME_BASE ) / AV_TIME_BASE;


		ptr_output_ctx->base_ipts  += file_duration;  //completed files sum time duration
		printf("end while ......,time_base = %f .............> \n" ,ptr_output_ctx->base_ipts  );
		ptr_output_ctx->audio_resample = 0;
		sws_freeContext(ptr_output_ctx->img_convert_ctx);
		free_input(ptr_input_ctx);
    } //end for

	printf("before flush ,ptr_output_ctx->ptr_format_ctx->nb_streams = %d \n\n" ,ptr_output_ctx->ptr_format_ctx->nb_streams);
	encode_flush(ptr_output_ctx ,ptr_output_ctx->ptr_format_ctx->nb_streams);

	write_m3u8_tailer(ptr_output_ctx);
	printf("before wirite tailer ...\n\n");
	av_write_trailer(ptr_output_ctx->ptr_format_ctx );

	return 0;
}
Ejemplo n.º 11
0
/* Our write callback */
static void f__output_write_cb(INT32 args)
{
  NBIO_INT_T written = 0, len = 0;
  char *buf = NULL;
  input *inp = THIS->inputs;

  pop_n_elems(args);
  DERR(fprintf(stderr, "output write callback\n"));
  if(THIS->buf_len) {
    /* We currently have buffered data to write */
    len = THIS->buf_len;
    buf = THIS->buf + THIS->buf_pos;
    DERR(fprintf(stderr, "Sending buffered data (%ld bytes left)\n", (long)len));
    written = do_write(THIS->buf + THIS->buf_pos, THIS->buf_len);
    switch(written) {
    case -1: /* We're done here. The write is the weakest link. Goodbye. */
    case 0:  /* Done, but because the write would block or
	      * nothing was written. I.e try again later.
	      */
      set_outp_write_cb(THIS->outp);
      return; 

    default:
      /* Write succeeded */
      THIS->buf_len -= written;
      THIS->buf_pos += written;
      if(THIS->buf_len) {
	/* We couldn't write everything. Return to try later. */
	set_outp_write_cb(THIS->outp);
	return;
      }
      
      /* We wrote all our buffered data. Just fall through to possibly
       * write more.
       */
      THIS->buf_pos = 0;
      THIS->buf_len = 0;
    }
  }
  if(inp == NULL) {
    finished();
    return;
  }
  switch(inp->type) {
  case NBIO_OBJ: /* non-blocking input - if no data available,
		  * just return. once data is available, write_cb will
		  * be called. 
		  */
    THIS->outp->mode = IDLE;
    DERR(fprintf(stderr, "Waiting for NB input data.\n"));
    if(inp->mode == SLEEPING) {
      /* Set read callback here since object is idle */
      push_callback(input_read_cb_off);
      push_int(0);
      push_callback(input_close_cb_off);
      apply_low(THIS->inputs->u.file, THIS->inputs->set_nb_off, 3);
      inp->mode = READING;
    }
    return;
    
  case NBIO_STR: 
    buf = inp->u.data->str + inp->pos;
    len = inp->len - inp->pos;
    DERR(fprintf(stderr, "Sending string data (%ld bytes left)\n", (long)len));
    written = do_write(buf, len);

    if(written >= 0) {
      inp->pos += written;
      if(inp->pos == inp->len) {
	DERR(fprintf(stderr, "Done sending string input (position == length).\n"));
	free_input(inp);
      }
      set_outp_write_cb(THIS->outp);
    }
    break;

#ifdef USE_MMAP
  case NBIO_MMAP:
    len = inp->u.mmap_storage->m_end - inp->pos;
    if(!len) {
      /* need to mmap more data. No need to check if there's more to allocate
       * since the object would have been freed in that case */
      DERR(fprintf(stderr, "mmapping more data from fd %d\n", inp->fd));
      len = MIN(inp->len - inp->pos, MAX_MMAP_SIZE);
      munmap(inp->u.mmap_storage->data, inp->u.mmap_storage->m_len);
      mmapped -= inp->u.mmap_storage->m_len;
      DERR(fprintf(stderr, "trying to mmap %ld bytes starting at pos %ld\n",
		   (long)len, (long)inp->pos));
#ifdef TEST_MMAP_FAILOVER
      inp->u.mmap_storage->data = MAP_FAILED;
#else
      inp->u.mmap_storage->data =
	(char *)mmap(0, len, PROT_READ,
		     MAP_FILE | MAP_SHARED, inp->fd,
		     inp->pos);
#endif
      if(inp->u.mmap_storage->data == MAP_FAILED) {
	struct object *tmp;
	/* FIXME: Better error handling here before falling over to
	 * normal file objects?
	 */
	DERR(perror("additional mmap failed"));
	/* converting to NBIO_OBJ */
	THIS->outp->mode = IDLE;
	tmp = inp->u.mmap_storage->file;
	free(inp->u.mmap_storage);
	inp->u.file = tmp;
#if TEST_MMAP_FAILOVER == NBIO_BLOCK_OBJ
	inp->set_nb_off = -1;	inp->set_b_off  = -1;
#else
	inp->set_nb_off = find_identifier("set_nonblocking",inp->u.file->prog);
	inp->set_b_off  = find_identifier("set_blocking", inp->u.file->prog);
#endif
	if(inp->set_nb_off < 0 || inp->set_b_off < 0)
	{
	  inp->type   = NBIO_BLOCK_OBJ; /* No set_nonblocking/set_blocking funcs */
	  inp->set_nb_off = inp->set_b_off = 0;
	  DERR(fprintf(stderr, "Converting input to NBIO_BLOCK_OBJ.\n"));
	  goto nbio_block_obj_read;
	} else {
	  DERR(fprintf(stderr, "Converting input to NBIO_OBJ.\n"));
	  inp->type   = NBIO_OBJ; /* Fake nonblocking object */
	  push_callback(input_read_cb_off);
	  push_int(0);
	  push_callback(input_close_cb_off);
	  apply_low(inp->u.file, inp->set_nb_off, 3);
	  inp->mode = READING;
	}
	nobjects++;
	break;
      } else {
	inp->u.mmap_storage->m_start = inp->pos;
	inp->u.mmap_storage->m_len   = len;
	inp->u.mmap_storage->m_end   = len + inp->pos;
	mmapped += len;
      }
    }
    buf = inp->u.mmap_storage->data +
      (inp->pos - inp->u.mmap_storage->m_start);
    DERR(fprintf(stderr,"Sending mmapped file (%ld to write, %ld total left)\n"
		 , (long)len, (long)(inp->len - inp->pos)));
    written = do_write(buf, len);

    if(written >= 0) {
      inp->pos += written;
      if(inp->pos == inp->len){
	DERR(fprintf(stderr, "Done sending mmapped input (position == length).\n"));

	free_input(inp);
      }
      set_outp_write_cb(THIS->outp);
    }
#endif
    break;
    
  case NBIO_BLOCK_OBJ:
  nbio_block_obj_read:
    {
      int read;
      read = read_data(); /* At this point we have no data, so read some */
      switch(read) {
      case  -1:
	/* We are done. No more inputs */
	finished();
	return;
      case -2: /* Invalid input for read_data == redo this function */
      case -3: /* We read from a fake object and got a string == redo */
	f__output_write_cb(0);
	return;
      }
      len = THIS->buf_len;
      buf = THIS->buf;
      DERR(fprintf(stderr, "Sending buffered data (%ld bytes left)\n", (long)len));
      written = do_write(buf, len);
      if(written >= 0) {
	THIS->buf_len -= written;
	THIS->buf_pos += written;
	set_outp_write_cb(THIS->outp);
      }
    }
  }   
  if(written < 0) {
    return;
  } 
  if(!THIS->buf_len && THIS->inputs == NULL) {
    finished();
  }
}
Ejemplo n.º 12
0
/* This function reads some data from the current input (file object)
 */
static INLINE int read_data(void)
{
  int buf_size = READ_BUFFER_SIZE;
  NBIO_INT_T to_read  = 0;
  char *rd;
  input *inp;
 redo:
  DERR(fprintf(stderr, "Reading from blocking input.\n"));
  THIS->buf_pos = 0;
  inp = THIS->inputs;
  if(inp == NULL)
    return -1; /* No more inputs */
  if(inp->type != NBIO_BLOCK_OBJ)
    return -2; /* invalid input for read_data */
  if(inp->fd != -1) {
    char * ptr;
    DERR(fprintf(stderr, "Reading from real fd.\n"));
	
    if(inp->len != -1) 
      to_read = MIN(buf_size, inp->len - inp->pos);
    else
      to_read = buf_size;
    if(THIS->buf == NULL || THIS->buf_size < to_read) {
      alloc_data_buf(to_read);
    }
	
    ptr = THIS->buf;
    THREADS_ALLOW();
    to_read = fd_read(inp->fd, ptr, to_read);
    THREADS_DISALLOW();
    DERR(fprintf(stderr, "read %ld from file\n", (long)to_read));
  } else {
    DERR(fprintf(stderr, "Reading from fake fd.\n"));
    if(inp->len != -1 && inp->pos >= inp->len) {
      /* We are done reading from this one */
      DERR(fprintf(stderr, "Data done from fake fd.\n"));
      free_input(inp);
      goto redo; /* goto == ugly, but we want to read the next input
		  * if any
		  */
    }	

    to_read = READ_BUFFER_SIZE;
    push_int(to_read);
    push_int(1);
    apply_low(inp->u.file, inp->read_off, 2);
    if(Pike_sp[-1].type == T_STRING) {
      if(Pike_sp[-1].u.string->len == 0) {
	DERR(fprintf(stderr, "Read zero bytes from fake fd (EOF).\n"));
	to_read = 0;
      } else {
	new_input(Pike_sp[-1], 0, 1);
	to_read = THIS->inputs->len;
	inp->pos += to_read;
 	DERR(fprintf(stderr, "read %ld bytes from fake file\n",
		     (long)to_read));
	pop_stack();
	return -3; /* Got a string buffer appended to the input list */
      }
    } else if(Pike_sp[-1].type == T_INT && Pike_sp[-1].u.integer == 0) {
      to_read = 0;
    } else {
      Pike_error("Incorrect result from read, expected string.\n");
    }
    pop_stack();
  }
  switch(to_read) {
  case 0: /* EOF */
    DERR(fprintf(stderr, "read zero blocking bytes == EOF\n"));
    free_input(inp);
    break;

  case -1:
    if(errno != EAGAIN) {
      /* Got an error. Free input and continue */
      DERR(perror("Error while reading:"));
      free_input(inp); 
    }
    goto redo;

  default:
    inp->pos += to_read;
    if(inp->pos == inp->len) {
      DERR(fprintf(stderr, "Done reading (position == length).\n"));
      free_input(inp);
    }
    THIS->buf_len = to_read;
    break;
  }
  return to_read;
}
Ejemplo n.º 13
0
int
main (int argc, char *argv[])
{
  inp_t input_params;
  mdl_t source_mdl;
  net_t network;
  int cell_index;

  int verbose = 1;
  char *input_file;

  /* Parse options and command line arguments. Diplay help
     message if no (or more than one) argument is given. */

    {
      int opt;

      static struct option longopts[] = {
          {"help", no_argument, NULL, 'h'},
          {"version", no_argument, NULL, 'V'},
          {"verbose", no_argument, NULL, 'v'},
          {"quiet", no_argument, NULL, 'q'},
          {0, 0, 0, 0}
      };

      while ((opt = getopt_long (argc, argv, "hVvq", longopts, NULL)) != -1)
        {
          switch (opt)
            {
            case 'h':
              usage ();
              return EXIT_SUCCESS;
              break;
            case 'V':
              version ();
              return EXIT_SUCCESS;
              break;
            case 'v':
              verbose = 2;
              break;
            case 'q':
              verbose = 0;
              break;
            default:
              usage ();
              return EXIT_FAILURE;
            }
        };
      argc -= optind;
      argv += optind;
      if (argc != 1)
        {
          usage ();
          return EXIT_FAILURE;
        }
      input_file = argv[0];
    }

  /* Read the input file */
  if( read_input_file_names (input_file, &input_params.files, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the chemical network file */
  if( read_network (input_params.files.chem_file, &network, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the input file */
  if( read_input (input_file, &input_params, &network, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the source model file */
  if( read_source (input_params.files.source_file, &source_mdl, &input_params,
               verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  // Hdf5 files, datatype and dataspace
  hid_t       fid, datatype, dataspace, dataset, tsDataset, tsDataspace,  speciesDataset, speciesDataspace, speciesType;
  datatype = H5Tcopy(H5T_NATIVE_DOUBLE);

  hsize_t     dimsf[ ROUTE_DATASET_RANK ]={  source_mdl.n_cells, source_mdl.ts.n_time_steps, input_params.output.n_output_species, N_OUTPUT_ROUTES };
  fid = H5Fcreate( "astrochem_output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); 
  dataspace = H5Screate_simple( ABUNDANCE_DATASET_RANK, dimsf, NULL);

  // Add Atributes
  hid_t simpleDataspace = H5Screate(H5S_SCALAR);
  hid_t attrType = H5Tcopy(H5T_C_S1);
  H5Tset_size ( attrType, MAX_CHAR_FILENAME );
  H5Tset_strpad(attrType,H5T_STR_NULLTERM);
  hid_t attrNetwork = H5Acreate( fid, "chem_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT);
  H5Awrite( attrNetwork, attrType, realpath( input_params.files.chem_file, NULL ) );
  H5Aclose( attrNetwork );
  hid_t attrModel = H5Acreate( fid, "source_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT);
  H5Awrite( attrModel, attrType, realpath( input_params.files.source_file, NULL ) );
  H5Aclose( attrModel );

  H5Tclose( attrType );
  H5Sclose( simpleDataspace );

  // Define chunk property
  hsize_t     chunk_dims[ ROUTE_DATASET_RANK ] = { 1, 1, input_params.output.n_output_species, N_OUTPUT_ROUTES };
  hid_t prop_id = H5Pcreate(H5P_DATASET_CREATE);
  H5Pset_chunk(prop_id, ABUNDANCE_DATASET_RANK , chunk_dims);

  // Create dataset
  dataset = H5Dcreate(fid, "Abundances", datatype, dataspace, H5P_DEFAULT, prop_id, H5P_DEFAULT);

  int i;
  hid_t dataspaceRoute, route_t_datatype, r_t_datatype, route_prop_id, routeGroup;
  hid_t routeDatasets[ input_params.output.n_output_species ];
  if (input_params.output.trace_routes)
    {

      // Create route dataspace
      dataspaceRoute = H5Screate_simple( ROUTE_DATASET_RANK, dimsf, NULL);

      // Create route datatype
      r_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(r_t));
      H5Tinsert( r_t_datatype, "reaction_number", HOFFSET(r_t, reaction_no ), H5T_NATIVE_INT);
      H5Tinsert( r_t_datatype, "reaction_rate", HOFFSET(r_t, rate), H5T_NATIVE_DOUBLE);

      route_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(rout_t));
      H5Tinsert( route_t_datatype, "formation_rate", HOFFSET(rout_t, formation ), r_t_datatype );
      H5Tinsert( route_t_datatype, "destruction_rate", HOFFSET(rout_t, destruction ), r_t_datatype );

      // Define route chunk property
      route_prop_id = H5Pcreate(H5P_DATASET_CREATE);
      H5Pset_chunk( route_prop_id, ROUTE_DATASET_RANK, chunk_dims);


      // Create each named route dataset
      routeGroup = H5Gcreate( fid, "Routes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
      char routeName[6] = "route_";
      char tempName[ MAX_CHAR_SPECIES + sizeof( routeName ) ];
      for( i = 0; i < input_params.output.n_output_species ; i++ )
        {
          strcpy( tempName, routeName );
          strcat( tempName, network.species[input_params.output.output_species_idx[i]].name );
          routeDatasets[i] = H5Dcreate( routeGroup, tempName, route_t_datatype, dataspaceRoute, H5P_DEFAULT, route_prop_id, H5P_DEFAULT);
        }
    }
  // Timesteps and species
  hsize_t n_ts = source_mdl.ts.n_time_steps;
  hsize_t n_species =  input_params.output.n_output_species ;
  tsDataspace = H5Screate_simple( 1, &n_ts, NULL);
  speciesDataspace = H5Screate_simple( 1, &n_species, NULL);
  speciesType = H5Tcopy (H5T_C_S1);
  H5Tset_size (speciesType, MAX_CHAR_SPECIES );
  H5Tset_strpad(speciesType,H5T_STR_NULLTERM);

  // Create ts and species datasets
  tsDataset = H5Dcreate(fid, "TimeSteps", datatype, tsDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  speciesDataset = H5Dcreate(fid, "Species", speciesType, speciesDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  double* convTs = (double*) malloc( sizeof(double)* source_mdl.ts.n_time_steps );
  for( i=0; i< source_mdl.ts.n_time_steps; i++ )
    {
      convTs[i] = source_mdl.ts.time_steps[i] / CONST_MKSA_YEAR;
    }

  H5Dwrite( tsDataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, convTs );


  char speciesName [ input_params.output.n_output_species ][ MAX_CHAR_SPECIES ];
  for( i = 0; i < input_params.output.n_output_species ; i++ )
    {
      strcpy( speciesName[i], network.species[input_params.output.output_species_idx[i]].name );
    }

  H5Dwrite( speciesDataset, speciesType, H5S_ALL, H5S_ALL, H5P_DEFAULT, speciesName );

  free( convTs );
  H5Dclose( tsDataset );
  H5Dclose( speciesDataset );
  H5Tclose( speciesType );
  H5Sclose( tsDataspace );
  H5Sclose( speciesDataspace );


#ifdef HAVE_OPENMP
  /*Initialize lock*/
  omp_init_lock(&lock);


  /* Solve the ODE system for each cell. */
    {
#pragma omp parallel for schedule (dynamic, 1)
#endif
      for (cell_index = 0; cell_index < source_mdl.n_cells; cell_index++)
        {
          if (verbose >= 1)
            fprintf (stdout, "Computing abundances in cell %d...\n",
                     cell_index);
          if( full_solve ( fid, dataset, routeDatasets, dataspace, dataspaceRoute, datatype, route_t_datatype, cell_index, &input_params, source_mdl.mode,
                       &source_mdl.cell[cell_index], &network, &source_mdl.ts, verbose) != EXIT_SUCCESS )
            {
	      exit (EXIT_FAILURE);
            }
          if (verbose >= 1)
            fprintf (stdout, "Done with cell %d.\n", cell_index);
        }
#ifdef HAVE_OPENMP
    }


  /*Finished lock mechanism, destroy it*/
  omp_destroy_lock(&lock);
#endif

  /*
   * Close/release hdf5 resources.
   */
  if (input_params.output.trace_routes)
    {


      for( i = 0; i <  input_params.output.n_output_species ; i++ )
        {
          H5Dclose(routeDatasets[i] );
        }
      H5Sclose(dataspaceRoute);
      H5Gclose(routeGroup);
      H5Pclose(route_prop_id);
      H5Tclose(r_t_datatype);
      H5Tclose(route_t_datatype);
    }
  H5Dclose(dataset);
  H5Pclose(prop_id);
  H5Sclose(dataspace);
  H5Tclose(datatype);

  H5Fclose(fid);

  free_input (&input_params);
  free_mdl (&source_mdl);
  free_network (&network);
  return (EXIT_SUCCESS);
}