Example #1
0
int AvxContext::OutputAudio() {
	FILE *sink;
	void *writeBuffer = NULL;
	sighandler_t old_sigpipe = signal(SIGPIPE, SIG_IGN);

	if (launchMPlayer) {
		char command[1024];
		if (MPlayerCommandAudio(command))
			return -1;
		AVXLOG_INFO("MPlayer command line: %s", command);

		sink = popen(command, "w");
		if (!sink) {
			AVXLOG_ERROR("%s", "Error starting mplayer");
			return -1;
		}
	} else {
		sink = stdout;
	}

	#define AUDIO_SAMPLES 1000
	try {
		writeBuffer = malloc(vi.BytesPerAudioSample() * AUDIO_SAMPLES);
		if (!writeBuffer) {
			AVXLOG_ERROR("%s", "Unable to allocate memory");
			goto fail;
		}
		for (__int64 i = 0; i < vi.num_audio_samples; i += AUDIO_SAMPLES) {
			if (launchMPlayer && (feof(sink) || ferror(sink))) {
				AVXLOG_ERROR("%s", "mplayer process exited");
				break;
			}
			int read_samples;
			if (vi.num_audio_samples - AUDIO_SAMPLES < i)
				read_samples = vi.num_audio_samples - i;
			else
				read_samples = AUDIO_SAMPLES;
			clip->GetAudio(writeBuffer, i, read_samples, avx_library.env);
			fwrite(writeBuffer, vi.BytesPerAudioSample(), read_samples, sink);
		}
	} catch (AvisynthError &e) {
		AVXLOG_ERROR("AvisynthError: %s", e.msg);
		goto fail;
	}
	#undef AUDIO_SAMPLES

	free(writeBuffer);
	if (launchMPlayer)
		pclose(sink);
	signal(SIGPIPE, old_sigpipe);
	return 0;

fail:
	if (writeBuffer)
		free(writeBuffer);
	if (launchMPlayer)
		pclose(sink);
	signal(SIGPIPE, old_sigpipe);
	return -1;
}
Example #2
0
ConditionalReader::ConditionalReader(PClip _child, const char* filename, const char _varname[], bool _show, IScriptEnvironment* env) :
    GenericVideoFilter(_child), show(_show), variableName(_varname)
{
    FILE * f;
    char *line;
    int lines;

    if ((f = fopen(filename, "rb")) == NULL)
        env->ThrowError("ConditionalReader: Could not open file '%s'.", filename);

    lines = 0;
    mode = MODE_UNKNOWN;

    while ((line = readline(f)) != NULL) {
        char *ptr;
        int fields;

        lines++;

        /* We skip spaces */
        ptr = skipspaces(line);

        /* Skip coment lines or empty lines */
        if(iscomment(ptr) || *ptr == '\0') {
            free(line);
            continue;
        }

        if (mode == MODE_UNKNOWN) {
            // We have not recieved a mode - We expect type.
            char keyword [1024];
            char type [1024];
            fields = sscanf(ptr,"%1023s %1023s", keyword, type);
            if (fields) {
                if (!strcasecmp((const char*)keyword, "type")) {
                    if (!strcasecmp((const char*)type, "int")) {
                        mode = MODE_INT;
                        intVal = new int[vi.num_frames];
                    } else if (!strcasecmp((const char*)type, "float")) {
                        mode = MODE_FLOAT;
                        floatVal = new float[vi.num_frames];
                    } else if (!strcasecmp((const char*)type, "bool")) {
                        mode = MODE_BOOL;
                        boolVal = new bool[vi.num_frames];
                    } else {
                        ThrowLine("ConditionalReader: Unknown 'type' specified in line %d", lines, env);
                    }// end if compare type
                }// end if compare keyword
            }// end if fields

        } else { // We have a defined mode and allocated the values.

            char keyword [1024];
            char type [1024];
            fields = sscanf(ptr,"%1023s %1023s", keyword, type);

            if (!strcasecmp((const char*)keyword, "default")) {
                AVSValue def = ConvertType((const char*)type, lines, env);
                SetRange(0, vi.num_frames-1, def);
                free(line);
                continue;
            } // end if "default"

            if (ptr[0] == 'R' || ptr[0] == 'r') {  // Range
                ptr++;
                ptr = skipspaces(ptr);
                int start;
                int stop;
                char value [64];
                fields = sscanf(ptr, "%d %d %63s", &start, &stop, value);

                if (fields != 3)
                    ThrowLine("ConditionalReader: Could not read range in line %d", lines, env);
                if (start > stop)
                    ThrowLine("ConditionalReader: The start frame is after the end frame in line %d", lines, env);

                AVSValue set = ConvertType((const char*)value, lines, env);
                SetRange(start, stop, set);
            } else if (ptr[0] == 'I' || ptr[0] == 'i') {  // Interpolate
                if (mode == MODE_BOOL)
                    ThrowLine("ConditionalReader: Cannot interpolate booleans in line %d", lines, env);

                ptr++;
                ptr = skipspaces(ptr);
                int start;
                int stop;
                char start_value [64];
                char stop_value [64];
                fields = sscanf(ptr, "%d %d %63s %63s", &start, &stop, start_value, stop_value);

                if (fields != 4)
                    ThrowLine("ConditionalReader: Could not read interpolation range in line %d", lines, env);
                if (start > stop)
                    ThrowLine("ConditionalReader: The start frame is after the end frame in line %d", lines, env);

                AVSValue set_start = ConvertType((const char*)start_value, lines, env);
                AVSValue set_stop = ConvertType((const char*)stop_value, lines, env);

                int range = stop-start;
                double diff = set_stop.AsFloat() - set_start.AsFloat();
                for (int i = 0; i<=range; i++) {
                    double where = (double)(i)/(double)range;
                    double n = where * diff + set_start.AsFloat();
                    SetFrame(i+start, (mode == MODE_FLOAT)
                             ? AVSValue(n)
                             : AVSValue((int) n));
                }
            } else {
                char value [64];
                int cframe;
                fields = sscanf(ptr, "%d %63s", &cframe, value);
                if (fields == 2) {
                    AVSValue set = ConvertType((const char*)value, lines, env);
                    SetFrame(cframe, set);
                } else {
                    AVXLOG_INFO("ConditionalReader: Ignored line %d.\n", lines);
                }
            }

        } // End we have defined type
        free(line);
    }// end while still some file left to read.

    /* We are done with the file */
    fclose(f);

    if (mode == MODE_UNKNOWN)
        env->ThrowError("ConditionalReader: Mode was not defined!");

}
Example #3
0
int AvxContext::OutputVideo() {
	FILE *sink;
	unsigned char *writeBuffer = NULL;
	sighandler_t old_sigpipe = signal(SIGPIPE, SIG_IGN);

	if (launchMPlayer) {
		char command[1024];
		if (MPlayerCommandVideo(command))
			return -1;
		AVXLOG_INFO("MPlayer command line: %s", command);

		sink = popen(command, "w");
		if (!sink) {
			AVXLOG_ERROR("%s", "Error starting mplayer");
			return -1;
		}
	} else {
		sink = stdout;
	}

	writeBuffer = (unsigned char *)malloc(vi.RowSize() * vi.height);
	if (!writeBuffer) {
		AVXLOG_ERROR("%s", "Unable to allocate memory");
		goto fail;
	}

	try {
		for (int i = 0; i < vi.num_frames; ++i) {
			if (launchMPlayer && (feof(sink) || ferror(sink))) {
				AVXLOG_ERROR("%s", "mplayer process exited");
				break;
			}
			PVideoFrame frame = clip->GetFrame(i, avx_library.env);
			if (vi.IsPlanar()) { // Check plane count in 2.6.
				int planes[] = {PLANAR_Y, PLANAR_V, PLANAR_U};
				for (int j = 0; j < 3; ++j) {
					int plane = planes[j];
					int src_pitch = frame->GetPitch(plane);
					int row_size = frame->GetRowSize(plane);
					int height = frame->GetHeight(plane);
					const unsigned char *srcp = frame->GetReadPtr(plane);

					avx_library.env->BitBlt(writeBuffer, row_size, srcp, src_pitch, row_size, height);
					fwrite(writeBuffer, 1, row_size * height, sink);
				}
			} else {
				int src_pitch = frame->GetPitch();
				int row_size = frame->GetRowSize();
				int height = frame->GetHeight();
				const unsigned char *srcp = frame->GetReadPtr();

				avx_library.env->BitBlt(writeBuffer, row_size, srcp, src_pitch, row_size, height);
				fwrite(writeBuffer, 1, row_size * height, sink);
			}
		}
	} catch (AvisynthError &e) {
		AVXLOG_ERROR("AvisynthError: %s", e.msg);
		goto fail;
	}

	free(writeBuffer);
	if (launchMPlayer)
		pclose(sink);
	signal(SIGPIPE, old_sigpipe);
	return 0;

fail:
	if (writeBuffer)
		free(writeBuffer);
	if (launchMPlayer)
		pclose(sink);
	signal(SIGPIPE, old_sigpipe);
	return -1;
};