<commit_before>#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>

#include <ogg/ogg.h>

#include <vorbis/codec.h>

#include <theora/theoradec.h>

#ifdef OPUS
#include <opus/opus_multistream.h>
#include "opus_header.h"
#include "opus_helper.h"
#endif

/* never forget that globals are a one-way ticket to Hell */
/* Ogg and codec state for demux/decode */
ogg_sync_state    oggSyncState;
ogg_page          oggPage;

/* Video decode state */
ogg_stream_state  theoraStreamState;
th_info           theoraInfo;
th_comment        theoraComment;
th_setup_info    *theoraSetupInfo;
th_dec_ctx       *theoraDecoderContext;

int               theora_p=0;
int               theora_processing_headers;

/* single frame video buffering */
int               videobuf_ready=0;
ogg_int64_t       videobuf_granulepos=-1;
double            videobuf_time=0;

int               audiobuf_ready=0;
ogg_int64_t       audiobuf_granulepos=0; /* time position of last sample */

int               raw=0;

/* Audio decode state */
int               vorbis_p = 0;
int               vorbis_processing_headers;
ogg_stream_state  vorbisStreamState;
vorbis_info       vorbisInfo;
vorbis_dsp_state  vorbisDspState;
vorbis_block      vorbisBlock;
vorbis_comment    vorbisComment;

#ifdef OPUS
int               opus_p = 0;
ogg_stream_state  opusStreamState;
OpusMSDecoder    *opusDecoder = NULL;
int               opusMappingFamily;
int               opusChannels;
int               opusPreskip;
ogg_int64_t       opusPrevPacketGranpos;
float             opusGain;
int               opusStreams;
/* 120ms at 48000 */
#define OPUS_MAX_FRAME_SIZE (960*6)
#endif

int               crop = 0;
int               got_sigint = 0;

static void sigint_handler(int signal) {
    got_sigint = 1;
}

// Callbacks
extern void OgvJsLoadedMetadata();

extern void OgvJsInitVideo(int frameWidth, int frameHeight,
                           int hdec, int vdec,
                           double fps,
                           int picWidth, int picHeight,
                           int picX, int picY);
extern void OgvJsOutputFrameReady();
extern void OgvJsOutputFrame(unsigned char *bufferY, int strideY,
                             unsigned char *bufferCb, int strideCb,
                             unsigned char *bufferCr, int strideCr,
                             int width, int height,
                             int hdec, int vdec,
                             double timestamp);

extern void OgvJsInitAudio(int channels, int rate);
extern void OgvJsOutputAudioReady();
extern void OgvJsOutputAudio(float **buffers, int channels, int sampleCount);

/*Write out the planar YUV frame, uncropped.*/
static void video_write(void) {
    th_ycbcr_buffer ycbcr;
    th_decode_ycbcr_out(theoraDecoderContext, ycbcr);

    int hdec = !(theoraInfo.pixel_fmt & 1);
    int vdec = !(theoraInfo.pixel_fmt & 2);

    OgvJsOutputFrame(ycbcr[0].data, ycbcr[0].stride,
            ycbcr[1].data, ycbcr[1].stride,
            ycbcr[2].data, ycbcr[2].stride,
            theoraInfo.frame_width, theoraInfo.frame_height,
            hdec, vdec,
            videobuf_time);
}

/* dump the theora comment header */
static int dump_comments(th_comment *_tc) {
    int i;
    int len;
    printf("Encoded by %s\n", _tc->vendor);
    if (_tc->comments) {
        printf("theora comment header:\n");
        for (i = 0; i < _tc->comments; i++) {
            if (_tc->user_comments[i]) {
                len = _tc->comment_lengths[i] < INT_MAX ? _tc->comment_lengths[i] : INT_MAX;
                printf("\t%.*s\n", len, _tc->user_comments[i]);
            }
        }
    }
    return 0;
}

/* helper: push a page into the appropriate steam */
/* this can be done blindly; a stream won't accept a page
                that doesn't belong to it */
static int queue_page(ogg_page *page) {
    if (theora_p) ogg_stream_pagein(&theoraStreamState, page);
    if (vorbis_p) ogg_stream_pagein(&vorbisStreamState, page);
#ifdef OPUS
    if (opus_p) ogg_stream_pagein(&opusStreamState, page);
#endif
    return 0;
}


ogg_packet oggPacket;
ogg_packet audioPacket;
ogg_packet videoPacket;

int frames = 0;

enum AppState {
    STATE_BEGIN,
    STATE_HEADERS,
    STATE_DECODING
} appState;

int process_audio, process_video;

void OgvJsInit(int process_audio_flag, int process_video_flag) {
	// Allow the caller to specify whether we want audio, video, or both.
	// Or neither, but that won't be very useful.
	process_audio = process_audio_flag;
	process_video = process_video_flag;

	appState = STATE_BEGIN;
	
  /* start up Ogg stream synchronization layer */
  ogg_sync_init(&oggSyncState);

  /* init supporting Vorbis structures needed in header parsing */
  vorbis_info_init(&vorbisInfo);
  vorbis_comment_init(&vorbisComment);

  /* init supporting Theora structures needed in header parsing */
  th_comment_init(&theoraComment);
  th_info_init(&theoraInfo);
}

static void processHeaders();

static void processBegin() {
    if (ogg_page_bos(&oggPage)) {
        printf("Packet is at start of a bitstream\n");
        int got_packet;

        // Initialize a stream state object...
        ogg_stream_state test;
        ogg_stream_init(&test, ogg_page_serialno(&oggPage));<commit_msg>more reformatting<commit_after>#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>

#include <ogg/ogg.h>

#include <vorbis/codec.h>

#include <theora/theoradec.h>

#ifdef OPUS
#include <opus/opus_multistream.h>
#include "opus_header.h"
#include "opus_helper.h"
#endif

/* never forget that globals are a one-way ticket to Hell */
/* Ogg and codec state for demux/decode */
ogg_sync_state    oggSyncState;
ogg_page          oggPage;

/* Video decode state */
ogg_stream_state  theoraStreamState;
th_info           theoraInfo;
th_comment        theoraComment;
th_setup_info    *theoraSetupInfo;
th_dec_ctx       *theoraDecoderContext;

int               theora_p=0;
int               theora_processing_headers;

/* single frame video buffering */
int               videobuf_ready=0;
ogg_int64_t       videobuf_granulepos=-1;
double            videobuf_time=0;

int               audiobuf_ready=0;
ogg_int64_t       audiobuf_granulepos=0; /* time position of last sample */

int               raw=0;

/* Audio decode state */
int               vorbis_p = 0;
int               vorbis_processing_headers;
ogg_stream_state  vorbisStreamState;
vorbis_info       vorbisInfo;
vorbis_dsp_state  vorbisDspState;
vorbis_block      vorbisBlock;
vorbis_comment    vorbisComment;

#ifdef OPUS
int               opus_p = 0;
ogg_stream_state  opusStreamState;
OpusMSDecoder    *opusDecoder = NULL;
int               opusMappingFamily;
int               opusChannels;
int               opusPreskip;
ogg_int64_t       opusPrevPacketGranpos;
float             opusGain;
int               opusStreams;
/* 120ms at 48000 */
#define OPUS_MAX_FRAME_SIZE (960*6)
#endif

int               crop = 0;
int               got_sigint = 0;

static void sigint_handler(int signal) {
    got_sigint = 1;
}

// Callbacks
extern void OgvJsLoadedMetadata();

extern void OgvJsInitVideo(int frameWidth, int frameHeight,
                           int hdec, int vdec,
                           double fps,
                           int picWidth, int picHeight,
                           int picX, int picY);
extern void OgvJsOutputFrameReady();
extern void OgvJsOutputFrame(unsigned char *bufferY, int strideY,
                             unsigned char *bufferCb, int strideCb,
                             unsigned char *bufferCr, int strideCr,
                             int width, int height,
                             int hdec, int vdec,
                             double timestamp);

extern void OgvJsInitAudio(int channels, int rate);
extern void OgvJsOutputAudioReady();
extern void OgvJsOutputAudio(float **buffers, int channels, int sampleCount);

/*Write out the planar YUV frame, uncropped.*/
static void video_write(void) {
    th_ycbcr_buffer ycbcr;
    th_decode_ycbcr_out(theoraDecoderContext, ycbcr);

    int hdec = !(theoraInfo.pixel_fmt & 1);
    int vdec = !(theoraInfo.pixel_fmt & 2);

    OgvJsOutputFrame(ycbcr[0].data, ycbcr[0].stride,
            ycbcr[1].data, ycbcr[1].stride,
            ycbcr[2].data, ycbcr[2].stride,
            theoraInfo.frame_width, theoraInfo.frame_height,
            hdec, vdec,
            videobuf_time);
}

/* dump the theora comment header */
static int dump_comments(th_comment *_tc) {
    int i;
    int len;
    printf("Encoded by %s\n", _tc->vendor);
    if (_tc->comments) {
        printf("theora comment header:\n");
        for (i = 0; i < _tc->comments; i++) {
            if (_tc->user_comments[i]) {
                len = _tc->comment_lengths[i] < INT_MAX ? _tc->comment_lengths[i] : INT_MAX;
                printf("\t%.*s\n", len, _tc->user_comments[i]);
            }
        }
    }
    return 0;
}

/* helper: push a page into the appropriate steam */
/* this can be done blindly; a stream won't accept a page
                that doesn't belong to it */
static int queue_page(ogg_page *page) {
    if (theora_p) ogg_stream_pagein(&theoraStreamState, page);
    if (vorbis_p) ogg_stream_pagein(&vorbisStreamState, page);
#ifdef OPUS
    if (opus_p) ogg_stream_pagein(&opusStreamState, page);
#endif
    return 0;
}


ogg_packet oggPacket;
ogg_packet audioPacket;
ogg_packet videoPacket;

int frames = 0;

enum AppState {
    STATE_BEGIN,
    STATE_HEADERS,
    STATE_DECODING
} appState;

int process_audio, process_video;

void OgvJsInit(int process_audio_flag, int process_video_flag) {
    // Allow the caller to specify whether we want audio, video, or both.
    // Or neither, but that won't be very useful.
    process_audio = process_audio_flag;
    process_video = process_video_flag;

    appState = STATE_BEGIN;

    /* start up Ogg stream synchronization layer */
    ogg_sync_init(&oggSyncState);

    /* init supporting Vorbis structures needed in header parsing */
    vorbis_info_init(&vorbisInfo);
    vorbis_comment_init(&vorbisComment);

    /* init supporting Theora structures needed in header parsing */
    th_comment_init(&theoraComment);
    th_info_init(&theoraInfo);
}

static void processHeaders();

static void processBegin() {
    if (ogg_page_bos(&oggPage)) {
        printf("Packet is at start of a bitstream\n");
        int got_packet;

        // Initialize a stream state object...
        ogg_stream_state test;
        ogg_stream_init(&test, ogg_page_serialno(&oggPage));
{'fraction_non_alphanumeric': 0.09157440420341527, 'fraction_numerical': 0.007881403640457873, 'mean_word_length': 2.574446680080483, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1723390', 'n_tokens_mistral': 3514, 'n_tokens_neox': 3383, 'n_words': 874}