| 1 |
/* |
| 2 |
libheif example application "heif". |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2023 Dirk Farin <[email protected]> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in all |
| 16 |
copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "decoder_y4m.h" |
| 28 |
#include <iostream> |
| 29 |
#include <fstream> |
| 30 |
#include <memory> |
| 31 |
#include <string> |
| 32 |
|
| 33 |
static struct heif_error heif_error_ok = {heif_error_Ok, heif_suberror_Unspecified, "Success"}; |
| 34 |
|
| 35 |
heif_error loadY4M(const char *filename, InputImage *input_image) |
| 36 |
{ |
| 37 |
|
| 38 |
struct heif_image* image = nullptr; |
| 39 |
|
| 40 |
|
| 41 |
// open input file |
| 42 |
|
| 43 |
std::ifstream istr(filename, std::ios_base::binary); |
| 44 |
if (istr.fail()) { |
| 45 |
struct heif_error err = { |
| 46 |
.code = heif_error_Invalid_input, |
| 47 |
.subcode = heif_suberror_Unspecified, |
| 48 |
.message = "Cannot open Y4M file"}; |
| 49 |
return err; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
std::string header; |
| 54 |
getline(istr, header); |
| 55 |
|
| 56 |
if (header.find("YUV4MPEG2 ") != 0) { |
| 57 |
struct heif_error err = { |
| 58 |
.code = heif_error_Unsupported_feature, |
| 59 |
.subcode = heif_suberror_Unspecified, |
| 60 |
.message = "Input is not a Y4M file."}; |
| 61 |
return err; |
| 62 |
} |
| 63 |
|
| 64 |
int w = -1; |
| 65 |
int h = -1; |
| 66 |
|
| 67 |
size_t pos = 0; |
| 68 |
for (;;) { |
| 69 |
pos = header.find(' ', pos + 1) + 1; |
| 70 |
if (pos == std::string::npos) { |
| 71 |
break; |
| 72 |
} |
| 73 |
|
| 74 |
size_t end = header.find_first_of(" \n", pos + 1); |
| 75 |
if (end == std::string::npos) { |
| 76 |
break; |
| 77 |
} |
| 78 |
|
| 79 |
if (end - pos <= 1) { |
| 80 |
struct heif_error err = { |
| 81 |
.code = heif_error_Unsupported_feature, |
| 82 |
.subcode = heif_suberror_Unspecified, |
| 83 |
.message = "Header format error in Y4M file."}; |
| 84 |
return err; |
| 85 |
} |
| 86 |
|
| 87 |
char tag = header[pos]; |
| 88 |
std::string value = header.substr(pos + 1, end - pos - 1); |
| 89 |
if (tag == 'W') { |
| 90 |
w = atoi(value.c_str()); |
| 91 |
} |
| 92 |
else if (tag == 'H') { |
| 93 |
h = atoi(value.c_str()); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
std::string frameheader; |
| 98 |
getline(istr, frameheader); |
| 99 |
|
| 100 |
if (frameheader != "FRAME") { |
| 101 |
struct heif_error err = { |
| 102 |
.code = heif_error_Unsupported_feature, |
| 103 |
.subcode = heif_suberror_Unspecified, |
| 104 |
.message = "Y4M misses the frame header."}; |
| 105 |
return err; |
| 106 |
} |
| 107 |
|
| 108 |
if (w < 0 || h < 0) { |
| 109 |
struct heif_error err = { |
| 110 |
.code = heif_error_Unsupported_feature, |
| 111 |
.subcode = heif_suberror_Unspecified, |
| 112 |
.message = "Y4M has invalid frame size."}; |
| 113 |
return err; |
| 114 |
} |
| 115 |
|
| 116 |
struct heif_error err = heif_image_create(w, h, |
| 117 |
heif_colorspace_YCbCr, |
| 118 |
heif_chroma_420, |
| 119 |
&image); |
| 120 |
if (err.code != heif_error_Ok) { |
| 121 |
return err; |
| 122 |
} |
| 123 |
|
| 124 |
heif_image_add_plane(image, heif_channel_Y, w, h, 8); |
| 125 |
heif_image_add_plane(image, heif_channel_Cb, (w + 1) / 2, (h + 1) / 2, 8); |
| 126 |
heif_image_add_plane(image, heif_channel_Cr, (w + 1) / 2, (h + 1) / 2, 8); |
| 127 |
|
| 128 |
size_t y_stride, cb_stride, cr_stride; |
| 129 |
uint8_t* py = heif_image_get_plane2(image, heif_channel_Y, &y_stride); |
| 130 |
uint8_t* pcb = heif_image_get_plane2(image, heif_channel_Cb, &cb_stride); |
| 131 |
uint8_t* pcr = heif_image_get_plane2(image, heif_channel_Cr, &cr_stride); |
| 132 |
|
| 133 |
for (int y = 0; y < h; y++) { |
| 134 |
istr.read((char*) (py + y * y_stride), w); |
| 135 |
} |
| 136 |
|
| 137 |
for (int y = 0; y < (h + 1) / 2; y++) { |
| 138 |
istr.read((char*) (pcb + y * cb_stride), (w + 1) / 2); |
| 139 |
} |
| 140 |
|
| 141 |
for (int y = 0; y < (h + 1) / 2; y++) { |
| 142 |
istr.read((char*) (pcr + y * cr_stride), (w + 1) / 2); |
| 143 |
} |
| 144 |
|
| 145 |
input_image->image = std::shared_ptr<heif_image>(image, |
| 146 |
[](heif_image* img) { heif_image_release(img); }); |
| 147 |
|
| 148 |
return heif_error_ok; |
| 149 |
} |