static unsigned char
uchar_range (unsigned min, unsigned max)
{
  unsigned x = unsigned_value ();
  if (x < min || max < x)
    x = min;
  return x;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
  int    use_stdin = FALSE;
  char  *input_name = NULL;
  int    had_input_name = FALSE;

  TS_reader_p  tsreader = NULL;

  int       max     = 0;     // The maximum number of TS packets to read (or 0)
  int       verbose = FALSE; // True => output diagnostic/progress messages
  int       quiet   = FALSE;
  int       report_timing  = FALSE;
  int       report_buffering = FALSE;
  int       show_data = FALSE;
  char     *output_name = NULL;
  uint32_t  continuity_cnt_pid = INVALID_PID;

  uint64_t  report_mask = ~0;   // report as many bits as we get

  int       select_pid = FALSE;
  uint32_t  just_pid = 0;

  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("-timing",argv[ii]) || !strcmp("-t",argv[ii]))
      {
        report_timing = TRUE;
        quiet = FALSE;
      }
      else if (!strcmp("-buffering",argv[ii]) || !strcmp("-b",argv[ii]))
      {
        report_buffering = TRUE;
        quiet = FALSE;
      }
      else if (!strcmp("-o",argv[ii]))
      {
        output_name = argv[ii+1];
        ii ++;
      }
      else if (!strcmp("-cnt",argv[ii]))
      {
        err = unsigned_value("tsreport",argv[ii],argv[ii+1],10,&continuity_cnt_pid);
        if (err) return 1;
        printf("Reporting on continuity_counter for pid = %04x (%u)\n",
               continuity_cnt_pid,continuity_cnt_pid);
        report_buffering = TRUE;
        quiet = FALSE;
        ii ++;
      }
      else if (!strcmp("-data",argv[ii]))
      {
        show_data = TRUE;
        quiet = FALSE;
      }
      else if (!strcmp("-32",argv[ii]))
      {
        report_mask = 0xFFFFFFFF;       // i.e., bottom 32 bits only
      }
      else if (!strcmp("-tfmt",argv[ii]))
      {
        CHECKARG("tsreport",ii);
        if ((tfmt_diff = fmtx_str_to_timestamp_flags(argv[ii + 1])) < 0)
        {
          printf("### tsreport: Bad timestamp format '%s'\n",argv[ii+1]);
          return 1;
        }
        ii++;
      }
      else if (!strcmp("-tafmt",argv[ii]))
      {
        CHECKARG("tsreport",ii);
        if ((tfmt_abs = fmtx_str_to_timestamp_flags(argv[ii + 1])) < 0)
        {
          printf("### tsreport: Bad timestamp format '%s'\n",argv[ii+1]);
          return 1;
        }
        ii++;
      }
      else if (!strcmp("-justpid",argv[ii]))
      {
        CHECKARG("tsreport",ii);
        err = unsigned_value("tsreport",argv[ii],argv[ii+1],0,&just_pid);
        if (err) return 1;
        select_pid = TRUE;
        ii++;
      }
      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("tsreport",ii);
        err = int_value("tsreport",argv[ii],argv[ii+1],TRUE,10,&max);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-stdin",argv[ii]))
      {
        use_stdin = TRUE;
        had_input_name = TRUE;  // so to speak
      }
      else
      {
        fprintf(stderr,"### tsreport: "
                "Unrecognised command line switch '%s'\n",argv[ii]);
        return 1;
      }
    }
    else
    {
      if (had_input_name)
      {
        fprintf(stderr,"### tsreport: Unexpected '%s'\n",argv[ii]);
        return 1;
      }
      else
      {
        input_name = argv[ii];
        had_input_name = TRUE;
      }
    }
    ii++;
  }

  if (!had_input_name)
  {
    fprintf(stderr,"### tsreport: No input file specified\n");
    return 1;
  }

  err = open_file_for_TS_read((use_stdin?NULL:input_name),&tsreader);
  if (err)
  {
    fprintf(stderr,
            "### tsreport: Unable to open input file %s for reading TS\n",
            use_stdin?"<stdin>":input_name);
    return 1;
  }
  printf("Reading from %s\n",(use_stdin?"<stdin>":input_name));

  if (max)
    printf("Stopping after %d TS packets\n",max);

  if (select_pid)
    err = report_single_pid(tsreader,max,quiet,just_pid);
  else if (report_buffering)
    err = report_buffering_stats(tsreader,max,verbose,quiet,
                                 output_name,continuity_cnt_pid,report_mask);
  else
    err = report_ts(tsreader,max,verbose,show_data,report_timing);
  if (err)
  {
    fprintf(stderr,"### tsreport: Error reporting on input stream\n");
    (void) close_TS_reader(&tsreader);
    return 1;
  }
  err = close_TS_reader(&tsreader);
  if (err) return 1;

  return 0;
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
  int     use_stdin = FALSE;
  int     use_stdout = FALSE;
  int     use_tcpip = FALSE;
  int     port = 88; // Useful default port number
  char   *input_name = NULL;
  char   *output_name = NULL;
  int     had_input_name = FALSE;
  int     had_output_name = FALSE;
  PS_reader_p ps = NULL;
  TS_writer_p output = NULL;
  int     verbose = FALSE;
  int     quiet = FALSE;
  int     max = 0;
  uint32_t pmt_pid = 0x66;
  uint32_t video_pid = 0x68;
  uint32_t pcr_pid = video_pid;  // Use PCRs from the video stream
  uint32_t audio_pid = 0x67;
  int     keep_audio = TRUE;
  int     repeat_program_every = 100;
  int     pad_start = 8;
  int     err = 0;
  int     ii = 1;

  int     video_type = VIDEO_H262;  // hopefully a sensible default
  int     force_stream_type = FALSE;

  int     video_stream = -1;
  int     audio_stream = -1;
  int     want_ac3_audio = FALSE;

  int     input_is_dvd = TRUE;
  int     want_dolby_as_dvb = TRUE;

  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("-avc",argv[ii]) || !strcmp("-h264",argv[ii]))
      {
        force_stream_type = TRUE;
        video_type = VIDEO_H264;
      }
      else if (!strcmp("-h262",argv[ii]))
      {
        force_stream_type = TRUE;
        video_type = VIDEO_H262;
      }
      else if (!strcmp("-vtype",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = int_value("ps2ts",argv[ii],argv[ii+1],TRUE,0,
                        &video_type);
        if (err) return 1;
        ii++;
        force_stream_type = TRUE;
      }
      else if (!strcmp("-mp42",argv[ii]))
      {
        force_stream_type = TRUE;
        video_type = VIDEO_MPEG4_PART2;
      }
      else if (!strcmp("-dolby",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        if (!strcmp("dvb",argv[ii+1]))
          want_dolby_as_dvb = TRUE;
        else if (!strcmp("atsc",argv[ii+1]))
          want_dolby_as_dvb = FALSE;
        else
        {
          print_err("### ps2ts: -dolby must be followed by dvb or atsc\n");
          return 1;
        }
        ii++;
      }
      else if (!strcmp("-stdin",argv[ii]))
      {
        had_input_name = TRUE; // more or less
        use_stdin = TRUE;
      }
      else if (!strcmp("-stdout",argv[ii]))
      {
        had_output_name = TRUE; // more or less
        use_stdout = TRUE;
        redirect_output_stderr();
      }
      else if (!strcmp("-err",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        if (!strcmp(argv[ii+1],"stderr"))
          redirect_output_stderr();
        else if (!strcmp(argv[ii+1],"stdout"))
          redirect_output_stdout();
        else
        {
          fprint_err("### ps2ts: "
                     "Unrecognised option '%s' to -err (not 'stdout' or"
                     " 'stderr')\n",argv[ii+1]);
          return 1;
        }
        ii++;
      }
      else if (!strcmp("-dvd",argv[ii]))
      {
        input_is_dvd = TRUE;
      }
      else if (!strcmp("-notdvd",argv[ii]) || !strcmp("-nodvd",argv[ii]))
      {
        input_is_dvd = FALSE;
      }
      else if (!strcmp("-host",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = host_value("ps2ts",argv[ii],argv[ii+1],&output_name,&port);
        if (err) return 1;
        had_output_name = TRUE; // more or less
        use_tcpip = TRUE;
        ii++;
      }
      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("ps2ts",ii);
        err = int_value("ps2ts",argv[ii],argv[ii+1],TRUE,10,&max);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-prepeat",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = int_value("ps2ts",argv[ii],argv[ii+1],TRUE,10,
                        &repeat_program_every);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-pad",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = int_value("ps2ts",argv[ii],argv[ii+1],TRUE,10,&pad_start);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-vpid",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = unsigned_value("ps2ts",argv[ii],argv[ii+1],0,&video_pid);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-apid",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = unsigned_value("ps2ts",argv[ii],argv[ii+1],0,&audio_pid);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-pmt",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = unsigned_value("ps2ts",argv[ii],argv[ii+1],0,&pmt_pid);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-noaudio",argv[ii]))
      {
        keep_audio = FALSE;
      }
      else if (!strcmp("-vstream",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = int_value_in_range("ps2ts",argv[ii],argv[ii+1],0,0xF,0,
                                 &video_stream);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-astream",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = int_value_in_range("ps2ts",argv[ii],argv[ii+1],0,0x1F,0,
                                 &audio_stream);
        if (err) return 1;
        want_ac3_audio = FALSE;
        ii++;
      }
      else if (!strcmp("-ac3stream",argv[ii]))
      {
        CHECKARG("ps2ts",ii);
        err = int_value_in_range("ps2ts",argv[ii],argv[ii+1],0,0x7,0,
                                 &audio_stream);
        if (err) return 1;
        want_ac3_audio = TRUE;
        input_is_dvd = TRUE;
        ii++;
      }
      else
      {
        fprint_err("### ps2ts: "
                   "Unrecognised command line switch '%s'\n",argv[ii]);
        return 1;
      }
    }
    else
    {
      if (had_input_name && had_output_name)
      {
        fprint_err("### ps2ts: Unexpected '%s'\n",argv[ii]);
        return 1;
      }
      else if (had_input_name)
      {
        output_name = argv[ii];
        had_output_name = TRUE;
      }
      else
      {
        input_name = argv[ii];
        had_input_name = TRUE;
      }
    }
    ii++;
  }

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

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

  err = open_PS_file(input_name,quiet,&ps);
  if (err)
  {
    fprint_err("### ps2ts: Unable to open input %s\n",
               (use_stdin?"<stdin>":input_name));
    return 1;
  }

  if (!quiet)
    fprint_msg("Reading from %s\n",(use_stdin?"<stdin>":input_name));

  // Try to decide what sort of data stream we have
  if (force_stream_type || use_stdin)
  {
    if (!quiet)
      fprint_msg("Reading input as %s (0x%02x)\n",
                 h222_stream_type_str(video_type),video_type);
  }
  else
  {
    err = determine_PS_video_type(ps,&video_type);
    if (err) return 1;
    if (!quiet)
      fprint_msg("Video appears to be %s (0x%02x)\n",
                 h222_stream_type_str(video_type),video_type);
  }

  if (!quiet)
  {
    if (input_is_dvd)
      print_msg("Treating input as from DVD\n");
    else
      print_msg("Treating input as NOT from DVD\n");

    print_msg("Reading video from ");
    if (video_stream == -1)
      print_msg("first stream found");
    else
      fprint_msg("stream %0#x (%d)",video_stream,video_stream);
    if (keep_audio)
    {
      print_msg(", audio from ");
      if (audio_stream == -1)
        fprint_msg("first %s found",(want_ac3_audio?"AC3 stream":"stream"));
      else
        fprint_msg("%s %0#x (%d)",(want_ac3_audio?"AC3 stream":"stream"),
               audio_stream,audio_stream);
      print_msg("\n");
    }

    fprint_msg("Writing video with PID 0x%02x",video_pid);
    if (keep_audio)
      fprint_msg(", audio with PID 0x%02x,",audio_pid);
    fprint_msg(" PMT PID 0x%02x, PCR PID 0x%02x\n",pmt_pid,pcr_pid);
    if (max)
      fprint_msg("Stopping after %d program stream packets\n",max);
  }

  if (use_stdout)
    err = tswrite_open(TS_W_STDOUT,NULL,NULL,0,quiet,&output);
  else if (use_tcpip)
    err = tswrite_open(TS_W_TCP,output_name,NULL,port,quiet,&output);
  else
    err = tswrite_open(TS_W_FILE,output_name,NULL,0,quiet,&output);
  if (err)
  {
    fprint_err("### ps2ts: Unable to open %s\n",output_name);
    (void) close_PS_file(&ps);
    return 1;
  }

  err = ps_to_ts(ps,output,pad_start,repeat_program_every,
                 video_type,input_is_dvd,
                 video_stream,audio_stream,want_ac3_audio,
                 want_dolby_as_dvb,pmt_pid,pcr_pid,video_pid,
                 keep_audio,audio_pid,max,verbose,quiet);
  if (err)
  {
    print_err("### ps2ts: Error transferring data\n");
    (void) close_PS_file(&ps);
    (void) tswrite_close(output,TRUE);
    return 1;
  }

  // And tidy up when we're finished
  err = tswrite_close(output,quiet);
  if (err)
    fprint_err("### ps2ts: Error closing output %s: %s\n",output_name,
            strerror(errno));
  err = close_PS_file(&ps);
  if (err)
    fprint_err("### ps2ts: Error closing input %s\n",
               (use_stdin?"<stdin>":input_name));
  return 0;
}
Exemplo n.º 4
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;
}