|
| 1 | +#include "gop.h" |
| 2 | + |
| 3 | +#define NALU_LENGTH_SIZE 4 |
| 4 | + |
| 5 | +/*******************/ |
| 6 | + |
| 7 | +#define GOP_LOG_ERROR( ... ) general_log( NULL, "gop", X265_LOG_ERROR, __VA_ARGS__ ) |
| 8 | + |
| 9 | +#define GOP_FAIL_IF_ERR_R( cond, ... )\ |
| 10 | +if( cond )\ |
| 11 | +{\ |
| 12 | + GOP_LOG_ERROR( __VA_ARGS__ );\ |
| 13 | + b_fail = true;\ |
| 14 | + clean_up();\ |
| 15 | + return;\ |
| 16 | +} |
| 17 | + |
| 18 | +#define GOP_FAIL_IF_ERR( cond, ... )\ |
| 19 | +if( cond )\ |
| 20 | +{\ |
| 21 | + GOP_LOG_ERROR( __VA_ARGS__ );\ |
| 22 | + b_fail = true;\ |
| 23 | + clean_up();\ |
| 24 | + return -1;\ |
| 25 | +} |
| 26 | + |
| 27 | +/*******************/ |
| 28 | + |
| 29 | +using namespace std; |
| 30 | + |
| 31 | +namespace x265 { |
| 32 | + |
| 33 | +void GOPOutput::clean_up() |
| 34 | +{ |
| 35 | + if(data_file) fclose(data_file); |
| 36 | + if(gop_file) fclose(gop_file); |
| 37 | +} |
| 38 | + |
| 39 | +int GOPOutput::openFile(const char *gop_filename) |
| 40 | +{ |
| 41 | + gop_file = fopen(gop_filename, "wb"); |
| 42 | + GOP_FAIL_IF_ERR(!gop_file, "cannot open output file `%s'.\n", gop_filename); |
| 43 | + |
| 44 | + const char* slash = strrchr(gop_filename, '/'); |
| 45 | + if(slash == NULL) slash = strrchr(gop_filename, '\\'); |
| 46 | + if(slash == NULL) { |
| 47 | + dir_prefix = ""; |
| 48 | + slash = gop_filename; |
| 49 | + } |
| 50 | + else { |
| 51 | + int dir_len = slash - gop_filename + 2; |
| 52 | + char* tmp_dir_prefix = new char[dir_len]; |
| 53 | + strncpy(tmp_dir_prefix, gop_filename, dir_len - 1); |
| 54 | + tmp_dir_prefix[dir_len - 1] = 0; |
| 55 | + dir_prefix = tmp_dir_prefix; |
| 56 | + slash++; |
| 57 | + } |
| 58 | + int len = strlen(slash) - 3; |
| 59 | + filename_prefix = new char[len]; |
| 60 | + strncpy(filename_prefix, slash, len - 1); |
| 61 | + filename_prefix[len - 1] = 0; |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +void GOPOutput::setParam(x265_param *p_param) |
| 67 | +{ |
| 68 | + p_param->bAnnexB = false; |
| 69 | + p_param->bRepeatHeaders = false; |
| 70 | + i_numframe = 0; |
| 71 | + |
| 72 | + int len = strlen(dir_prefix) + strlen(filename_prefix) + 20; |
| 73 | + char opt_filename[len]; |
| 74 | + sprintf(opt_filename, "%s%s.options", dir_prefix, filename_prefix); |
| 75 | + FILE* opt_file = fopen(opt_filename, "wb"); |
| 76 | + GOP_FAIL_IF_ERR_R(!opt_file, "cannot open options file `%s'.\n", opt_filename); |
| 77 | + |
| 78 | + fprintf(gop_file, "#options %s.options\n", filename_prefix); |
| 79 | + |
| 80 | + fprintf(opt_file, "b-frames %d\n", p_param->bframes); |
| 81 | + fprintf(opt_file, "b-pyramid %d\n", p_param->bBPyramid); |
| 82 | + fprintf(opt_file, "input-timebase-num %d\n", info.timebaseNum); |
| 83 | + fprintf(opt_file, "input-timebase-den %d\n", info.timebaseDenom); |
| 84 | + fprintf(opt_file, "output-fps-num %u\n", p_param->fpsNum); |
| 85 | + fprintf(opt_file, "output-fps-den %u\n", p_param->fpsDenom); |
| 86 | + fprintf(opt_file, "source-width %d\n", p_param->sourceWidth); |
| 87 | + fprintf(opt_file, "source-height %d\n", p_param->sourceHeight); |
| 88 | + fprintf(opt_file, "sar-width %d\n", p_param->vui.sarWidth); |
| 89 | + fprintf(opt_file, "sar-height %d\n", p_param->vui.sarHeight); |
| 90 | + fprintf(opt_file, "primaries-index %d\n", p_param->vui.colorPrimaries); |
| 91 | + fprintf(opt_file, "transfer-index %d\n", p_param->vui.transferCharacteristics); |
| 92 | + fprintf(opt_file, "matrix-index %d\n", p_param->vui.matrixCoeffs >= 0 ? p_param->vui.matrixCoeffs : ISOM_MATRIX_INDEX_UNSPECIFIED); |
| 93 | + fprintf(opt_file, "full-range %d\n", p_param->vui.bEnableVideoFullRangeFlag >= 0 ? p_param->vui.bEnableVideoFullRangeFlag : 0); |
| 94 | + |
| 95 | + fclose(opt_file); |
| 96 | +} |
| 97 | + |
| 98 | +int GOPOutput::writeHeaders(const x265_nal* p_nal, uint32_t nalcount) |
| 99 | +{ |
| 100 | + GOP_FAIL_IF_ERR(nalcount < 3, "header should contain 3+ nals"); |
| 101 | + |
| 102 | + int len = strlen(filename_prefix) + 20; |
| 103 | + char hdr_filename[len]; |
| 104 | + sprintf(hdr_filename, "%s%s.headers", dir_prefix, filename_prefix); |
| 105 | + FILE* hdr_file = fopen(hdr_filename, "wb"); |
| 106 | + GOP_FAIL_IF_ERR(!hdr_file, "cannot open headers file `%s'.\n", hdr_filename); |
| 107 | + |
| 108 | + fprintf(gop_file, "#headers %s.headers\n", filename_prefix); |
| 109 | + |
| 110 | + for(unsigned int i = 0; i < nalcount; i++) |
| 111 | + fwrite(p_nal[i].payload, sizeof(uint8_t), p_nal[i].sizeBytes, hdr_file); |
| 112 | + |
| 113 | + fclose(hdr_file); |
| 114 | + return p_nal[0].sizeBytes + p_nal[1].sizeBytes + p_nal[2].sizeBytes; |
| 115 | +} |
| 116 | + |
| 117 | +int GOPOutput::writeFrame(const x265_nal* p_nalu, uint32_t nalcount, x265_picture& pic) |
| 118 | +{ |
| 119 | + const bool b_keyframe = pic.sliceType == X265_TYPE_IDR; |
| 120 | + int i_size = 0; |
| 121 | + |
| 122 | + if (b_keyframe) { |
| 123 | + if (data_file) |
| 124 | + fclose(data_file); |
| 125 | + int len = strlen(filename_prefix) + 20; |
| 126 | + char data_filename[len]; |
| 127 | + sprintf(data_filename, "%s%s.data-%d", dir_prefix, filename_prefix, i_numframe); |
| 128 | + data_file = fopen(data_filename, "wb"); |
| 129 | + GOP_FAIL_IF_ERR(!data_file, "cannot open data file `%s'.\n", data_filename); |
| 130 | + fprintf(gop_file, "%s.data-%d\n", filename_prefix, i_numframe); |
| 131 | + fflush(gop_file); |
| 132 | + } |
| 133 | + int8_t ts_len = 2 * sizeof(int64_t); |
| 134 | + int8_t ts_lenx[4] = {0, 0, 0, ts_len}; |
| 135 | + fwrite(&ts_lenx, sizeof(int8_t), 4, data_file); |
| 136 | + fwrite(&pic.pts, sizeof(int64_t), 1, data_file); |
| 137 | + fwrite(&pic.dts, sizeof(int64_t), 1, data_file); |
| 138 | + |
| 139 | + for(uint8_t i = 0; i < nalcount; i++) |
| 140 | + i_size += p_nalu[i].sizeBytes; |
| 141 | + |
| 142 | + for(uint8_t i = 0; i < nalcount; i++) |
| 143 | + fwrite(p_nalu[i].payload, sizeof(uint8_t), p_nalu[i].sizeBytes, data_file); |
| 144 | + |
| 145 | + i_numframe++; |
| 146 | + |
| 147 | + return i_size; |
| 148 | +} |
| 149 | + |
| 150 | +void GOPOutput::closeFile(int64_t, int64_t) |
| 151 | +{ |
| 152 | + clean_up(); |
| 153 | +} |
| 154 | + |
| 155 | +} |
0 commit comments