michael@0: /* michael@0: * Copyright 2013 The LibYuv Project Authors. All rights reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: // Convert an ARGB image to YUV. michael@0: // Usage: convert src_argb.raw dst_yuv.raw michael@0: michael@0: #ifndef _CRT_SECURE_NO_WARNINGS michael@0: #define _CRT_SECURE_NO_WARNINGS michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "libyuv/convert.h" michael@0: #include "libyuv/planar_functions.h" michael@0: #include "libyuv/scale_argb.h" michael@0: michael@0: // options michael@0: bool verbose = false; michael@0: bool attenuate = false; michael@0: bool unattenuate = false; michael@0: int image_width = 0, image_height = 0; // original width and height michael@0: int dst_width = 0, dst_height = 0; // new width and height michael@0: int fileindex_org = 0; // argv argument contains the original file name. michael@0: int fileindex_rec = 0; // argv argument contains the reconstructed file name. michael@0: int num_rec = 0; // Number of reconstructed images. michael@0: int num_skip_org = 0; // Number of frames to skip in original. michael@0: int num_frames = 0; // Number of frames to convert. michael@0: int filter = 1; // Bilinear filter for scaling. michael@0: michael@0: static __inline uint32 Abs(int32 v) { michael@0: return v >= 0 ? v : -v; michael@0: } michael@0: michael@0: // Parse PYUV format. ie name.1920x800_24Hz_P420.yuv michael@0: bool ExtractResolutionFromFilename(const char* name, michael@0: int* width_ptr, michael@0: int* height_ptr) { michael@0: // Isolate the .width_height. section of the filename by searching for a michael@0: // dot or underscore followed by a digit. michael@0: for (int i = 0; name[i]; ++i) { michael@0: if ((name[i] == '.' || name[i] == '_') && michael@0: name[i + 1] >= '0' && name[i + 1] <= '9') { michael@0: int n = sscanf(name + i + 1, "%dx%d", width_ptr, height_ptr); // NOLINT michael@0: if (2 == n) { michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void PrintHelp(const char * program) { michael@0: printf("%s [-options] src_argb.raw dst_yuv.raw\n", program); michael@0: printf(" -s .... specify source resolution. " michael@0: "Optional if name contains\n" michael@0: " resolution (ie. " michael@0: "name.1920x800_24Hz_P420.yuv)\n" michael@0: " Negative value mirrors.\n"); michael@0: printf(" -d .... specify destination resolution.\n"); michael@0: printf(" -f ............ 0 = point, 1 = bilinear (default).\n"); michael@0: printf(" -skip ....... Number of frame to skip of src_argb\n"); michael@0: printf(" -frames .......... Number of frames to convert\n"); michael@0: printf(" -attenuate ............. Attenuate the ARGB image\n"); michael@0: printf(" -unattenuate ........... Unattenuate the ARGB image\n"); michael@0: printf(" -v ..................... verbose\n"); michael@0: printf(" -h ..................... this help\n"); michael@0: exit(0); michael@0: } michael@0: michael@0: void ParseOptions(int argc, const char* argv[]) { michael@0: if (argc <= 1) PrintHelp(argv[0]); michael@0: for (int c = 1; c < argc; ++c) { michael@0: if (!strcmp(argv[c], "-v")) { michael@0: verbose = true; michael@0: } else if (!strcmp(argv[c], "-attenuate")) { michael@0: attenuate = true; michael@0: } else if (!strcmp(argv[c], "-unattenuate")) { michael@0: unattenuate = true; michael@0: } else if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { michael@0: PrintHelp(argv[0]); michael@0: } else if (!strcmp(argv[c], "-s") && c + 2 < argc) { michael@0: image_width = atoi(argv[++c]); // NOLINT michael@0: image_height = atoi(argv[++c]); // NOLINT michael@0: } else if (!strcmp(argv[c], "-d") && c + 2 < argc) { michael@0: dst_width = atoi(argv[++c]); // NOLINT michael@0: dst_height = atoi(argv[++c]); // NOLINT michael@0: } else if (!strcmp(argv[c], "-skip") && c + 1 < argc) { michael@0: num_skip_org = atoi(argv[++c]); // NOLINT michael@0: } else if (!strcmp(argv[c], "-frames") && c + 1 < argc) { michael@0: num_frames = atoi(argv[++c]); // NOLINT michael@0: } else if (!strcmp(argv[c], "-f") && c + 1 < argc) { michael@0: filter = atoi(argv[++c]); // NOLINT michael@0: } else if (argv[c][0] == '-') { michael@0: fprintf(stderr, "Unknown option. %s\n", argv[c]); michael@0: } else if (fileindex_org == 0) { michael@0: fileindex_org = c; michael@0: } else if (fileindex_rec == 0) { michael@0: fileindex_rec = c; michael@0: num_rec = 1; michael@0: } else { michael@0: ++num_rec; michael@0: } michael@0: } michael@0: if (fileindex_org == 0 || fileindex_rec == 0) { michael@0: fprintf(stderr, "Missing filenames\n"); michael@0: PrintHelp(argv[0]); michael@0: } michael@0: if (num_skip_org < 0) { michael@0: fprintf(stderr, "Skipped frames incorrect\n"); michael@0: PrintHelp(argv[0]); michael@0: } michael@0: if (num_frames < 0) { michael@0: fprintf(stderr, "Number of frames incorrect\n"); michael@0: PrintHelp(argv[0]); michael@0: } michael@0: michael@0: int org_width, org_height; michael@0: int rec_width, rec_height; michael@0: bool org_res_avail = ExtractResolutionFromFilename(argv[fileindex_org], michael@0: &org_width, michael@0: &org_height); michael@0: bool rec_res_avail = ExtractResolutionFromFilename(argv[fileindex_rec], michael@0: &rec_width, michael@0: &rec_height); michael@0: if (image_width == 0 || image_height == 0) { michael@0: if (org_res_avail) { michael@0: image_width = org_width; michael@0: image_height = org_height; michael@0: } else if (rec_res_avail) { michael@0: image_width = rec_width; michael@0: image_height = rec_height; michael@0: } else { michael@0: fprintf(stderr, "Missing dimensions.\n"); michael@0: PrintHelp(argv[0]); michael@0: } michael@0: } michael@0: if (dst_width == 0 || dst_height == 0) { michael@0: if (rec_res_avail) { michael@0: dst_width = rec_width; michael@0: dst_height = rec_height; michael@0: } else { michael@0: dst_width = Abs(image_width); michael@0: dst_height = Abs(image_height); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static const int kTileX = 32; michael@0: static const int kTileY = 32; michael@0: michael@0: static int TileARGBScale(const uint8* src_argb, int src_stride_argb, michael@0: int src_width, int src_height, michael@0: uint8* dst_argb, int dst_stride_argb, michael@0: int dst_width, int dst_height, michael@0: libyuv::FilterMode filtering) { michael@0: for (int y = 0; y < dst_height; y += kTileY) { michael@0: for (int x = 0; x < dst_width; x += kTileX) { michael@0: int clip_width = kTileX; michael@0: if (x + clip_width > dst_width) { michael@0: clip_width = dst_width - x; michael@0: } michael@0: int clip_height = kTileY; michael@0: if (y + clip_height > dst_height) { michael@0: clip_height = dst_height - y; michael@0: } michael@0: int r = libyuv::ARGBScaleClip(src_argb, src_stride_argb, michael@0: src_width, src_height, michael@0: dst_argb, dst_stride_argb, michael@0: dst_width, dst_height, michael@0: x, y, clip_width, clip_height, filtering); michael@0: if (r) { michael@0: return r; michael@0: } michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: int main(int argc, const char* argv[]) { michael@0: ParseOptions(argc, argv); michael@0: michael@0: // Open original file (first file argument) michael@0: FILE* const file_org = fopen(argv[fileindex_org], "rb"); michael@0: if (file_org == NULL) { michael@0: fprintf(stderr, "Cannot open %s\n", argv[fileindex_org]); michael@0: exit(1); michael@0: } michael@0: michael@0: // Open all files to convert to michael@0: FILE** file_rec = new FILE* [num_rec]; michael@0: memset(file_rec, 0, num_rec * sizeof(FILE*)); // NOLINT michael@0: for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) { michael@0: file_rec[cur_rec] = fopen(argv[fileindex_rec + cur_rec], "wb"); michael@0: if (file_rec[cur_rec] == NULL) { michael@0: fprintf(stderr, "Cannot open %s\n", argv[fileindex_rec + cur_rec]); michael@0: fclose(file_org); michael@0: for (int i = 0; i < cur_rec; ++i) { michael@0: fclose(file_rec[i]); michael@0: } michael@0: delete[] file_rec; michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: bool org_is_yuv = strstr(argv[fileindex_org], "_P420.") != NULL; michael@0: bool org_is_argb = strstr(argv[fileindex_org], "_ARGB.") != NULL; michael@0: if (!org_is_yuv && !org_is_argb) { michael@0: fprintf(stderr, "Original format unknown %s\n", argv[fileindex_org]); michael@0: exit(1); michael@0: } michael@0: int org_size = Abs(image_width) * Abs(image_height) * 4; // ARGB michael@0: // Input is YUV michael@0: if (org_is_yuv) { michael@0: const int y_size = Abs(image_width) * Abs(image_height); michael@0: const int uv_size = ((Abs(image_width) + 1) / 2) * michael@0: ((Abs(image_height) + 1) / 2); michael@0: org_size = y_size + 2 * uv_size; // YUV original. michael@0: } michael@0: michael@0: const int dst_size = dst_width * dst_height * 4; // ARGB scaled michael@0: const int y_size = dst_width * dst_height; michael@0: const int uv_size = ((dst_width + 1) / 2) * ((dst_height + 1) / 2); michael@0: const size_t total_size = y_size + 2 * uv_size; michael@0: #if defined(_MSC_VER) michael@0: _fseeki64(file_org, michael@0: static_cast<__int64>(num_skip_org) * michael@0: static_cast<__int64>(org_size), SEEK_SET); michael@0: #else michael@0: fseek(file_org, num_skip_org * total_size, SEEK_SET); michael@0: #endif michael@0: michael@0: uint8* const ch_org = new uint8[org_size]; michael@0: uint8* const ch_dst = new uint8[dst_size]; michael@0: uint8* const ch_rec = new uint8[total_size]; michael@0: if (ch_org == NULL || ch_rec == NULL) { michael@0: fprintf(stderr, "No memory available\n"); michael@0: fclose(file_org); michael@0: for (int i = 0; i < num_rec; ++i) { michael@0: fclose(file_rec[i]); michael@0: } michael@0: delete[] ch_org; michael@0: delete[] ch_dst; michael@0: delete[] ch_rec; michael@0: delete[] file_rec; michael@0: exit(1); michael@0: } michael@0: michael@0: if (verbose) { michael@0: printf("Size: %dx%d to %dx%d\n", image_width, image_height, michael@0: dst_width, dst_height); michael@0: } michael@0: michael@0: int number_of_frames; michael@0: for (number_of_frames = 0; ; ++number_of_frames) { michael@0: if (num_frames && number_of_frames >= num_frames) michael@0: break; michael@0: michael@0: // Load original YUV or ARGB frame. michael@0: size_t bytes_org = fread(ch_org, sizeof(uint8), michael@0: static_cast(org_size), file_org); michael@0: if (bytes_org < static_cast(org_size)) michael@0: break; michael@0: michael@0: // TODO(fbarchard): Attenuate doesnt need to know dimensions. michael@0: // ARGB attenuate frame michael@0: if (org_is_argb && attenuate) { michael@0: libyuv::ARGBAttenuate(ch_org, 0, ch_org, 0, org_size / 4, 1); michael@0: } michael@0: // ARGB unattenuate frame michael@0: if (org_is_argb && unattenuate) { michael@0: libyuv::ARGBUnattenuate(ch_org, 0, ch_org, 0, org_size / 4, 1); michael@0: } michael@0: michael@0: for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) { michael@0: // Scale YUV or ARGB frame. michael@0: if (org_is_yuv) { michael@0: int src_width = Abs(image_width); michael@0: int src_height = Abs(image_height); michael@0: int half_src_width = (src_width + 1) / 2; michael@0: int half_src_height = (src_height + 1) / 2; michael@0: int half_dst_width = (dst_width + 1) / 2; michael@0: int half_dst_height = (dst_height + 1) / 2; michael@0: I420Scale(ch_org, src_width, michael@0: ch_org + src_width * src_height, half_src_width, michael@0: ch_org + src_width * src_height + michael@0: half_src_width * half_src_height, half_src_width, michael@0: image_width, image_height, michael@0: ch_rec, dst_width, michael@0: ch_rec + dst_width * dst_height, half_dst_width, michael@0: ch_rec + dst_width * dst_height + michael@0: half_dst_width * half_dst_height, half_dst_width, michael@0: dst_width, dst_height, michael@0: static_cast(filter)); michael@0: } else { michael@0: TileARGBScale(ch_org, Abs(image_width) * 4, michael@0: image_width, image_height, michael@0: ch_dst, dst_width * 4, michael@0: dst_width, dst_height, michael@0: static_cast(filter)); michael@0: } michael@0: bool rec_is_yuv = strstr(argv[fileindex_rec + cur_rec], "_P420.") != NULL; michael@0: bool rec_is_argb = michael@0: strstr(argv[fileindex_rec + cur_rec], "_ARGB.") != NULL; michael@0: if (!rec_is_yuv && !rec_is_argb) { michael@0: fprintf(stderr, "Output format unknown %s\n", michael@0: argv[fileindex_rec + cur_rec]); michael@0: continue; // Advance to next file. michael@0: } michael@0: michael@0: // Convert ARGB to YUV. michael@0: if (!org_is_yuv && rec_is_yuv) { michael@0: int half_width = (dst_width + 1) / 2; michael@0: int half_height = (dst_height + 1) / 2; michael@0: libyuv::ARGBToI420(ch_dst, dst_width * 4, michael@0: ch_rec, dst_width, michael@0: ch_rec + dst_width * dst_height, half_width, michael@0: ch_rec + dst_width * dst_height + michael@0: half_width * half_height, half_width, michael@0: dst_width, dst_height); michael@0: } michael@0: michael@0: // Output YUV or ARGB frame. michael@0: if (rec_is_yuv) { michael@0: size_t bytes_rec = fwrite(ch_rec, sizeof(uint8), michael@0: static_cast(total_size), michael@0: file_rec[cur_rec]); michael@0: if (bytes_rec < static_cast(total_size)) michael@0: break; michael@0: } else { michael@0: size_t bytes_rec = fwrite(ch_dst, sizeof(uint8), michael@0: static_cast(dst_size), michael@0: file_rec[cur_rec]); michael@0: if (bytes_rec < static_cast(dst_size)) michael@0: break; michael@0: } michael@0: if (verbose) { michael@0: printf("%5d", number_of_frames); michael@0: } michael@0: if (verbose) { michael@0: printf("\t%s", argv[fileindex_rec + cur_rec]); michael@0: printf("\n"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: fclose(file_org); michael@0: for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) { michael@0: fclose(file_rec[cur_rec]); michael@0: } michael@0: delete[] ch_org; michael@0: delete[] ch_dst; michael@0: delete[] ch_rec; michael@0: delete[] file_rec; michael@0: return 0; michael@0: }