<commit_before>/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/opt.h>
#include <ffmpeg_mediametadataretriever.h>

const int TARGET_IMAGE_FORMAT = PIX_FMT_RGB24;
const int TARGET_IMAGE_CODEC = CODEC_ID_PNG;

const char *DURATION = "duration";
const char *AUDIO_CODEC = "audio_codec";
const char *VIDEO_CODEC = "video_codec";
const char *ICY_METADATA = "icy_metadata";
//const char *ICY_ARTIST = "icy_artist";
//const char *ICY_TITLE = "icy_title";

const int SUCCESS = 0;
const int FAILURE = -1;

void convert_image(AVCodecContext *pCodecCtx, AVFrame *pFrame, AVPacket *avpkt, int *got_packet_ptr);

int is_supported_format(int codec_id) {
	if (codec_id == CODEC_ID_PNG ||
			codec_id == CODEC_ID_MJPEG ||
	        codec_id == CODEC_ID_BMP) {
		return 1;
	}
	
	return 0;
}

size_t first_char_pos(const char *value, const char ch) {
    char *chptr = strchr(value, ch);
    return chptr - value;
}

size_t last_char_pos(const char *value, const char ch) {
    char *chptr = strrchr(value, ch);
    return chptr - value;
}

void get_shoutcast_metadata(AVFormatContext *ic) {
    char *value = NULL;
    
    if (av_opt_get(ic, "icy_metadata_packet", 1, (uint8_t **) &value) < 0) {
        value = NULL;
    }
	
    if (value && value[0]) {
    	av_dict_set(&ic->metadata, ICY_METADATA, value, 0);
    	
    	/*int first_pos = first_char_pos(value, '\'');
        int last_pos = first_char_pos(value, ';') - 2;
        int pos = last_pos - first_pos;
        
        if (pos == 0) {
        	return;
        }
        
        char temp[pos];
        memcpy(temp, value + first_pos + 1 , pos);
        temp[pos] = '\0';
        value = temp;

    	first_pos = first_char_pos(value, '-') - 1;
        
        char artist[first_pos];
        memcpy(artist, value, first_pos);
        artist[first_pos] = '\0';
        
		av_dict_set(&ic->metadata, ICY_ARTIST, artist, 0);
        
        pos = strlen(value) - first_char_pos(value, '-') + 2;
         
        char album[pos];
        memcpy(album, value + first_char_pos(value, '-') + 2, pos);
        album[pos] = '\0';
        
		av_dict_set(&ic->metadata, ICY_TITLE, album, 0);*/
    }
}

void get_duration(AVFormatContext *ic, char * value) {
	int duration = 0;

	if (ic) {
		if (ic->duration != AV_NOPTS_VALUE) {
			duration = ((ic->duration / AV_TIME_BASE) * 1000);
		}
	}

	sprintf(value, "%d", duration); // %i
}

void set_codec(AVFormatContext *ic, int i) {
    const char *codec_type = av_get_media_type_string(ic->streams[i]->codec->codec_type);

	if (!codec_type) {
		return;
	}

    const char *codec_name = avcodec_get_name(ic->streams[i]->codec->codec_id);

	if (strcmp(codec_type, "audio") == 0) {
		av_dict_set(&ic->metadata, AUDIO_CODEC, codec_name, 0);
    } else if (codec_type && strcmp(codec_type, "video") == 0) {
	   	av_dict_set(&ic->metadata, VIDEO_CODEC, codec_name, 0);
	}
}

int stream_component_open(State *s, int stream_index) {
	AVFormatContext *pFormatCtx = s->pFormatCtx;
	AVCodecContext *codecCtx;
	AVCodec *codec;

	if (stream_index < 0 || stream_index >= pFormatCtx->nb_streams) {
		return FAILURE;
	}

	// Get a pointer to the codec context for the stream
	codecCtx = pFormatCtx->streams[stream_index]->codec;

	printf("avcodec_find_decoder %s\n", codecCtx->codec_name);

	// Find the decoder for the audio stream
	codec = avcodec_find_decoder(codecCtx->codec_id);

	if(codec == NULL) {
	    printf("avcodec_find_decoder() failed to find audio decoder\n");
	    return FAILURE;
	}

	// Open the codec
    if (!codec || (avcodec_open2(codecCtx, codec, NULL) < 0)) {
	  	printf("avcodec_open2() failed\n");
		return FAILURE;
	}

	switch(codecCtx->codec_type) {
		case AVMEDIA_TYPE_AUDIO:
			s->audio_stream = stream_index;
		    s->audio_st = pFormatCtx->streams[stream_index];
			break;
		case AVMEDIA_TYPE_VIDEO:
			s->video_stream = stream_index;
		    s->video_st = pFormatCtx->streams[stream_index];
			break;
		default:
			break;
	}

	return SUCCESS;
}<commit_msg>Don't attempt to open codecs<commit_after>/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/opt.h>
#include <ffmpeg_mediametadataretriever.h>

const int TARGET_IMAGE_FORMAT = PIX_FMT_RGB24;
const int TARGET_IMAGE_CODEC = CODEC_ID_PNG;

const char *DURATION = "duration";
const char *AUDIO_CODEC = "audio_codec";
const char *VIDEO_CODEC = "video_codec";
const char *ICY_METADATA = "icy_metadata";
//const char *ICY_ARTIST = "icy_artist";
//const char *ICY_TITLE = "icy_title";

const int SUCCESS = 0;
const int FAILURE = -1;

void convert_image(AVCodecContext *pCodecCtx, AVFrame *pFrame, AVPacket *avpkt, int *got_packet_ptr);

int is_supported_format(int codec_id) {
	if (codec_id == CODEC_ID_PNG ||
			codec_id == CODEC_ID_MJPEG ||
	        codec_id == CODEC_ID_BMP) {
		return 1;
	}
	
	return 0;
}

size_t first_char_pos(const char *value, const char ch) {
    char *chptr = strchr(value, ch);
    return chptr - value;
}

size_t last_char_pos(const char *value, const char ch) {
    char *chptr = strrchr(value, ch);
    return chptr - value;
}

void get_shoutcast_metadata(AVFormatContext *ic) {
    char *value = NULL;
    
    if (av_opt_get(ic, "icy_metadata_packet", 1, (uint8_t **) &value) < 0) {
        value = NULL;
    }
	
    if (value && value[0]) {
    	av_dict_set(&ic->metadata, ICY_METADATA, value, 0);
    	
    	/*int first_pos = first_char_pos(value, '\'');
        int last_pos = first_char_pos(value, ';') - 2;
        int pos = last_pos - first_pos;
        
        if (pos == 0) {
        	return;
        }
        
        char temp[pos];
        memcpy(temp, value + first_pos + 1 , pos);
        temp[pos] = '\0';
        value = temp;

    	first_pos = first_char_pos(value, '-') - 1;
        
        char artist[first_pos];
        memcpy(artist, value, first_pos);
        artist[first_pos] = '\0';
        
		av_dict_set(&ic->metadata, ICY_ARTIST, artist, 0);
        
        pos = strlen(value) - first_char_pos(value, '-') + 2;
         
        char album[pos];
        memcpy(album, value + first_char_pos(value, '-') + 2, pos);
        album[pos] = '\0';
        
		av_dict_set(&ic->metadata, ICY_TITLE, album, 0);*/
    }
}

void get_duration(AVFormatContext *ic, char * value) {
	int duration = 0;

	if (ic) {
		if (ic->duration != AV_NOPTS_VALUE) {
			duration = ((ic->duration / AV_TIME_BASE) * 1000);
		}
	}

	sprintf(value, "%d", duration); // %i
}

void set_codec(AVFormatContext *ic, int i) {
    const char *codec_type = av_get_media_type_string(ic->streams[i]->codec->codec_type);

	if (!codec_type) {
		return;
	}

    const char *codec_name = avcodec_get_name(ic->streams[i]->codec->codec_id);

	if (strcmp(codec_type, "audio") == 0) {
		av_dict_set(&ic->metadata, AUDIO_CODEC, codec_name, 0);
    } else if (codec_type && strcmp(codec_type, "video") == 0) {
	   	av_dict_set(&ic->metadata, VIDEO_CODEC, codec_name, 0);
	}
}

int stream_component_open(State *s, int stream_index) {
	AVFormatContext *pFormatCtx = s->pFormatCtx;
	AVCodecContext *codecCtx;
	AVCodec *codec;

	if (stream_index < 0 || stream_index >= pFormatCtx->nb_streams) {
		return FAILURE;
	}

	// Get a pointer to the codec context for the stream
	codecCtx = pFormatCtx->streams[stream_index]->codec;

	printf("avcodec_find_decoder %s\n", codecCtx->codec_name);

	// Find the decoder for the audio stream
	/*codec = avcodec_find_decoder(codecCtx->codec_id);

	if(codec == NULL) {
	    printf("avcodec_find_decoder() failed to find audio decoder\n");
	    return FAILURE;
	}

	// Open the codec
    if (!codec || (avcodec_open2(codecCtx, codec, NULL) < 0)) {
	  	printf("avcodec_open2() failed\n");
		return FAILURE;
	}*/

	switch(codecCtx->codec_type) {
		case AVMEDIA_TYPE_AUDIO:
			s->audio_stream = stream_index;
		    s->audio_st = pFormatCtx->streams[stream_index];
			break;
		case AVMEDIA_TYPE_VIDEO:
			s->video_stream = stream_index;
		    s->video_st = pFormatCtx->streams[stream_index];
			break;
		default:
			break;
	}

	return SUCCESS;
}
{'fraction_non_alphanumeric': 0.12869806685891275, 'fraction_numerical': 0.009185090248851864, 'mean_word_length': 3.3900609470229726, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5097874', 'n_tokens_mistral': 3504, 'n_tokens_neox': 3339, 'n_words': 822}