DWORD WINAPI threadAvsDec(LPVOID id) { BYTE pUVrow[info->width]; for (int f = 0; f < info->num_frames; f++) { while (BufferIsFull(frameBuffer)) Sleep(250); // only bad if encodes more than 1000fps, then decrease AVS_VideoFrame *frame = avs_get_frame(clip, f); const BYTE *pYplane = avs_get_read_ptr_p(frame, AVS_PLANAR_Y); const BYTE *pUplane = avs_get_read_ptr_p(frame, AVS_PLANAR_U); const BYTE *pVplane = avs_get_read_ptr_p(frame, AVS_PLANAR_V); BYTE *frameData = (BYTE*) malloc(hostPtrSize); BYTE *pBuf = frameData; // Y plane unsigned int pitch = avs_get_pitch_p(frame, AVS_PLANAR_Y); for (int h = 0; h < info->height; h++) { memcpy(pBuf, pYplane, info->width); pBuf += alignedSurfaceWidth; pYplane += pitch; } // UV planes unsigned int uiHalfHeight = info->height >> 1; unsigned int uiHalfWidth = info->width >> 1; //chromaWidth unsigned int pos = 0; for (unsigned int h = 0; h < uiHalfHeight; h++) { for (unsigned int i = 0; i < uiHalfWidth; ++i) { pUVrow[i*2] = pUplane[pos + i]; pUVrow[i*2 + 1] = pVplane[pos + i]; } memcpy(pBuf, pUVrow, info->width); pBuf += alignedSurfaceWidth; pos += uiHalfWidth; } BufferWrite(frameBuffer, (BufferType)frameData); // not sure release is needed, but it doesn't cause an error avs_release_frame(frame); } return 0; }
static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame ) { static const int plane[3] = { AVS_PLANAR_Y, AVS_PLANAR_U, AVS_PLANAR_V }; avs_hnd_t *h = handle; if( i_frame >= h->num_frames ) return -1; AVS_VideoFrame *frm = pic->opaque = h->func.avs_get_frame( h->clip, i_frame ); const char *err = h->func.avs_clip_get_error( h->clip ); FAIL_IF_ERROR( err, "%s occurred while reading frame %d\n", err, i_frame ); for( int i = 0; i < pic->img.planes; i++ ) { /* explicitly cast away the const attribute to avoid a warning */ pic->img.plane[i] = (uint8_t*)avs_get_read_ptr_p( frm, plane[i] ); pic->img.stride[i] = avs_get_pitch_p( frm, plane[i] ); } return 0; }
static int read_video( lsmash_handler_t *h, int sample_number, void *buf ) { avs_handler_t *hp = (avs_handler_t *)h->video_private; AVS_VideoFrame *as_frame = hp->func.avs_get_frame( hp->clip, sample_number ); if( hp->func.avs_clip_get_error( hp->clip ) ) return 0; if( avs_is_interleaved( hp->vi ) ) { uint8_t *read_ptr = (uint8_t *)avs_get_read_ptr( as_frame ); int pitch = avs_get_pitch( as_frame ); if( avs_is_rgb( hp->vi ) ) { hp->av_frame->data [0] = read_ptr + pitch * (hp->vi->height - 1); hp->av_frame->linesize[0] = -pitch; } else { hp->av_frame->data [0] = read_ptr; hp->av_frame->linesize[0] = pitch; } } else for( int i = 0; i < 3; i++ ) { static const int as_plane[3] = { AVS_PLANAR_Y, AVS_PLANAR_U, AVS_PLANAR_V }; hp->av_frame->data [i] = (uint8_t *)avs_get_read_ptr_p( as_frame, as_plane[i] ); hp->av_frame->linesize[i] = avs_get_pitch_p( as_frame, as_plane[i] ); } hp->av_frame->width = hp->vi->width; hp->av_frame->height = hp->vi->height; hp->av_frame->format = as_to_av_input_pixel_format( hp->vi->pixel_type, hp->bit_depth, &hp->av_frame->width ); hp->av_frame->color_range = AVCOL_RANGE_UNSPECIFIED; hp->av_frame->colorspace = AVCOL_SPC_UNSPECIFIED; /* Here, update_scaler_configuration_if_needed() is required to activate the scaler. */ if( update_scaler_configuration_if_needed( &hp->voh.scaler, NULL, hp->av_frame ) < 0 ) return 0; int frame_size = convert_colorspace( &hp->voh, hp->av_frame, buf ); hp->func.avs_release_video_frame( as_frame ); return frame_size; }
/* Copy AviSynth clip data into an AVPacket. */ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, int discard) { AviSynthContext *avs = s->priv_data; AVS_VideoFrame *frame; unsigned char *dst_p; const unsigned char *src_p; int n, i, plane, rowsize, planeheight, pitch, bits; const char *error; if (avs->curr_frame >= avs->vi->num_frames) return AVERROR_EOF; /* This must happen even if the stream is discarded to prevent desync. */ n = avs->curr_frame++; if (discard) return 0; #ifdef USING_AVISYNTH /* Define the bpp values for the new AviSynth 2.6 colorspaces. * Since AvxSynth doesn't have these functions, special-case * it in order to avoid implicit declaration errors. */ if (avs_library.avs_is_yv24(avs->vi)) bits = 24; else if (avs_library.avs_is_yv16(avs->vi)) bits = 16; else if (avs_library.avs_is_yv411(avs->vi)) bits = 12; else if (avs_library.avs_is_y8(avs->vi)) bits = 8; else bits = avs_library.avs_bits_per_pixel(avs->vi); #else bits = avs_bits_per_pixel(avs->vi); #endif /* Without the cast to int64_t, calculation overflows at about 9k x 9k * resolution. */ pkt->size = (((int64_t)avs->vi->width * (int64_t)avs->vi->height) * bits) / 8; if (!pkt->size) return AVERROR_UNKNOWN; if (av_new_packet(pkt, pkt->size) < 0) return AVERROR(ENOMEM); pkt->pts = n; pkt->dts = n; pkt->duration = 1; pkt->stream_index = avs->curr_stream; frame = avs_library.avs_get_frame(avs->clip, n); error = avs_library.avs_clip_get_error(avs->clip); if (error) { av_log(s, AV_LOG_ERROR, "%s\n", error); avs->error = 1; av_packet_unref(pkt); return AVERROR_UNKNOWN; } dst_p = pkt->data; for (i = 0; i < avs->n_planes; i++) { plane = avs->planes[i]; #ifdef USING_AVISYNTH src_p = avs_library.avs_get_read_ptr_p(frame, plane); pitch = avs_library.avs_get_pitch_p(frame, plane); rowsize = avs_library.avs_get_row_size_p(frame, plane); planeheight = avs_library.avs_get_height_p(frame, plane); #else src_p = avs_get_read_ptr_p(frame, plane); pitch = avs_get_pitch_p(frame, plane); rowsize = avs_get_row_size_p(frame, plane); planeheight = avs_get_height_p(frame, plane); #endif /* Flip RGB video. */ if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) { src_p = src_p + (planeheight - 1) * pitch; pitch = -pitch; } avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight); dst_p += rowsize * planeheight; } avs_library.avs_release_video_frame(frame); return 0; }
// Copy AviSynth clip data into an AVPacket. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, int discard) { AviSynthContext *avs = s->priv_data; AVS_VideoFrame *frame; unsigned char *dst_p; const unsigned char *src_p; int n, i, plane, rowsize, planeheight, pitch, bits; const char *error; if (avs->curr_frame >= avs->vi->num_frames) return AVERROR_EOF; // This must happen even if the stream is discarded to prevent desync. n = avs->curr_frame++; if (discard) return 0; pkt->pts = n; pkt->dts = n; pkt->duration = 1; // Define the bpp values for the new AviSynth 2.6 colorspaces if (avs_is_yv24(avs->vi)) { bits = 24; } else if (avs_is_yv16(avs->vi)) { bits = 16; } else if (avs_is_yv411(avs->vi)) { bits = 12; } else if (avs_is_y8(avs->vi)) { bits = 8; } else { bits = avs_bits_per_pixel(avs->vi); } // Without cast to int64_t, calculation overflows at about 9k x 9k resolution. pkt->size = (((int64_t)avs->vi->width * (int64_t)avs->vi->height) * bits) / 8; if (!pkt->size) return AVERROR_UNKNOWN; pkt->data = av_malloc(pkt->size); if (!pkt->data) return AVERROR_UNKNOWN; frame = avs_library->avs_get_frame(avs->clip, n); error = avs_library->avs_clip_get_error(avs->clip); if (error) { av_log(s, AV_LOG_ERROR, "%s\n", error); avs->error = 1; av_freep(&pkt->data); return AVERROR_UNKNOWN; } dst_p = pkt->data; for (i = 0; i < avs->n_planes; i++) { plane = avs->planes[i]; src_p = avs_get_read_ptr_p(frame, plane); rowsize = avs_get_row_size_p(frame, plane); planeheight = avs_get_height_p(frame, plane); pitch = avs_get_pitch_p(frame, plane); // Flip RGB video. if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) { src_p = src_p + (planeheight - 1) * pitch; pitch = -pitch; } // An issue with avs_bit_blt on 2.5.8 prevents video from working correctly. // This problem doesn't exist for 2.6 and AvxSynth, so enable the workaround // for 2.5.8 only. This only displays the warning and exits if the script has // video. 2.5.8's internal interface version is 3, so avs_get_version allows // it to work only in the circumstance that the interface is 5 or higher (4 is // unused). There's a strong chance that AvxSynth, having been based on 2.5.8, // would also be identified as interface version 3, but since AvxSynth doesn't // suffer from this problem, special-case it. #ifdef _WIN32 if (avs_library->avs_get_version(avs->clip) > 3) { avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight); } else { av_log(s, AV_LOG_ERROR, "Video input from AviSynth 2.5.8 is not supported. Please upgrade to 2.6.\n"); avs->error = 1; av_freep(&pkt->data); return AVERROR_UNKNOWN; } #else avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight); #endif dst_p += rowsize * planeheight; } avs_library->avs_release_video_frame(frame); return 0; }
// Copy AviSynth clip data into an AVPacket. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, int discard) { AviSynthContext *avs = s->priv_data; AVS_VideoFrame *frame; unsigned char *dst_p; const unsigned char *src_p; int n, i, plane, rowsize, planeheight, pitch, bits; const char *error; if (avs->curr_frame >= avs->vi->num_frames) return AVERROR_EOF; // This must happen even if the stream is discarded to prevent desync. n = avs->curr_frame++; if (discard) return 0; pkt->pts = n; pkt->dts = n; pkt->duration = 1; // Define the bpp values for the new AviSynth 2.6 colorspaces if (avs_is_yv24(avs->vi)) { bits = 24; } else if (avs_is_yv16(avs->vi)) { bits = 16; } else if (avs_is_yv411(avs->vi)) { bits = 12; } else if (avs_is_y8(avs->vi)) { bits = 8; } else { bits = avs_bits_per_pixel(avs->vi); } // Without cast to int64_t, calculation overflows at about 9k x 9k resolution. pkt->size = (((int64_t)avs->vi->width * (int64_t)avs->vi->height) * bits) / 8; if (!pkt->size) return AVERROR_UNKNOWN; pkt->data = av_malloc(pkt->size); if (!pkt->data) return AVERROR_UNKNOWN; frame = avs_library->avs_get_frame(avs->clip, n); error = avs_library->avs_clip_get_error(avs->clip); if (error) { av_log(s, AV_LOG_ERROR, "%s\n", error); avs->error = 1; av_freep(&pkt->data); return AVERROR_UNKNOWN; } dst_p = pkt->data; for (i = 0; i < avs->n_planes; i++) { plane = avs->planes[i]; src_p = avs_get_read_ptr_p(frame, plane); rowsize = avs_get_row_size_p(frame, plane); planeheight = avs_get_height_p(frame, plane); pitch = avs_get_pitch_p(frame, plane); // Flip RGB video. if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) { src_p = src_p + (planeheight - 1) * pitch; pitch = -pitch; } avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight); dst_p += rowsize * planeheight; } avs_library->avs_release_video_frame(frame); return 0; }