<commit_before>/*
 *  Copyright (c) 2009 <NAME>
 *
 *  This is free software; see gpl-3.0.txt
 */

#include <libavformat/avformat.h>
#include <stdbool.h>

#include "../dbg.h"
#include "../input-stream.h"
#include "../payload.h"
#include "../input.h"		//TODO: for flags. Check if we can do something smarter

#define STATIC_BUFF_SIZE 1000 * 1024
#define HEADER_REFRESH_PERIOD 50

struct input_stream {
  AVFormatContext *s;
  bool loop;	//loop on input file infinitely
  int audio_stream;
  int video_stream;
  int64_t last_ts;
  int64_t base_ts;
  int frames_since_global_headers;
};

static uint8_t codec_type(enum CodecID cid)
{
  switch (cid) {
    case CODEC_ID_MPEG1VIDEO:
    case CODEC_ID_MPEG2VIDEO:
      return 1;
    case CODEC_ID_H261:
      return 2;
    case CODEC_ID_H263P:
    case CODEC_ID_H263:
      return 3;
    case CODEC_ID_MJPEG:
      return 4;
    case CODEC_ID_MPEG4:
      return 5;
    case CODEC_ID_FLV1:
      return 6;
    case CODEC_ID_SVQ3:
      return 7;
    case CODEC_ID_DVVIDEO:
      return 8;
    case CODEC_ID_H264:
      return 9;
    case CODEC_ID_THEORA:
    case CODEC_ID_VP3:
      return 10;
    case CODEC_ID_SNOW:
      return 11;
    case CODEC_ID_VP6:
      return 12;
    case CODEC_ID_DIRAC:
      return 13;
    default:
      fprintf(stderr, "Unknown codec ID %d\n", cid);
      return 0;
  }
}

static void video_header_fill(uint8_t *data, AVStream *st)
{
  int num, den;

  num = st->avg_frame_rate.num;
  den = st->avg_frame_rate.den;
//fprintf(stderr, "Rate: %d/%d\n", num, den);
  if (num == 0) {
    num = st->r_frame_rate.num;
    den = st->r_frame_rate.den;
  }
  if (num > (1 << 16)) {
    num /= 1000;
    den /= 1000;
  }
  payload_header_write(data, codec_type(st->codec->codec_id), st->codec->width, st->codec->height, num, den);
}

static void frame_header_fill(uint8_t *data, int size, AVPacket *pkt, AVStream *st, int64_t base_ts)
{
  AVRational fps;
  int32_t pts, dts;

  fps = st->avg_frame_rate;
  if (fps.num == 0) {
    fps = st->r_frame_rate;
  }
  if (pkt->pts != AV_NOPTS_VALUE) {
    pts = av_rescale_q(pkt->pts, st->time_base, (AVRational){fps.den, fps.num}),
    pts += av_rescale_q(base_ts, AV_TIME_BASE_Q, (AVRational){fps.den, fps.num});
  } else {
    pts = -1;
  }
  dprintf("pkt->pts=%ld PTS=%d",pkt->pts, pts);
  if (pkt->dts != AV_NOPTS_VALUE) {
    dts = av_rescale_q(pkt->dts, st->time_base, (AVRational){fps.den, fps.num});
    dts += av_rescale_q(base_ts, AV_TIME_BASE_Q, (AVRational){fps.den, fps.num});
  } else {
    fprintf(stderr, "No DTS???\n");
    dts = 0;
  }
  dprintf(" DTS=%d\n",dts);
  frame_header_write(data, size, pts, dts);
}

struct input_stream *input_stream_open(const char *fname, int *period, uint16_t flags)
{
  struct input_stream *desc;
  int i, res;

  avcodec_register_all();
  av_register_all();

  desc = malloc(sizeof(struct input_stream));
  if (desc == NULL) {
    return NULL;
  }
  res = av_open_input_file(&desc->s, fname, NULL, 0, NULL);
  if (res < 0) {
    fprintf(stderr, "Error opening %s: %d\n", fname, res);

    return NULL;
  }

  res = av_find_stream_info(desc->s);
  if (res < 0) {
    fprintf(stderr, "Cannot find codec parameters for %s\n", fname);

    return NULL;
  }
  desc->video_stream = -1;
  desc->audio_stream = -1;
  desc->last_ts = 0;
  desc->base_ts = 0;
  desc->frames_since_global_headers = 0;
  desc->loop = flags & INPUT_LOOP;
  for (i = 0; i < desc->s->nb_streams; i++) {
    if (desc->video_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
      desc->video_stream = i;
      fprintf(stderr, "Video Frame Rate = %d/%d --- Period: %lld\n",
              desc->s->streams[i]->r_frame_rate.num,
              desc->s->streams[i]->r_frame_rate.den,
              av_rescale(1000000, desc->s->streams[i]->r_frame_rate.den, desc->s->streams[i]->r_frame_rate.num));
      *period = av_rescale(1000000, desc->s->streams[i]->r_frame_rate.den, desc->s->streams[i]->r_frame_rate.num);
    }
    if (desc->audio_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
      desc->audio_stream = i;<commit_msg>Set AVFMT_FLAG_GENPTS, to avoid having pts == AV_NOPTS_VALUE<commit_after>/*
 *  Copyright (c) 2009 <NAME>
 *
 *  This is free software; see gpl-3.0.txt
 */

#include <libavformat/avformat.h>
#include <stdbool.h>

#include "../dbg.h"
#include "../input-stream.h"
#include "../payload.h"
#include "../input.h"		//TODO: for flags. Check if we can do something smarter

#define STATIC_BUFF_SIZE 1000 * 1024
#define HEADER_REFRESH_PERIOD 50

struct input_stream {
  AVFormatContext *s;
  bool loop;	//loop on input file infinitely
  int audio_stream;
  int video_stream;
  int64_t last_ts;
  int64_t base_ts;
  int frames_since_global_headers;
};

static uint8_t codec_type(enum CodecID cid)
{
  switch (cid) {
    case CODEC_ID_MPEG1VIDEO:
    case CODEC_ID_MPEG2VIDEO:
      return 1;
    case CODEC_ID_H261:
      return 2;
    case CODEC_ID_H263P:
    case CODEC_ID_H263:
      return 3;
    case CODEC_ID_MJPEG:
      return 4;
    case CODEC_ID_MPEG4:
      return 5;
    case CODEC_ID_FLV1:
      return 6;
    case CODEC_ID_SVQ3:
      return 7;
    case CODEC_ID_DVVIDEO:
      return 8;
    case CODEC_ID_H264:
      return 9;
    case CODEC_ID_THEORA:
    case CODEC_ID_VP3:
      return 10;
    case CODEC_ID_SNOW:
      return 11;
    case CODEC_ID_VP6:
      return 12;
    case CODEC_ID_DIRAC:
      return 13;
    default:
      fprintf(stderr, "Unknown codec ID %d\n", cid);
      return 0;
  }
}

static void video_header_fill(uint8_t *data, AVStream *st)
{
  int num, den;

  num = st->avg_frame_rate.num;
  den = st->avg_frame_rate.den;
//fprintf(stderr, "Rate: %d/%d\n", num, den);
  if (num == 0) {
    num = st->r_frame_rate.num;
    den = st->r_frame_rate.den;
  }
  if (num > (1 << 16)) {
    num /= 1000;
    den /= 1000;
  }
  payload_header_write(data, codec_type(st->codec->codec_id), st->codec->width, st->codec->height, num, den);
}

static void frame_header_fill(uint8_t *data, int size, AVPacket *pkt, AVStream *st, int64_t base_ts)
{
  AVRational fps;
  int32_t pts, dts;

  fps = st->avg_frame_rate;
  if (fps.num == 0) {
    fps = st->r_frame_rate;
  }
  if (pkt->pts != AV_NOPTS_VALUE) {
    pts = av_rescale_q(pkt->pts, st->time_base, (AVRational){fps.den, fps.num}),
    pts += av_rescale_q(base_ts, AV_TIME_BASE_Q, (AVRational){fps.den, fps.num});
  } else {
    pts = -1;
  }
  dprintf("pkt->pts=%ld PTS=%d",pkt->pts, pts);
  if (pkt->dts != AV_NOPTS_VALUE) {
    dts = av_rescale_q(pkt->dts, st->time_base, (AVRational){fps.den, fps.num});
    dts += av_rescale_q(base_ts, AV_TIME_BASE_Q, (AVRational){fps.den, fps.num});
  } else {
    fprintf(stderr, "No DTS???\n");
    dts = 0;
  }
  dprintf(" DTS=%d\n",dts);
  frame_header_write(data, size, pts, dts);
}

struct input_stream *input_stream_open(const char *fname, int *period, uint16_t flags)
{
  struct input_stream *desc;
  int i, res;

  avcodec_register_all();
  av_register_all();

  desc = malloc(sizeof(struct input_stream));
  if (desc == NULL) {
    return NULL;
  }
  res = av_open_input_file(&desc->s, fname, NULL, 0, NULL);
  if (res < 0) {
    fprintf(stderr, "Error opening %s: %d\n", fname, res);

    return NULL;
  }

  desc->s->flags |= AVFMT_FLAG_GENPTS;
  res = av_find_stream_info(desc->s);
  if (res < 0) {
    fprintf(stderr, "Cannot find codec parameters for %s\n", fname);

    return NULL;
  }
  desc->video_stream = -1;
  desc->audio_stream = -1;
  desc->last_ts = 0;
  desc->base_ts = 0;
  desc->frames_since_global_headers = 0;
  desc->loop = flags & INPUT_LOOP;
  for (i = 0; i < desc->s->nb_streams; i++) {
    if (desc->video_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
      desc->video_stream = i;
      fprintf(stderr, "Video Frame Rate = %d/%d --- Period: %lld\n",
              desc->s->streams[i]->r_frame_rate.num,
              desc->s->streams[i]->r_frame_rate.den,
              av_rescale(1000000, desc->s->streams[i]->r_frame_rate.den, desc->s->streams[i]->r_frame_rate.num));
      *period = av_rescale(1000000, desc->s->streams[i]->r_frame_rate.den, desc->s->streams[i]->r_frame_rate.num);
    }
    if (desc->audio_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
      desc->audio_stream = i;
{'fraction_non_alphanumeric': 0.15169756802311582, 'fraction_numerical': 0.02552371779436552, 'mean_word_length': 3.155577788894447, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 133, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7291855', 'n_tokens_mistral': 3537, 'n_tokens_neox': 3388, 'n_words': 707}