static void test_mpeg(void *bytes, uint32_t len, struct h262_picparm *picparm, struct h262_seqparm *seqparm) { #if 1 struct bitstream *str = vs_new_decode(VS_H262, bytes, len); int nslice = 0; while (str->bytepos < str->bytesnum) { unsigned start_code; if (vs_start(str, &start_code)) goto err; if (start_code >= H262_START_CODE_SLICE_BASE && start_code <= H262_START_CODE_SLICE_LAST) { struct h262_slice *slice; nslice++; slice = calloc (sizeof *slice, 1); slice->mbs = calloc (sizeof *slice->mbs, picparm->pic_size_in_mbs); slice->slice_vertical_position = start_code - H262_START_CODE_SLICE_BASE; if (seqparm->vertical_size > 2800) { uint32_t svp_ext; if (vs_u(str, &svp_ext, 3)) { h262_del_slice(slice); goto err; } if (slice->slice_vertical_position >= 0x80) { fprintf(stderr, "Invalid slice start code for large picture\n"); goto err; } slice->slice_vertical_position += svp_ext * 0x80; } if (slice->slice_vertical_position >= picparm->pic_height_in_mbs) { fprintf(stderr, "slice_vertical_position too large\n"); goto err; } if (h262_slice(str, seqparm, picparm, slice)) { // h262_print_slice(seqparm, picparm, slice); h262_del_slice(slice); goto err; } // h262_print_slice(seqparm, picparm, slice); if (vs_end(str)) { h262_del_slice(slice); goto err; } h262_del_slice(slice); } else { fprintf(stderr, "Unknown start code %08x\n", start_code); goto err; } } return; err: assert(0); exit(1); #endif }
void mpp(int i,...) { va_list args; char fmt[41]; sprintf(fmt, "%d"); va_start(args, fmt); vsprintf(ss, fmt, args); vs_end(args); }
int main() { struct bitstream *str = vs_new_encode(VS_H264); struct h264_slice *slp = calloc (sizeof *slp, 1); slp->slice_type = 1; slp->num_ref_idx_l0_active_minus1 = 7; slp->num_ref_idx_l1_active_minus1 = 0x13; struct h264_pred_weight_table *weights = calloc (sizeof *weights, 1); weights->luma_log2_weight_denom = 3; weights->chroma_log2_weight_denom = 5; int i; for (i = 0; i <= slp->num_ref_idx_l0_active_minus1; i++) { weights->l0[i].luma_weight_flag = 1; weights->l0[i].chroma_weight_flag = 1; } for (i = 0; i <= slp->num_ref_idx_l1_active_minus1; i++) { weights->l1[i].luma_weight_flag = 1; weights->l1[i].chroma_weight_flag = 1; } weights->l0[3].luma_weight_flag = 0; weights->l0[3].luma_weight = 8; weights->l0[4].luma_weight = 0x3; weights->l0[5].luma_weight = -0x3; weights->l0[4].luma_offset = -5; weights->l0[5].luma_weight = 5; weights->l1[0x10].chroma_weight_flag = 0; weights->l1[0x10].chroma_weight[0] = 32; weights->l1[0x10].chroma_weight[1] = 32; weights->l1[0x11].chroma_weight[0] = 7; weights->l1[0x12].chroma_weight[1] = 8; weights->l1[0x13].chroma_offset[0] = 9; weights->l1[0x14].chroma_offset[1] = 10; if (h264_pred_weight_table(str, slp, weights)) { fprintf(stderr, "Failed\n"); return 1; } if (vs_end(str)) { fprintf(stderr, "Failed\n"); return 1; } fwrite(str->bytes, str->bytesnum, 1, stdout); return 0; }
static void generate_mpeg(const VdpPictureInfoMPEG1Or2 *info, int mpeg2, const void **byte, uint32_t *len) { struct bitstream *str = vs_new_encode(VS_H262); struct h262_seqparm seqparm; struct h262_picparm pp; uint32_t x, y, val = H262_START_CODE_SLICE_BASE; struct h262_macroblock *mbs; struct h262_slice slice; seqparm.is_ext = mpeg2; seqparm.chroma_format = 1; // 420 seqparm.vertical_size = input_height; pp.is_ext = mpeg2; fill_mpeg_picparm(info, &pp); mbs = calloc(sizeof(*mbs), pp.pic_size_in_mbs); slice.slice_vertical_position = val; slice.quantiser_scale_code = 1; slice.intra_slice_flag = 0; slice.first_mb_in_slice = 0; slice.last_mb_in_slice = pp.pic_width_in_mbs; slice.mbs = mbs; for (x = 0; x < pp.pic_size_in_mbs; ++x) fill_mpeg_mb(&pp, &slice, &mbs[x]); for (y = 0; y < pp.pic_size_in_mbs; y += pp.pic_width_in_mbs, ++val) { vs_start(str, &val); if (h262_slice(str, &seqparm, &pp, &slice) || vs_end(str)) { fprintf(stderr, "Failed to encode slice!\n"); return; } } *byte = str->bytes; *len = str->bytesnum; test_mpeg(str->bytes, str->bytesnum, &pp, &seqparm); free(mbs); free(str); }
int main() { uint8_t *bytes = 0; int bytesnum = 0; int bytesmax = 0; int c; int res; while ((c = getchar()) != EOF) { ADDARRAY(bytes, c); } struct bitstream *str = vs_new_decode(VS_H262, bytes, bytesnum); struct h262_seqparm *seqparm = calloc(sizeof *seqparm, 1); struct h262_picparm *picparm = calloc(sizeof *picparm, 1); struct h262_gop *gop = calloc(sizeof *gop, 1); struct h262_slice *slice; while (1) { uint32_t start_code; uint32_t ext_start_code; if (vs_start(str, &start_code)) goto err; printf("Start code: %02x\n", start_code); switch (start_code) { case H262_START_CODE_SEQPARM: if (h262_seqparm(str, seqparm)) goto err; if (vs_end(str)) goto err; h262_print_seqparm(seqparm); break; case H262_START_CODE_PICPARM: if (h262_picparm(str, seqparm, picparm)) goto err; if (vs_end(str)) goto err; h262_print_picparm(picparm); break; case H262_START_CODE_GOP: if (h262_gop(str, gop)) goto err; if (vs_end(str)) goto err; h262_print_gop(gop); break; case H262_START_CODE_EXTENSION: if (vs_u(str, &ext_start_code, 4)) goto err; printf("Extension start code: %d\n", ext_start_code); switch (ext_start_code) { case H262_EXT_SEQUENCE: if (h262_seqparm_ext(str, seqparm)) goto err; if (vs_end(str)) goto err; h262_print_seqparm(seqparm); break; case H262_EXT_PIC_CODING: if (h262_picparm_ext(str, seqparm, picparm)) goto err; if (vs_end(str)) goto err; h262_print_picparm(picparm); break; default: fprintf(stderr, "Unknown extension start code\n"); goto err; } break; case H262_START_CODE_END: printf ("End of sequence.\n"); break; default: if (start_code >= H262_START_CODE_SLICE_BASE && start_code <= H262_START_CODE_SLICE_LAST) { slice = calloc (sizeof *slice, 1); slice->mbs = calloc (sizeof *slice->mbs, picparm->pic_size_in_mbs); slice->slice_vertical_position = start_code - H262_START_CODE_SLICE_BASE; if (seqparm->vertical_size > 2800) { uint32_t svp_ext; if (vs_u(str, &svp_ext, 3)) { h262_del_slice(slice); goto err; } if (slice->slice_vertical_position >= 0x80) { fprintf(stderr, "Invalid slice start code for large picture\n"); goto err; } slice->slice_vertical_position += svp_ext * 0x80; } if (slice->slice_vertical_position >= picparm->pic_height_in_mbs) { fprintf(stderr, "slice_vertical_position too large\n"); goto err; } if (h262_slice(str, seqparm, picparm, slice)) { h262_print_slice(seqparm, picparm, slice); h262_del_slice(slice); goto err; } h262_print_slice(seqparm, picparm, slice); if (vs_end(str)) { h262_del_slice(slice); goto err; } h262_del_slice(slice); break; } else { fprintf(stderr, "Unknown start code\n"); goto err; } } printf("NAL decoded successfully\n\n"); continue; err: res = vs_search_start(str); if (res == -1) return 1; if (!res) break; printf("\n"); } return 0; }