예제 #1
0
/*
 * Open an ES file and build an elementary stream datastructure to read
 * it with.
 *
 * - `filename` is the ES files name. As a special case, if this is NULL
 *   then standard input (STDIN_FILENO) will be read from.
 *
 * Opens the file for read, builds the datastructure, and reads the first 3
 * bytes of the input file (this is done to prime the triple-byte search
 * mechanism).
 *
 * Returns 0 if all goes well, 1 otherwise.
 */
extern int open_elementary_stream(char  *filename,
                                  ES_p  *es)
{
	int err;
	int input;

	if (filename == NULL)
		input = STDIN_FILENO;
	else
	{
		input = open_binary_file(filename,FALSE);
		if (input == -1) return 1;
	}

	err = build_elementary_stream_file(input,es);
	if (err)
	{
		fprintf(stderr,"### Error building elementary stream for file %s\n",
		        filename);
		return 1;
	}
	return 0;
}
예제 #2
0
/* get chunk date from specified local file path */
static SIXELSTATUS
sixel_chunk_from_file(
    char const      /* in */ *filename,
    sixel_chunk_t   /* in */ *pchunk,
    int const       /* in */ *cancel_flag
)
{
    SIXELSTATUS status = SIXEL_FALSE;
    int ret;
    FILE *f;
    int n;
    size_t const bucket_size = 4096;

    status = open_binary_file(&f, filename);
    if (SIXEL_FAILED(status)) {
        goto end;
    }

    for (;;) {
        if (pchunk->max_size - pchunk->size < bucket_size) {
            pchunk->max_size *= 2;
            pchunk->buffer = (unsigned char *)sixel_allocator_realloc(pchunk->allocator,
                                                                      pchunk->buffer,
                                                                      pchunk->max_size);
            if (pchunk->buffer == NULL) {
                sixel_helper_set_additional_message(
                    "sixel_chunk_from_file: sixel_allocator_realloc() failed.");
                status = SIXEL_BAD_ALLOCATION;
                goto end;
            }
        }

        if (isatty(fileno(f))) {
            for (;;) {
                if (*cancel_flag) {
                    status = SIXEL_INTERRUPTED;
                    goto end;
                }
                ret = wait_file(fileno(f), 10000);
                if (ret < 0) {
                    sixel_helper_set_additional_message(
                        "sixel_chunk_from_file: wait_file() failed.");
                    status = SIXEL_RUNTIME_ERROR;
                    goto end;
                }
                if (ret == 0) {
                    break;
                }
            }
        }
        n = fread(pchunk->buffer + pchunk->size, 1, 4096, f);
        if (n <= 0) {
            break;
        }
        pchunk->size += n;
    }

    if (f != stdin) {
        fclose(f);
    }

    status = SIXEL_OK;

end:
    return status;
}
예제 #3
0
파일: stream_type.c 프로젝트: gozfree/src
int main(int argc, char **argv)
{
  char   *input_name = NULL;
  int     had_input_name = FALSE;
  int     input = -1;
  int     verbose = FALSE;
  int     quiet = FALSE;
  int     err = 0;
  int     ii = 1;
  int     decided = FALSE;
  int     result = STREAM_IS_ERROR;
  
  if (argc < 2)
  {
    print_usage();
    return 0;
  }

  while (ii < argc)
  {
    if (argv[ii][0] == '-')
    {
      if (!strcmp("--help",argv[ii]) || !strcmp("-h",argv[ii]) ||
          !strcmp("-help",argv[ii]))
      {
        print_usage();
        return STREAM_IS_ERROR;
      }
      else if (!strcmp("-verbose",argv[ii]) || !strcmp("-v",argv[ii]))
      {
        verbose = TRUE;
        quiet = FALSE;
      }
      else if (!strcmp("-quiet",argv[ii]) || !strcmp("-q",argv[ii]))
      {
        verbose = FALSE;
        quiet = TRUE;
      }
      else
      {
        fprintf(stderr,"### stream_type: "
                "Unrecognised command line switch '%s'\n",argv[ii]);
        return STREAM_IS_ERROR;
      }
    }
    else
    {
      if (had_input_name)
      {
        fprintf(stderr,"### stream_type: Unexpected '%s'\n",argv[ii]);
        return STREAM_IS_ERROR;
      }
      else
      {
        input_name = argv[ii];
        had_input_name = TRUE;
      }
    }
    ii++;
  }
  
  if (!had_input_name)
  {
    fprintf(stderr,"### stream_type: No input file specified\n");
    return STREAM_IS_ERROR;
  }
  
  input = open_binary_file(input_name,FALSE);
  if (input == -1)
  {
    fprintf(stderr,"### stream_type: Unable to open input file %s\n",
            input_name);
    return 1;
  }

  if (!quiet)
    printf("Reading from %s\n",input_name);
  
  // Try to guess
  err = determine_packet_type(input,verbose,&decided,&result);
  if (err)
  {
    fprintf(stderr,"### Unable to decide on stream type due to error\n");
    return STREAM_IS_ERROR;
  }

  if (!quiet)
  {
    if (!decided)
    {
      printf("Unable to decide\n");
      result = STREAM_IS_UNSURE;
    }
    else
    {
      switch (result)
      {
      case STREAM_IS_TS:
        printf("It appears to be Transport Stream\n");
        break;
      case STREAM_IS_PS:
        printf("It appears to be Program Stream\n");
        break;
      case STREAM_IS_H262:
        printf("It appears to be Elementary Stream, MPEG-2 (H.262)\n");
        break;
      case STREAM_IS_H264:
        printf("It appears to be Elementary Stream, MPEG-4 (H.264)\n");
        break;
      case STREAM_IS_AVS:
        printf("It appears to be Elementary Stream, AVS\n");
        break;
      case STREAM_MAYBE_PES:
        printf("It looks likely to be PES\n");
        break;
      case STREAM_IS_UNSURE:
        printf("It is not recognised\n");
        break;
      default:
        printf("Unexpected decision value %d\n",result);
        result = STREAM_IS_ERROR;
        break;
      }
    }
  }
  return result;
}
예제 #4
0
int main(int argc, char **argv)
{
  int    had_video_name = FALSE;
  int    had_audio_name = FALSE;
  int    had_output_name = FALSE;
  char  *video_name = NULL;
  char  *audio_name = NULL;
  char  *output_name = NULL;
  int    err = 0;
  ES_p   video_es = NULL;
  access_unit_context_p h264_video_context = NULL;
  avs_context_p avs_video_context = NULL;
  int    audio_file = -1;
  TS_writer_p output = NULL;
  int    quiet = FALSE;
  int    verbose = FALSE;
  int    debugging = FALSE;
  int    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
  int    audio_sample_rate = CD_RATE;
  int    video_frame_rate = DEFAULT_VIDEO_FRAME_RATE;
  int    audio_type = AUDIO_ADTS;
  int    video_type = VIDEO_H264;
  int    pat_pmt_freq = 0;
  int    ii = 1;

#if TEST_PTS_DTS
  test_pts();
  return 0;
#endif

  if (argc < 2)
  {
    print_usage();
    return 0;
  }

  while (ii < argc)
  {
    if (argv[ii][0] == '-')
    {
      if (!strcmp("--help",argv[ii]) || !strcmp("-help",argv[ii]) ||
          !strcmp("-h",argv[ii]))
      {
        print_usage();
        return 0;
      }
      else if (!strcmp("-verbose",argv[ii]) || !strcmp("-v",argv[ii]))
      {
        verbose = TRUE;
      }
      else if (!strcmp("-quiet",argv[ii]) || !strcmp("-q",argv[ii]))
      {
        quiet = TRUE;
      }
      else if (!strcmp("-x",argv[ii]))
      {
        debugging = TRUE;
        quiet = FALSE;
      }
      else if (!strcmp("-rate",argv[ii]))
      {
        CHECKARG("esmerge",ii);
        err = int_value("esmerge",argv[ii],argv[ii+1],TRUE,10,&audio_sample_rate);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-cd",argv[ii]))
      {
        audio_sample_rate = CD_RATE;
      }
      else if (!strcmp("-dat",argv[ii]))
      {
        audio_sample_rate = DAT_RATE;
      }
      else if (!strcmp("-vidrate",argv[ii]))
      {
        CHECKARG("esmerge",ii);
        err = int_value("esmerge",argv[ii],argv[ii+1],TRUE,10,&video_frame_rate);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-adts",argv[ii]))
      {
        audio_type = AUDIO_ADTS;
      }
      else if (!strcmp("-l2",argv[ii]))
      {
        audio_type = AUDIO_L2;
      }
      else if (!strcmp("-ac3", argv[ii]))
      {
        audio_type = AUDIO_AC3;
      }
      else if (!strcmp("-h264",argv[ii]))
      {
        video_type = VIDEO_H264;
      }
      else if (!strcmp("-mp2adts", argv[ii]))
      {
        audio_type = AUDIO_ADTS_MPEG2;
      }
      else if (!strcmp("-mp4adts", argv[ii]))
      {
        audio_type = AUDIO_ADTS_MPEG4;
      }
      else if (!strcmp("-avs",argv[ii]))
      {
        video_type = VIDEO_AVS;
      }
      else if (!strcmp("-patpmtfreq", argv[ii]))
        {
          err = int_value("patpmtfreq", argv[ii], argv[ii+1], TRUE, 10, &pat_pmt_freq);
          if (err) { return 1; }
          ++ii;
        }
      else
      {
        fprintf(stderr,"### esmerge: "
                "Unrecognised command line switch '%s'\n",argv[ii]);
        return 1;
      }
    }
    else
    {
      if (!had_video_name)
      {
        video_name = argv[ii];
        had_video_name = TRUE;
      }
      else if (!had_audio_name)
      {
        audio_name = argv[ii];
        had_audio_name = TRUE;
      }
      else if (!had_output_name)
      {
        output_name = argv[ii];
        had_output_name = TRUE;
      }
      else
      {
        fprintf(stderr,"### esmerge: Unexpected '%s'\n",argv[ii]);
        return 1;
      }
    }
    ii++;
  }

  if (!had_video_name)
  {
    fprintf(stderr,"### esmerge: No video input file specified\n");
    return 1;
  }
  if (!had_audio_name)
  {
    fprintf(stderr,"### esmerge: No audio input file specified\n");
    return 1;
  }
  if (!had_output_name)
  {
    fprintf(stderr,"### esmerge: No output file specified\n");
    return 1;
  }

  err = open_elementary_stream(video_name,&video_es);
  if (err)
  {
    fprintf(stderr,"### esmerge: "
            "Problem starting to read video as ES - abandoning reading\n");
    return 1;
  }

  if (video_type == VIDEO_H264)
  {
    err = build_access_unit_context(video_es,&h264_video_context);
    if (err)
    {
      fprintf(stderr,"### esmerge: "
              "Problem starting to read video as H.264 - abandoning reading\n");
      close_elementary_stream(&video_es);
      return 1;
    }
  }
  else if (video_type == VIDEO_AVS)
  {
    err = build_avs_context(video_es,&avs_video_context);
    if (err)
    {
      fprintf(stderr,"### esmerge: "
              "Problem starting to read video as H.264 - abandoning reading\n");
      close_elementary_stream(&video_es);
      return 1;
    }
  }
  else
  {
    fprintf(stderr,"### esmerge: Unknown video type\n");
    return 1;
  }

  audio_file = open_binary_file(audio_name,FALSE);
  if (audio_file == -1)
  {
    fprintf(stderr,"### esmerge: "
            "Problem opening audio file - abandoning reading\n");
    close_elementary_stream(&video_es);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    return 1;
  }

  err = tswrite_open(TS_W_FILE,output_name,NULL,0,quiet,&output);
  if (err)
  {
    fprintf(stderr,"### esmerge: "
            "Problem opening output file %s - abandoning reading\n",
            output_name);
    close_elementary_stream(&video_es);
    close_file(audio_file);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    return 1;
  }

  switch (audio_type)
  {
  case AUDIO_ADTS:
    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
    break;
  case AUDIO_L2:
    audio_samples_per_frame = L2_SAMPLES_PER_FRAME;
    break;
  case AUDIO_AC3:
    audio_samples_per_frame = AC3_SAMPLES_PER_FRAME;
    break;
  default:              // hmm - or we could give up...
    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
    break;
  }

  if (!quiet)
  {
    printf("Reading video from %s\n",video_name);
    printf("Reading audio from %s (as %s)\n",audio_name,AUDIO_STR(audio_type));
    printf("Writing output to  %s\n",output_name);
    printf("Audio sample rate: %dHz (%.2fKHz)\n",audio_sample_rate,
           audio_sample_rate/1000.0);
    printf("Audio samples per frame: %d\n",audio_samples_per_frame);
    printf("Video frame rate: %dHz\n",video_frame_rate);
  }


  if (video_type == VIDEO_H264)
    err = merge_with_h264(h264_video_context,audio_file,output,
                          audio_type,
                          audio_samples_per_frame,audio_sample_rate,
                          video_frame_rate,
                          pat_pmt_freq,
                          quiet,verbose,debugging);
  else if (video_type == VIDEO_AVS)
    err = merge_with_avs(avs_video_context,audio_file,output,
                         audio_type,
                         audio_samples_per_frame,audio_sample_rate,
                         video_frame_rate,
                         pat_pmt_freq,
                         quiet,verbose,debugging);
  else
  {
    fprintf(stderr,"### esmerge: Unknown video type\n");
    return 1;
  }
  if (err)
  {
    fprintf(stderr,"### esmerge: Error merging video and audio streams\n");
    close_elementary_stream(&video_es);
    close_file(audio_file);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    (void) tswrite_close(output,quiet);
    return 1;
  }

  close_elementary_stream(&video_es);
  close_file(audio_file);
  free_access_unit_context(&h264_video_context);
  free_avs_context(&avs_video_context);
  err = tswrite_close(output,quiet);
  if (err)
  {
    fprintf(stderr,"### esmerge: Error closing output %s\n",output_name);
    return 1;
  }
  return 0;
}
예제 #5
0
int main(int argc, char **argv)
{
    int    use_stdout = FALSE;
    int    use_stdin = FALSE;
    char  *input_name = NULL;
    char  *output_name = NULL;
    int    had_input_name = FALSE;
    int    had_output_name = FALSE;
    char  *action_switch = "None";

    EXTRACT   extract = EXTRACT_VIDEO; // What we're meant to extract
    int       input   = -1;    // Our input file descriptor
    FILE     *output  = NULL;  // The stream we're writing to (if any)
    int       max     = 0;     // The maximum number of TS packets to read (or 0)
    uint32_t  pid     = 0;     // The PID of the (single) stream to extract
    int       quiet   = FALSE; // True => be as quiet as possible
    int       verbose = FALSE; // True => output diagnostic/progress messages
    int       use_pes = FALSE;

    int    err = 0;
    int    ii = 1;

    if (argc < 2)
    {
        print_usage();
        return 0;
    }

    while (ii < argc)
    {
        if (argv[ii][0] == '-')
        {
            if (!strcmp("--help",argv[ii]) || !strcmp("-h",argv[ii]) ||
                    !strcmp("-help",argv[ii]))
            {
                print_usage();
                return 0;
            }
            else if (!strcmp("-verbose",argv[ii]) || !strcmp("-v",argv[ii]))
            {
                verbose = TRUE;
                quiet = FALSE;
            }
            else if (!strcmp("-quiet",argv[ii]) || !strcmp("-q",argv[ii]))
            {
                verbose = FALSE;
                quiet = TRUE;
            }
            else if (!strcmp("-max",argv[ii]) || !strcmp("-m",argv[ii]))
            {
                CHECKARG("ts2es",ii);
                err = int_value("ts2es",argv[ii],argv[ii+1],TRUE,10,&max);
                if (err) return 1;
                ii++;
            }
            else if (!strcmp("-pes",argv[ii]) || !strcmp("-ps",argv[ii]))
            {
                use_pes = TRUE;
            }
            else if (!strcmp("-pid",argv[ii]))
            {
                CHECKARG("ts2es",ii);
                err = unsigned_value("ts2es",argv[ii],argv[ii+1],0,&pid);
                if (err) return 1;
                ii++;
                extract = EXTRACT_PID;
            }
            else if (!strcmp("-video",argv[ii]))
            {
                extract = EXTRACT_VIDEO;
            }
            else if (!strcmp("-audio",argv[ii]))
            {
                extract = EXTRACT_AUDIO;
            }
            else if (!strcmp("-stdin",argv[ii]))
            {
                use_stdin = TRUE;
                had_input_name = TRUE;  // so to speak
            }
            else if (!strcmp("-stdout",argv[ii]))
            {
                use_stdout = TRUE;
                had_output_name = TRUE;  // so to speak
                redirect_output_stderr();
            }
            else if (!strcmp("-err",argv[ii]))
            {
                CHECKARG("ts2es",ii);
                if (!strcmp(argv[ii+1],"stderr"))
                    redirect_output_stderr();
                else if (!strcmp(argv[ii+1],"stdout"))
                    redirect_output_stdout();
                else
                {
                    fprint_err("### ts2es: "
                               "Unrecognised option '%s' to -err (not 'stdout' or"
                               " 'stderr')\n",argv[ii+1]);
                    return 1;
                }
                ii++;
            }
            else
            {
                fprint_err("### ts2es: "
                           "Unrecognised command line switch '%s'\n",argv[ii]);
                return 1;
            }
        }
        else
        {
            if (had_input_name && had_output_name)
            {
                fprint_err("### ts2es: Unexpected '%s'\n",argv[ii]);
                return 1;
            }
            else if (had_input_name)  // shouldn't do this if had -stdout
            {
                output_name = argv[ii];
                had_output_name = TRUE;
            }
            else
            {
                input_name = argv[ii];
                had_input_name = TRUE;
            }
        }
        ii++;
    }

    if (!had_input_name)
    {
        print_err("### ts2es: No input file specified\n");
        return 1;
    }

    if (!had_output_name)
    {
        fprint_err("### ts2es: "
                   "No output file specified for %s\n",action_switch);
        return 1;
    }

    // ============================================================
    // Testing PES output
    if (use_pes && extract == EXTRACT_PID)
    {
        print_err("### ts2es: -pid is not supported with -pes\n");
        return 1;
    }
    if (use_pes && use_stdout)
    {
        print_err("### ts2es: -stdout is not supported with -pes\n");
        return 1;
    }
    if (use_pes && use_stdin)
    {
        print_err("### ts2es: -stdin is not supported with -pes\n");
        return 1;
    }
    if (use_pes)
    {
        err = extract_av_via_pes(input_name,output_name,(extract==EXTRACT_VIDEO),
                                 quiet);
        if (err)
        {
            print_err("### ts2es: Error writing via PES\n");
            return 1;
        }
        return 0;
    }
    // ============================================================

    // Try to stop extraneous data ending up in our output stream
    if (use_stdout)
    {
        verbose = FALSE;
        quiet = TRUE;
    }

    if (use_stdin)
        input = STDIN_FILENO;
    else
    {
        input = open_binary_file(input_name,FALSE);
        if (input == -1)
        {
            fprint_err("### ts2es: Unable to open input file %s\n",input_name);
            return 1;
        }
    }
    if (!quiet)
        fprint_msg("Reading from %s\n",(use_stdin?"<stdin>":input_name));

    if (had_output_name)
    {
        if (use_stdout)
            output = stdout;
        else
        {
            output = fopen(output_name,"wb");
            if (output == NULL)
            {
                if (!use_stdin) (void) close_file(input);
                fprint_err("### ts2es: "
                           "Unable to open output file %s: %s\n",output_name,
                           strerror(errno));
                return 1;
            }
        }
        if (!quiet)
            fprint_msg("Writing to   %s\n",(use_stdout?"<stdout>":output_name));
    }

    if (!quiet)
    {
        if (extract == EXTRACT_PID)
            fprint_msg("Extracting packets for PID %04x (%d)\n",pid,pid);
        else
            fprint_msg("Extracting %s\n",(extract==EXTRACT_VIDEO?"video":"audio"));
    }

    if (max && !quiet)
        fprint_msg("Stopping after %d TS packets\n",max);

    if (extract == EXTRACT_PID)
        err = extract_pid(input,output,pid,max,verbose,quiet);
    else
        err = extract_av(input,output,(extract==EXTRACT_VIDEO),
                         max,verbose,quiet);
    if (err)
    {
        print_err("### ts2es: Error extracting data\n");
        if (!use_stdin)  (void) close_file(input);
        if (!use_stdout) (void) fclose(output);
        return 1;
    }

    // And tidy up when we're finished
    if (!use_stdout)
    {
        errno = 0;
        err = fclose(output);
        if (err)
        {
            fprint_err("### ts2es: Error closing output file %s: %s\n",
                       output_name,strerror(errno));
            (void) close_file(input);
            return 1;
        }
    }
    if (!use_stdin)
    {
        err = close_file(input);
        if (err)
            fprint_err("### ts2es: Error closing input file %s\n",input_name);
    }
    return 0;
}