예제 #1
0
static SchroBuffer *
gst_schro_wrap_gst_buffer (GstBuffer *buffer)
{
  SchroBuffer *schrobuf;

  schrobuf = schro_buffer_new_with_data (GST_BUFFER_DATA (buffer),
      GST_BUFFER_SIZE (buffer));
  schrobuf->free = gst_schro_buffer_free;
  schrobuf->priv = buffer;

  return schrobuf;
}
static SchroBuffer *find_next_parse_unit(SchroParseUnitContext *parse_ctx)
{
    SchroBuffer *enc_buf = NULL;
    int next_pu_offset = 0;
    unsigned char *in_buf;

    if (parse_ctx->buf_size < 13 ||
        parse_ctx->buf[0] != 'B' ||
        parse_ctx->buf[1] != 'B' ||
        parse_ctx->buf[2] != 'C' ||
        parse_ctx->buf[3] != 'D')
        return NULL;

    next_pu_offset = (parse_ctx->buf[5] << 24) +
                     (parse_ctx->buf[6] << 16) +
                     (parse_ctx->buf[7] <<  8) +
                      parse_ctx->buf[8];

    if (next_pu_offset == 0 &&
        SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
        next_pu_offset = 13;

    if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
        return NULL;

    in_buf = av_malloc(next_pu_offset);
    if (!in_buf) {
        av_log(parse_ctx, AV_LOG_ERROR, "Unable to allocate input buffer\n");
        return NULL;
    }

    memcpy(in_buf, parse_ctx->buf, next_pu_offset);
    enc_buf       = schro_buffer_new_with_data(in_buf, next_pu_offset);
    enc_buf->free = libschroedinger_decode_buffer_free;
    enc_buf->priv = in_buf;

    parse_ctx->buf      += next_pu_offset;
    parse_ctx->buf_size -= next_pu_offset;

    return enc_buf;
}
예제 #3
0
static SchroBuffer* FfmpegFindNextSchroParseUnit(FfmpegSchroParseUnitContext *parse_ctx)
{
    SchroBuffer *enc_buf = NULL;
    int next_pu_offset = 0;
    unsigned char *in_buf;

    if (parse_ctx->buf_size < 13 ||
        parse_ctx->buf[0] != 'B' ||
        parse_ctx->buf[1] != 'B' ||
        parse_ctx->buf[2] != 'C' ||
        parse_ctx->buf[3] != 'D')
        return NULL;

    next_pu_offset = (parse_ctx->buf[5] << 24) +
                     (parse_ctx->buf[6] << 16) +
                     (parse_ctx->buf[7] <<  8) +
                      parse_ctx->buf[8];

    if (next_pu_offset == 0 &&
        SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
        next_pu_offset = 13;

    if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
        return NULL;

    in_buf = av_malloc(next_pu_offset);
    memcpy(in_buf, parse_ctx->buf, next_pu_offset);
    enc_buf       = schro_buffer_new_with_data(in_buf, next_pu_offset);
    enc_buf->free = libschroedinger_decode_buffer_free;
    enc_buf->priv = in_buf;

    parse_ctx->buf      += next_pu_offset;
    parse_ctx->buf_size -= next_pu_offset;

    return enc_buf;
}
예제 #4
0
void
decode (FILE *file)
{
  SchroDecoder *decoder;
  SchroBuffer *buffer;
  SchroVideoFormat *format = NULL;
  SchroFrame *frame;
  int picture_number;
  int go;
  int it;
  int eos = FALSE;
  void *packet;
  int size;
  int ret;
  int parse_code;

  decoder = schro_decoder_new();
  schro_decoder_set_picture_order (decoder, SCHRO_DECODER_PICTURE_ORDER_CODED);

  while(!eos) {
    ret = parse_packet (file, &packet, &size);
    if (!ret) {
      exit(1);
    }

    if (size == 0) {
      schro_decoder_push_end_of_stream (decoder);
    } else {
      parse_code = ((uint8_t *)packet)[4];

      buffer = schro_buffer_new_with_data (packet, size);
      buffer->free = buffer_free;
      buffer->priv = packet;

      printf("pushing 0x%02x size %d\n", parse_code, size);

      if (SCHRO_PARSE_CODE_IS_PICTURE (parse_code)) {
        frame = schro_frame_new_and_alloc (NULL,
            schro_params_get_frame_format(8, format->chroma_format),
            format->width, format->height);
        schro_decoder_add_output_picture (decoder, frame);
      }

      it = schro_decoder_push (decoder, buffer);
      if (it == SCHRO_DECODER_FIRST_ACCESS_UNIT) {
        format = schro_decoder_get_video_format (decoder);
      }

      if (!SCHRO_PARSE_CODE_IS_PICTURE (parse_code)) {
        continue;
      }
    }

    go = 1;
    while (go) {
      it = schro_decoder_wait (decoder);

      switch (it) {
        case SCHRO_DECODER_NEED_BITS:
          go = 0;
          break;
        case SCHRO_DECODER_NEED_FRAME:
          frame = schro_frame_new_and_alloc (NULL,
              schro_params_get_frame_format(8, format->chroma_format),
              format->width, format->height);
          schro_decoder_add_output_picture (decoder, frame);
          break;
        case SCHRO_DECODER_OK:
          picture_number = schro_decoder_get_picture_number (decoder);
          frame = schro_decoder_pull (decoder);
          printf("got frame %p, picture number %d\n", frame,
              picture_number);

          if (frame) {
            schro_frame_unref (frame);
          }
          go = 0;
          break;
        case SCHRO_DECODER_EOS:
          printf("got eos\n");
          eos = TRUE;
          go = 0;
          break;
        case SCHRO_DECODER_STALLED:
          printf("stall\n");
          go = 0;
          break;
        case SCHRO_DECODER_ERROR:
          exit(0);
          break;
      }
    }
  }

  printf("freeing decoder\n");
  schro_decoder_free (decoder);
  free(format);
}
예제 #5
0
static int libschroedinger_decode_frame(AVCodecContext *avccontext,
                                        void *data, int *data_size,
                                        const uint8_t *buf, int buf_size)
{

    FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
    SchroDecoder *decoder = p_schro_params->decoder;
    SchroVideoFormat *format;
    AVPicture *picture = data;
    SchroBuffer *enc_buf;
    SchroFrame* frame;
    int state;
    int go = 1;

    *data_size = 0;

    if (buf_size>0) {
        unsigned char *in_buf = av_malloc(buf_size);
        memcpy (in_buf, buf, buf_size);
        enc_buf = schro_buffer_new_with_data (in_buf, buf_size);
        enc_buf->free = libschroedinger_decode_buffer_free;
        enc_buf->priv = in_buf;
        /* Push buffer into decoder. */
        state = schro_decoder_push (decoder, enc_buf);
        if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
            libschroedinger_handle_first_access_unit(avccontext);
    } else {
        if (!p_schro_params->eos_signalled) {
            state = schro_decoder_push_end_of_stream(decoder);
            p_schro_params->eos_signalled = 1;
        }
    }

    format = p_schro_params->format;

    while (go) {
        /* Parse data and process result. */
        state = schro_decoder_wait (decoder);
        switch (state)
        {
        case SCHRO_DECODER_FIRST_ACCESS_UNIT:
            libschroedinger_handle_first_access_unit (avccontext);
            break;

        case SCHRO_DECODER_NEED_BITS:
            /* Need more input data - stop iterating over what we have. */
            go = 0;
            break;

        case SCHRO_DECODER_NEED_FRAME:
            /* Decoder needs a frame - create one and push it in. */

            frame = schro_frame_new_and_alloc(NULL,
                                              p_schro_params->frame_format,
                                              format->width,
                                              format->height);
            schro_decoder_add_output_picture (decoder, frame);
            break;

        case SCHRO_DECODER_OK:
            /* Pull a frame out of the decoder. */
            frame = schro_decoder_pull (decoder);

            if (frame) {
                ff_dirac_schro_queue_push_back(
                                             &p_schro_params->dec_frame_queue,
                                             frame);
            }
            break;
        case SCHRO_DECODER_EOS:
            go = 0;
            p_schro_params->eos_pulled = 1;
            schro_decoder_reset (decoder);
            break;

        case SCHRO_DECODER_ERROR:
            return -1;
            break;
        }
    }

    /* Grab next frame to be returned from the top of the queue. */
    frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);

    if (frame != NULL) {
        memcpy (p_schro_params->dec_pic.data[0],
                frame->components[0].data,
                frame->components[0].length);

        memcpy (p_schro_params->dec_pic.data[1],
                frame->components[1].data,
                frame->components[1].length);

        memcpy (p_schro_params->dec_pic.data[2],
                frame->components[2].data,
                frame->components[2].length);

        /* Fill picture with current buffer data from Schroedinger. */
        avpicture_fill(picture, p_schro_params->dec_pic.data[0],
                       avccontext->pix_fmt,
                       avccontext->width, avccontext->height);

        *data_size = sizeof(AVPicture);

        /* Now free the frame resources. */
        libschroedinger_decode_frame_free (frame);
    }
    return buf_size;
}
예제 #6
0
static gavl_source_status_t 
get_data(bgav_stream_t * s, SchroBuffer ** ret_p)
  {
  gavl_source_status_t st;
  schroedinger_priv_t* priv;
  SchroBuffer * ret;
  int size;
  uint8_t * data;
  priv = s->decoder_priv;
  
  if(priv->eof)
    return GAVL_SOURCE_EOF;
  
  
  if(priv->buffer_size < 13)
    {
    if(priv->p)
      {
      bgav_stream_done_packet_read(s, priv->p);
      priv->p = NULL;
      }

    if(!priv->header_sent)
      {
      priv->buffer_size = s->ext_size;
      priv->buffer_ptr  = s->ext_data;
      priv->header_sent = 1;
      }
    else
      {
      while(1)
        {
        if((st = bgav_stream_get_packet_read(s, &priv->p)) != GAVL_SOURCE_OK)
          {
          if(st == GAVL_SOURCE_EOF)
            {
            schro_decoder_push_end_of_stream(priv->dec);
            priv->eof = 1;
            }
          return st;
          }
        if(!(priv->p->flags & PACKET_FLAG_SKIP))
          break;
        bgav_stream_done_packet_read(s, priv->p);
        }
      priv->buffer_size = priv->p->data_size;
      priv->buffer_ptr = priv->p->data;
      }
    }

  //  fprintf(stderr, "Got packet\n");
  //  bgav_packet_dump(priv->p);
  //  gavl_hexdump(priv->p->data, 16, 16);
  size = next_startcode(priv->buffer_ptr, priv->buffer_size);
  
  if(SCHRO_PARSE_CODE_IS_PICTURE(priv->buffer_ptr[4]))
    {
    if(priv->p->pts != priv->last_pts)
      {
      uint32_t pic_num;
      pic_num = BGAV_PTR_2_32BE(priv->buffer_ptr + 13);
      
      //      fprintf(stderr, "Got picture %d\n", pic_num);
      
      bgav_pts_cache_push(&priv->pc,
                          priv->p,
                          NULL, NULL);
      priv->last_pts = priv->p->pts;
      }
    //    codec->dec_delay++;
    //    fprintf(stderr, "** Delay++: %d\n", codec->dec_delay);
    }
  else if(SCHRO_PARSE_CODE_IS_SEQ_HEADER(priv->buffer_ptr[4]))
    {
    //    fprintf(stderr, "Got sequence\n");
    }
  data = malloc(size);
  memcpy(data, priv->buffer_ptr, size);

  //  fprintf(stderr, "Buffer %d\n", size);
  //  lqt_hexdump(data, 128 > size ? size : 128, 16);
  
  ret = schro_buffer_new_with_data(data, size);
  
  ret->free = buffer_free;
  ret->priv = data;
  
  priv->buffer_size -= size;
  priv->buffer_ptr += size;
  *ret_p = ret;
    
  return GAVL_SOURCE_OK;
  }
예제 #7
0
void
decode (FILE *file)
{
  SchroDecoder *decoder;
  SchroBuffer *buffer;
  SchroVideoFormat *format = NULL;
  SchroFrame *frame;
  int go;
  int it;
  int eos = FALSE;
  void *packet;
  int size;
  int ret;

  decoder = schro_decoder_new();

  while(!eos) {
    ret = parse_packet (file, &packet, &size);
    if (!ret) {
      exit(1);
    }

    if (size == 0) {
      schro_decoder_push_end_of_stream (decoder);
    } else {
      buffer = schro_buffer_new_with_data (packet, size);
      buffer->free = buffer_free;
      buffer->priv = packet;

      it = schro_decoder_push (decoder, buffer);
      if (it == SCHRO_DECODER_FIRST_ACCESS_UNIT) {
        format = schro_decoder_get_video_format (decoder);
      }
    }

    go = 1;
    while (go) {
      it = schro_decoder_wait (decoder);

      switch (it) {
        case SCHRO_DECODER_NEED_BITS:
          go = 0;
          break;
        case SCHRO_DECODER_NEED_FRAME:
          frame = schro_frame_new_and_alloc (NULL,
              schro_params_get_frame_format(8, format->chroma_format),
              format->width, format->height);
          schro_decoder_add_output_picture (decoder, frame);
          break;
        case SCHRO_DECODER_OK:
          frame = schro_decoder_pull (decoder);
          //printf("got frame %p\n", frame);

          if (frame) {
            //printf("picture number %d\n",
            //    schro_decoder_get_picture_number (decoder) - 1);

            schro_frame_unref (frame);
          }
          break;
        case SCHRO_DECODER_EOS:
          printf("got eos\n");
          eos = TRUE;
          go = 0;
          break;
        case SCHRO_DECODER_ERROR:
          exit(0);
          break;
      }
    }
  }

  printf("freeing decoder\n");
  schro_decoder_free (decoder);
  free(format);
}