static void encoder_state_write_bitstream_aud(encoder_state * const encoder_state) { bitstream * const stream = &encoder_state->stream; encoder_state_write_bitstream_access_unit_delimiter(encoder_state); nal_write(stream, AUD_NUT, 0, 1); bitstream_align(stream); }
int ff_ccitt_unpack(AVCodecContext *avctx, const uint8_t *src, int srcsize, uint8_t *dst, int height, int stride, enum TiffCompr compr, int opts) { int j; BitstreamContext bc; int *runs, *ref = NULL, *runend; int ret; int runsize = avctx->width + 2; runs = av_malloc(runsize * sizeof(runs[0])); ref = av_malloc(runsize * sizeof(ref[0])); if (!runs || !ref) { ret = AVERROR(ENOMEM); goto fail; } ref[0] = avctx->width; ref[1] = 0; ref[2] = 0; bitstream_init(&bc, src, srcsize * 8); for (j = 0; j < height; j++) { runend = runs + runsize; if (compr == TIFF_G4) { ret = decode_group3_2d_line(avctx, &bc, avctx->width, runs, runend, ref); if (ret < 0) goto fail; } else { int g3d1 = (compr == TIFF_G3) && !(opts & 1); if (compr != TIFF_CCITT_RLE && find_group3_syncmarker(&bc, srcsize * 8) < 0) break; if (compr == TIFF_CCITT_RLE || g3d1 || bitstream_read_bit(&bc)) ret = decode_group3_1d_line(avctx, &bc, avctx->width, runs, runend); else ret = decode_group3_2d_line(avctx, &bc, avctx->width, runs, runend, ref); if (compr == TIFF_CCITT_RLE) bitstream_align(&bc); } if (avctx->err_recognition & AV_EF_EXPLODE && ret < 0) goto fail; if (ret < 0) { put_line(dst, stride, avctx->width, ref); } else { put_line(dst, stride, avctx->width, runs); FFSWAP(int *, runs, ref); } dst += stride; } ret = 0; fail: av_free(runs); av_free(ref); return ret; }
int swf_action_build(bitstream_t *bs, swf_action_t *act) { bitstream_align(bs); bitstream_putbyte(bs, act->action_id); if (act->action_id & 0x80) { if (act->action_data == NULL) { return 1; // error } bitstream_putbytesLE(bs, act->action_length, 2); bitstream_putstring(bs, act->action_data, act->action_length); } return 0; }
int swf_action_parse(bitstream_t *bs, swf_action_t *act) { unsigned long offset; bitstream_align(bs); act->action_id = bitstream_getbyte(bs); if (act->action_id & 0x80) { act->action_length = bitstream_getbytesLE(bs, 2); offset = bitstream_getbytepos(bs); act->action_data = malloc(act->action_length); if (act->action_data == NULL) { fprintf(stderr, "Can't alloc memory for act->action_data\n"); return 1; } bitstream_getstring(bs, act->action_data, act->action_length); } return 0; }
int swf_tag_shape_input_detail(swf_tag_t *tag, struct swf_object_ *swf) { swf_tag_shape_detail_t *swf_tag_shape = tag->detail; unsigned char *data = tag->data; unsigned long length = tag->length; bitstream_t *bs; int ret; (void) swf; if (swf_tag_shape == NULL) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape == NULL\n"); return 1; } // parse context swf_tag_shape->_current_x = 0; swf_tag_shape->_current_y = 0; swf_tag_shape->_parse_condition = 0; bs = bitstream_open(); bitstream_input(bs, data, length); swf_tag_shape->shape_id = bitstream_getbytesLE(bs, 2); ret = swf_rect_parse(bs, &(swf_tag_shape->rect)); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->rect parse failed. shape_id=%d\n", swf_tag_shape->shape_id); bitstream_close(bs); return ret; } // DefineMorphShape, DefineMorphShape2 swf_tag_shape->is_morph = (tag->code == 46) || (tag->code == 84); // DefineShape4, DefineMorphShape2 swf_tag_shape->has_strokes = (tag->code == 83) || (tag->code == 84); if (swf_tag_shape->is_morph) { ret = swf_rect_parse(bs, &(swf_tag_shape->rect_morph)); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->rect_morph parse failed. shape_id=%d\n", swf_tag_shape->shape_id); bitstream_close(bs); return ret; } } if (swf_tag_shape->has_strokes) { ret = swf_rect_parse(bs, &(swf_tag_shape->stroke_rect)); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->stroke_rect parse failed. shape_id=%d\n", swf_tag_shape->shape_id); bitstream_close(bs); return ret; } if (swf_tag_shape->is_morph) { ret = swf_rect_parse(bs, &(swf_tag_shape->stroke_rect_morph)); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->stroke_rect_morph parse failed. shape_id=%d\n", swf_tag_shape->shape_id); bitstream_close(bs); return ret; } } swf_tag_shape->define_shape_reserved = bitstream_getbits(bs, 6); swf_tag_shape->define_shape_non_scaling_strokes = bitstream_getbits(bs, 1); swf_tag_shape->define_shape_scaling_strokes = bitstream_getbits(bs, 1); } if (swf_tag_shape->is_morph) { bitstream_align(bs); swf_tag_shape->offset_morph = bitstream_getbytesLE(bs, 4); ret = swf_morph_shape_with_style_parse(bs, &swf_tag_shape->morph_shape_with_style, tag); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_shape_with_style_parse swf_tag_shape->morph_shape_with_style failed. shape_id=%d\n", swf_tag_shape->shape_id); bitstream_close(bs); return ret; } } else { ret = swf_shape_with_style_parse(bs, &swf_tag_shape->shape_with_style, tag); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_shape_with_style_parse swf_tag_shape->shape_with_style failed. shape_id=%d\n", swf_tag_shape->shape_id); bitstream_close(bs); return ret; } } bitstream_close(bs); return 0; }
unsigned char * swf_tag_shape_output_detail(swf_tag_t *tag, unsigned long *length, struct swf_object_ *swf) { swf_tag_shape_detail_t *swf_tag_shape = (swf_tag_shape_detail_t *) tag->detail; bitstream_t *bs; unsigned char *data; int ret; long offset_of_offset_morph = 0; long tmp_offset_byte = 0; long tmp_offset_bit = 0; (void) swf; *length = 0; // build context swf_tag_shape->_current_fill_style_num = 0; swf_tag_shape->_current_line_style_num = 0; swf_tag_shape->_current_x = 0; swf_tag_shape->_current_y = 0; // bs = bitstream_open(); bitstream_putbytesLE(bs, swf_tag_shape->shape_id, 2); swf_rect_build(bs, &(swf_tag_shape->rect)); // DefineMorphShape, DefineMorphShape2 swf_tag_shape->is_morph = (tag->code == 46) || (tag->code == 84); // DefineShape4, DefineMorphShape2 swf_tag_shape->has_strokes = (tag->code == 83) || (tag->code == 84); if (swf_tag_shape->is_morph) { ret = swf_rect_build(bs, &(swf_tag_shape->rect_morph)); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_output_detail: swf_tag_shape->rect_morph build failed\n"); bitstream_close(bs); return NULL; } } if (swf_tag_shape->has_strokes) { ret = swf_rect_build(bs, &(swf_tag_shape->stroke_rect)); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->stroke_rect build failed\n"); bitstream_close(bs); return NULL; } if (swf_tag_shape->is_morph) { ret = swf_rect_build(bs, &(swf_tag_shape->stroke_rect_morph)); if (ret) { fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->stroke_rect_morph build failed\n"); bitstream_close(bs); return NULL; } } bitstream_putbits(bs, 6, swf_tag_shape->define_shape_reserved ); bitstream_putbits(bs, 1, swf_tag_shape->define_shape_non_scaling_strokes ); bitstream_putbits(bs, 1, swf_tag_shape->define_shape_scaling_strokes); } if (swf_tag_shape->is_morph) { bitstream_align(bs); // memory offset for overwrite this value after. offset_of_offset_morph = bitstream_getbytepos(bs); bitstream_putbytesLE(bs, swf_tag_shape->offset_morph, 4); swf_morph_shape_with_style_build(bs, &swf_tag_shape->morph_shape_with_style, tag); // rewind to overwrite offset_morph field and overwrite it. // offset distance from offset_morph so -4 operation swf_tag_shape->offset_morph = swf_tag_shape->morph_shape_with_style.offset_of_end_edges - offset_of_offset_morph - 4; tmp_offset_byte = bitstream_getbytepos(bs); // save offset tmp_offset_bit = bitstream_getbitpos(bs); bitstream_setpos(bs, offset_of_offset_morph, 0); bitstream_putbytesLE(bs, swf_tag_shape->offset_morph , 4); bitstream_setpos(bs, tmp_offset_byte, tmp_offset_bit); // restore offset } else { ret = swf_shape_with_style_build(bs, &swf_tag_shape->shape_with_style, tag); if (ret) { fprintf(stderr, "swf_tag_shape_output_detail: swf_shape_with_style_build failed\n"); bitstream_close(bs); return NULL; } } data = bitstream_steal(bs, length); bitstream_close(bs); return data; }
int swf_tag_place_input_detail(swf_tag_t *tag, struct swf_object_ *swf) { swf_tag_place_detail_t *swf_tag_place = tag->detail; unsigned char *data = tag->data; unsigned long length = tag->length; bitstream_t *bs; int ret; (void) swf; if (swf_tag_place == NULL) { fprintf(stderr, "ERROR: swf_tag_place_input_detail: swf_tag_place == NULL\n"); return 1; } swf_tag_place->character_id = 0; // undefined bs = bitstream_open(); bitstream_input(bs, data, length); if (tag->code == 4) { // PlaceObject swf_tag_place->character_id = bitstream_getbytesLE(bs, 2); swf_tag_place->depth = bitstream_getbytesLE(bs, 2); ret = swf_matrix_parse(bs, &(swf_tag_place->matrix)); if (ret) { fprintf(stderr, "ERROR: swf_tag_place_input_detail: swf_tag_place->matrix parse failed. character_id=%d\n", swf_tag_place->character_id); bitstream_close(bs); return ret; } bitstream_align(bs); if (bitstream_getbytepos(bs) < length) { // optional ret = swf_cxform_parse(bs, &(swf_tag_place->color_transform)); if (ret) { fprintf(stderr, "ERROR: swf_tag_place_input_detail: swf_tag_place->color_transform parse failed. character_id=%d\n", swf_tag_place->character_id); bitstream_close(bs); return ret; } } else { swf_tag_place->color_transform.has_add_terms = 0; swf_tag_place->color_transform.has_mult_terms = 0; swf_tag_place->color_transform.nbits = 0; } } else if (tag->code == 26) { // PlaceObject2 swf_tag_place->flag_has_clip_action = bitstream_getbit(bs); swf_tag_place->flag_has_clip_depth = bitstream_getbit(bs); swf_tag_place->flag_has_name = bitstream_getbit(bs); swf_tag_place->flag_has_ratio = bitstream_getbit(bs); swf_tag_place->flag_has_color_transform = bitstream_getbit(bs); swf_tag_place->flag_has_matrix = bitstream_getbit(bs); swf_tag_place->flag_has_character = bitstream_getbit(bs); swf_tag_place->flag_has_move = bitstream_getbit(bs); swf_tag_place->depth = bitstream_getbytesLE(bs, 2); if (swf_tag_place->flag_has_character) { swf_tag_place->character_id = bitstream_getbytesLE(bs, 2); } else { swf_tag_place->character_id = 0; } if (swf_tag_place->flag_has_matrix) { ret = swf_matrix_parse(bs, &(swf_tag_place->matrix)); if (ret) { fprintf(stderr, "ERROR: swf_tag_place_input_detail: swf_tag_place->matrix parse failed. character_id=%d\n", swf_tag_place->character_id); bitstream_close(bs); return ret; } } if (swf_tag_place->flag_has_color_transform) { ret = swf_cxformwithalpha_parse(bs, &(swf_tag_place->color_transform_with_alpha)); if (ret) { fprintf(stderr, "ERROR: swf_tag_place_input_detail: swf_tag_place->color_transform parse failed. character_id=%d\n", swf_tag_place->character_id); bitstream_close(bs); return ret; } } if (swf_tag_place->flag_has_ratio) { swf_tag_place->ratio = bitstream_getbytesLE(bs, 2); } if (swf_tag_place->flag_has_name) { swf_tag_place->name = (char *) bitstream_outputstring(bs); } if (swf_tag_place->flag_has_clip_depth) { swf_tag_place->clip_depth = bitstream_getbytesLE(bs, 2); } // TODO: clip action data for SWF 5 } else { bitstream_close(bs); return 1; // unknown tag; } bitstream_close(bs); return 0; }