INTERNATIONAL ORGANISATION FOR STANDARDISATION ORGANISATION INTERNATIONALE DE NORMALISATION ISO/IEC JTC1/SC29/WG11 CODING OF MOVING PICTURES AND AUDIO

ISO/IEC JTC1/SC29/WG11 MPEG2007/N9458 October 2007, Shenzhen, China Source: Video Subgroup Status: Approved Title: Committee Draft of ISO/IEC 23002-4: Video Tool Library Editor: Yi-Shin Tung, Gwo-Giun Lee , Euee S. Jang, Sunyoung Lee, Kohtaro Asai, Yoshihisa Yamada, Marco Mattavelli,

Document type: Document subtype: Document stage: Document language: ISO/IEC JTC 1/SC 29 N

Date: 2008-01-18

ISO/IEC CD 23002-4

ISO/IEC JTC 1/SC 29/WG 11

Secretariat:

Information technology — MPEG video technologies — Part 4: Video tool library

Élément introductif — Élément central — Partie 4: Titre de la partie

Warning

This document is not an ISO International Standard. It is distributed for review and comment. It is subject to change without notice and may not be referred to as an International Standard.

Recipients of this draft are invited to submit, with their comments, notification of any relevant patent rights of which they are aware and to provide supporting documentation. ISO/IEC CD 23002-4 Copyright notice

This ISO document is a working draft or committee draft and is copyright-protected by ISO. While the reproduction of working drafts or committee drafts in any form for use by participants in the ISO standards development process is permitted without prior permission from ISO, neither this document nor any extract from it may be reproduced, stored or transmitted in any form for any other purpose without prior written permission from ISO.

Requests for permission to reproduce this document for the purpose of selling it should be addressed as shown below or to ISO's member body in the country of the requester: [Indicate the full address, telephone number, fax number, telex number, and electronic mail address, as appropriate, of the Copyright Manger of the ISO member body responsible for the secretariat of the TC or SC within the framework of which the working document has been prepared.]

Reproduction for sales purposes may be subject to royalty payments or a licensing agreement.

Violators may be prosecuted.

© ISO/IEC 2008 – All rights reserved 3 ISO/IEC CD 23002-4

Contents Page

Foreword v Introduction...... vi 1. Scope...... 7 2. Normative reference...... 9 3. Definitions and RVC Terminologies...... 9 4. Symbols and Abbreviations...... 10 5. Description of functional unit (Abstract FU Description)...... 10 5.1. Interfaces of FU...... 10 6. List of FU’s by profile categories...... 12 6.1. Identifiers of FU...... 12 6.2. Facilitation FU...... 14 6.3. Common FU’s for video decoding...... 14 6.4. FU’s for MPEG-2 Simple Profile...... 14 6.5. FU’s for MPEG-4 SP...... 27 6.6. FU’s for AVC BP...... 47 Annex A. Specification of the CAL actor language...... 74  A.1 Introduction...... 74  A.2 Introductory remarks...... 78  A.2.1 Lexical tokens...... 78  A.4 Data types...... 84  A.5 Variables...... 89  A.6 Expressions...... 94  A.7 Statements...... 105  A.8 Actions...... 111  A.9 Action-level control structures...... 119  A.10 Actor model...... 124 Annex B. CAL language syntax...... 130  B.1 Actor...... 130  B.2 Expressions...... 131  B.3 Statements...... 132  B.4 Actions...... 132  B.5 Action control...... 133 Annex C. Keywords...... 134 Annex D. Basic runtime infrastructure...... 137  D.1 Operator symbols...... 137  D.2 Basic data types and their operations...... 138 Annex E. Naming Convention of FU...... 145

4 © ISO/IEC 2008 – All rights reserved ISO/IEC CD 23002-4

Foreword

ISO (the International Organization for Standardization) and IEC (the International Electrotechnical Commission) form the specialized system for worldwide standardization. National bodies that are members of ISO or IEC participate in the development of International Standards through technical committees established by the respective organization to deal with particular fields of technical activity. ISO and IEC technical committees collaborate in fields of mutual interest. Other international organizations, governmental and non-governmental, in liaison with ISO and IEC, also take part in the work. In the field of information technology, ISO and IEC have established a joint technical committee, ISO/IEC JTC 1.

International Standards are drafted in accordance with the rules given in the ISO/IEC Directives, Part 2.

The main task of the joint technical committee is to prepare International Standards. Draft International Standards adopted by the joint technical committee are circulated to national bodies for voting. Publication as an International Standard requires approval by at least 75 % of the national bodies casting a vote.

Attention is drawn to the possibility that some of the elements of this document may be the subject of patent rights. ISO and IEC shall not be held responsible for identifying any or all such patent rights.

ISO/IEC 23002-4 was prepared by Joint Technical Committee ISO/IEC JTC 1, Information technology, Subcommittee SC 29, Coding of audio, picture, multimedia and hypermedia information.

ISO/IEC 23002 consists of the following parts, under the general title Information technology — MPEG video technologies:

Part 1: Accuracy requirements for implementation of integer-output 8x8 inverse discrete cosine transform

Part 2: Fixed-point 8x8 inverse discrete cosine transform and discrete cosine transform

Part 3: Representation of auxiliary video streams and supplemental information

Part 4: Video tool library

© ISO/IEC 2008 – All rights reserved 5 ISO/IEC CD 23002-4

Introduction

6 © ISO/IEC 2008 – All rights reserved Information technology — MPEG video technologies — Part 4: Video tool library

A. Scope MPEG has produced many important and innovative standards, such as MPEG-1, MPEG-2, MPEG- 4, MPEG-7, MPEG-21 and MPEG-A. MPEG thinks that it is important to continue to propose innovations in the media coding field that are capable of satisfying the changing landscape and needs of media coding applications. With this objective in mind, MPEG intends to standardize a reconfigurable media coding (RMC) framework. In this context, this standard also named MPEG-C part4 addresses reconfigurable video aspects and focuses on the specification of the video tool library (VTL) in reconfigurable video coding (RVC). The objective of MPEG RVC is to offer a framework by which in the initial phase the VTL contains relevant tools from existing MPEG coding standards. New coding tools can also be added to the VTL should MPEG consider this as beneficial. In using the coding tools from VTL, the RVC framework is enabled to flexibly configure video codecs. To develop the framework, an appropriate description is needed to describe configurations of the decoder composed or instantiated from a subset of video tools from the library. Such information known as decoder description (DD), contains information for bitstream syntax and decoder configurations as illustrated in Figure 1-1. In general, standards are different in bitstream syntax structures and are composed of various decoding tools and their interoperation (bitstream semantics mapping and subsequent decoding processes). Therefore, RVC must support various bitstream syntax descriptions as well as video decoding tools and their connections. As depicted in Figure1-1, a typical RVC decoder requires two types of information, namely the decoder description and the encoded media (e.g., video) data.

1. Bitstream syntax 2. Decoder configuration

Decoder Description Encoder Decoder

Encoded Video Data

Figure 1-1. Conceptual diagram of RVC

In Figure 1-2, a more detailed description of the RVC decoder shown in Figure 1-1 is depicted. An original decoder description is input to the decoder description decoder to produce an abstract decoder description. This abstract decoder description is then used in the model instantiation or decoder composition mechanism (DCM) to form an abstract decoder model (ADM). This abstract decoder model will be used to implement the decoder using proprietary non-normative tools and mechanisms. The Decoder Description, the Standard Video Tool Library, and Abstract Decoder Models are normative. However, the process of model instantiation, which includes the selection, and parameterization of FU’s is informative.

© ISO/IEC 2008 – All rights reserved 1 StandardNormative RVC Model

Decoder Standard MPEG Video Decoder Description Tool Library Description (DDL/BSDL) (CAL FUs)

Abstract Model Instantiation: Decoder Selection and Model Parametrization of FUs

Implementation of Proprietary Decoder MPEG Tool MPEG Video Tool Implementation Library

Encoded Decoding Decoded Video Data Solution Video Data

ProprietaryNon-normative Decoder Figure 1-2. Graphical representation of the instantiation process for the RVC normative ADM and for the non normative proprietary compliant decoder implementation.

The description of the MPEG video tool library shown in Figure 1-2 is provided in this specification. Based the decoder description (DD) specified by MPEG-B part4, this document defines the specification of functional units. The functional units defined here are intended to build decoder solutions that include:  MPEG-2 simple profile decoder,  MPEG-4 simple profile decoder,  MPEG-4 AVC baseline profile decoder,  MPEG-2 main profile decoder,  MPEG-4 advanced simple profile decoder,  MPEG-4 AVC main and high profile decoder and  MPEG-4 AVC SVC baseline profiler decoder. In the future, MPEG may also provide the amendments of this standard to support more existing MPEG video profiles.

The conformance testing tools for testing implementations of the textual specification contained in this standard will be specified in the RVC Conformance Standard.

The implementation of the functional units provided in this standard textual specification is not normative. RVC compliant implementations can be designed using any software or hardware language and components. The reference software for this textual specification is written in CAL Language. Annex A provides a formal syntax description of CAL.

B. Normative reference ISO/IEC 13818-2:2000, Information technology — Generic coding of moving pictures and associated audio information: Video

ISO/IEC 14496-2:2004, Information technology — Coding of audio-visual objects: Visual

2 © ISO/IEC 2008 – All rights reserved ISO/IEC 14496-10:2005, Information technology — Coding of audio-visual objects: Advanced video coding

ISO/IEC 23001-4:200x (WD), Codec configuration representation

C. Definitions and RVC Terminologies Reconfigurable A framework for MPEG to flexibly define video coding Video Coding solutions at tool level while maintaining interoperability between different implementations that use the same set of tools. Video Tool A collection of FU’s, which are in existing MPEG standards as Library well as new FU’s that are considered beneficial by MPEG. Each FU consists of textual description, SW or HW description, and conformance testing tools. Functional Unit A modular tool, which is used in existing MPEG standards or (FU) possibly new tool that is considered beneficial by MPEG. Each FU consists of a processing unit and context-control unit. Decoder An instantiation of video decoder reconfigured from the FU’s Configuration in the video tool library. It can be in either one of the following modalities:  Configuration of an existing MPEG standard at a specific profile and level  A new decoding solution built from tools of an existing MPEG standard corresponding to a possible new profile  A new decoding solution built from tools of an existing MPEG standard and some new MPEG tools;  A new decoding solution that is composed of new MPEG tools It consists of a list of FU, and the FU connections. Decoder The decoder description consists of the bitstream syntax and Description decoder configuration Dataflow Model Computational model based on the exchange of data entities between functional units Abstract Decoder Instantiation of the dataflow model of a decoder configuration Model Decoding solution Realization or implementation of the decoder description Token Defined in the literature as data entities exchanged among FU’s Connections Links from output ports to input ports of FU’s that enable token exchange between these corresponding FU’s

D. Symbols and Abbreviations I ADM Abstract Decoder Model DD Decoder Description DDD Decoder Description Decoder DDL Decoder Description Language BSDL Bitstream Syntax Description Language DCM Decoder Composition Mechanism

© ISO/IEC 2008 – All rights reserved 3 FU Functional Units RVC Reconfigurable Video Coding RMC Reconfigurable Media Coding

E. Description of functional unit (Abstract FU Description) This session describes a proper combination of FU’s as specified by a decoder description.

E.1. Interfaces of FU To make FU as compact as possible while retaining a FU to be an independent and complete task, the following FU’s are identified as building blocks of supported decoders.

Each FU description consists of the following elements, and is presented in a tabular form as shown in Table 5-1. Each field in the table is described as follows.  Name of Function Unit: Name to represent this function unit in this specification. Naming convention is described in Annex A.  Description: Some text to describe functionality of this functional unit  Operation range: The operation range of a functional unit denotes the processing extent of a functional unit  Applicable decoding solution: The list of existing standard/profile that this FU can apply to.  Function prototype: The programming interface for implementing an interoperable FU.  Input/Output data: Input and output data to and from an FU.  Input/Output Control Signals (CS): Input and output CSs to and from an FU.  Input/Output Context Information (CI): Input and output CI to and from an FU.  Internal Processing Algorithm: Given input/output data, CS and CI, the description of internal processing in an FU, which is described in the form of pseudo code. NOTE: all FU assumes data-driven concurrency policy as in a dataflow machine. By this way, the VTL spec can be well defined in any language (or pseudo code).

Table 5-1. Template of description of a functional unit. Name of Function Unit Name to represent this function unit in this specification Description Some text to describe functionality of this functional unit Operation Range One among entire video session, MB basis, 8x8 block- basis and 4x4 block-basis. Applicable decoding solution List of the existing standards that the FU can apply to, e.g. ISO/IEC 13818-2 Simple Profile, ISO/IEC 14496-2 Simple Profile Function Prototype Void fuXXX (list of input/output data, control signal and context information); Input/Output Data (Token) Input/Output Name Description of data Input Data #1 Output … Control Signals (Token) Input/Output Name Description of control signals Input CS #1

4 © ISO/IEC 2008 – All rights reserved … Output

Context Information (Token) Input/Output Name Description of context information Input CI #1 Output … Internal Processing Algorithm Step Process 1 2 …

F. List of FU’s by profile categories

F.1. Identifiers of FU FU of the specific functionality is identified by its unique identification number. Table 5.1 lists all FU’s described in this section and their corresponding IDs. This ID is used in the decoding description to download and to instantiate the FU. Table 6-2. List of FU’s specified in this section for MPEG-2 simple profile, MPEG-4 simple profile and MPEG-4 AVC baseline profile decoders. Identifier FU Name 0 Algo_SynP_Mp2vSP (Syntax Parsing FU) 1 Algo_VLD_Mp2vSP (Entropy Decoding FU) 2 Mgnt_MBGen_Mp2vSP (MBG FU) 3 Algo_DCR_DPCM (DCR FU) 4 Algo_IS_Zigzag_8x8 (Inverse scan (IS) FU) 5 Algo_IQ_QSAndQmatrixMp2v (Inverse quantization (IQ) FU) 6 Algo_IDCT2D_MPEGCPart1Compliant (Inverse transform (IT) FU) 7 Algo_PR_U8AddI16Saturation (Video Reconstruction (VR) Unit) 8 Algo_SynP_Mp4vSP (Syntax Parsing FU) 9 Algo_VLD_Mp4vSP (Entropy Decoding FU) 10 Mgnt_MBGen_Mp4vSP (MBG FU) 11 Algo_DCR_AdaptiveHorizontalOrVerticalPred (DCR FU) 12 Algo_IS_ZigzagOrAlternateHorizontalVertical_8x8 (Inverse scan (IS) FU) 13 Algo_IAP_AdaptiveHorizontalOrVerticalPred (Inverse AC prediction (IAP) FU) 14 Algo_IQ_QSAndQmatrixMp4vOrH.263Scaler (Inverse quantization (IQ) FU) 15 Algo_DCR_AdaptiveHorizontalOrVerticalPred_LUMA_8x8 (DC Reconstruction 8x8 FU) 16 Algo_DCR_AdaptiveHorizontalOrVerticalPred_CHROMA_8x8 (DC Reconstruction 8x8 FU) 17 Algo_DCRAddr_ThreeLeftTop_8x8 (DC Reconstruction Addressing MxN FU) 18 Algo_DCRInvPred_LUMA_8x8 (DC Reconstruction Inverse Prediction Luma

© ISO/IEC 2008 – All rights reserved 5 MxN FU) 19 Algo_DCRInvPred_CHROMA_8x8 (DC Reconstruction Inverse Prediction Luma MxN FU) 20 Algo_IAP_AdaptiveHorizontalOrVerticalPred_8x8 (Inverse AC Prediction MxN FU) 21 Mgnt_FBAddr_16x16 (Address Generator MxN FU) 22 Mgnt_FBAddr_8x8 (Address Generator MxN FU) 23 Mgnt_FB (Frame Buffer FU) 24 Algo_Interp_HalfpelBilinear (Interpolation FU) 25 Mgnt_DCSplit 26 Mgnt_MVSplit 27 Algo_MVReconstruction 28 Algo_Interp_TwoHypothesesAvg 29 Algo_NALP (Byte Stream NAL (NALP) Parsing FU) 30 Algo_SynP_AVCBP (CAVLC Syntax Parsing(SynP_CAVLC) FU) 31 Algo_CTXGEN_CAVLC (CAVLC CTX FU) 32 Algo_VLD_CAVLC (Entropy Decoding FU) 33 Mgnt_MBGen_AVCBP (MBG4x4 FU) 34 Algo_DCR_Hadamard_LUMA_4x4 (DC Reconstruction (DCR) FU) 35 Algo_IS_Zigzag_4x4 (4x4 Inverse scan (IS4x4) FU) 36 Algo_IQ_QSAndSLAndIDCTScaler_4x4 (4x4 Inverse quantization (IQ4x4) FU) 37 Algo_IntraPredAndPR_LUMA_4x4 (Intra Prediction and Picture Reconstructor (IPR4x4) FU) 38 Algo_DBF_AdaptiveFilterAVC (Deblocking Filter (DF) FU) 43 Algo_IS_Zigzag_8x8 (8x8 Inverse scan (IS8x8) FU) 44 Algo_IQ_QSAndSLAndIDCTScaler_8x8 (8x8 Inverse quantization (IQ8x8) FU) 45 Algo_IT2D_NoPreScaling_4x4 (4x4 Inverse transform (IT4x4) FU) 46 Algo_IT2D_NoPreScaling_8x8 (8x8 Inverse transform (IT8x8) FU) 47 Algo_IntraPredAndPR_LUMA_8x8 (Intra Prediction and Picture Reconstructor (IPR8x8) FU) 48 Algo_SynP_Generic (Syntax Parsing FU) 49 Algo_MismatchControl_8x8 50 Algo_IS_ZigzagAndAlternateVertical_8x8 51 Algo_DCR_Hadamard_CHROMA_2x2 (DC Reconstruction (DCR) FU) 52 Algo_IS_ZigzagAndField_4x4 53 Algo_IntraPred_LUMA_4x4 54 Algo_IntraPred_LUMA_16x16 55 Algo_IntraPred_CHROMA_8x8

6 © ISO/IEC 2008 – All rights reserved 58 Algo_Interp_SeparableSixTapQuarterPelAVC 59 Algo_Interp_EighthPelBilinear 60 Mgnt_FBAddrM_Luma_MxN 61 Mgnt_FBAddrM_Chroma_MxN 62 Algo_Interp_OneHypothesesWeighted 64 Algo_MVR_DPCM 65 Algo_MVR_MedianOfThreeLeftAndTopAndTopRight 66 Algo_MVR_Direct 67 Algo_MVR_MultiFrameAdaptive 69 Algo_SynP-VLD-MBG_Mp4vSP (Syntax Parsing FU) 72 Algo_IDCT2D_IEEE1180 73 Algo_IQ_QSAndIDCTScalerHadamard_4x4 74 Algo_IQ_QSAndIDCTScalerHadamard_2x2 75 Algo_Interp_HalfpelBilinearRoundingUp 76 Algo_IS_Hadamard_LUMA_4x4

F.2. Facilitation FU This category of functional units is not designed to fulfill some specific decoding process, but to provide the necessary connection facilitation and give large flexibility for connection. They may benefit the processes such as interface conversion, data granularity change.

F.2.1. Multiplexer

F.2.2. Demultiplexer

F.3. Common FU’s for video decoding

F.4. FU’s for MPEG-2 Simple Profile FU’s required for building MPEG-2 Simple Profile decoder are described in this section. The configuration description of the built decoder can be represented by MPEG-B Part 4

F.4.1. Syntax Parsing (SYP) FU Name of Function Unit Algo_ SynP_Mp2vSP (Syntax Parsing FU) Description Operation range Entire video session Applicable decoding solution 13818-2 Simple Profile

© ISO/IEC 2008 – All rights reserved 7 Function prototype Void FU’synFSM(GCU IN_OUT gcu, DATA Int IN_OUT state, DATA Int IN SymIdx, DATA Int OUT vld_table_ID, SIG Int OUT nMB, DATA int_queue IN_OUT mb_type_queue, DATA int_queue IN_OUT mb_dc_queue, DATA int_queue IN_OUT mb_ac_queue); Input/Output Data Input state The previous state represented by an integer SymIdx ID of the last decoded symbol by fuVLD. Refer to 5.1.3 Output state The output state represented by an integer vld_table_ID ID of a VLC table, which consists of all valid bit patterns for decoding the next symbol mb_type_queue, These 6 queues store different types of decoded symbols mb_quant_queue, for reconstructing a macroblock. Once a symbol is mb_dc_queue, generated, it is then pushed into the corresponding queue, mb_ac_queue respectively. Six types of symbols are:  mb_type: A macroblock type.  mb_quant: A QP value is a 5-bit integer that are computed from the quant_scale value in the video header, slice header or macroblock headers.  mb_dc: A quantized differential DC value (a 12-bit integer).  mb_ac: A pair of (RUN, LEVEL) of AC symbo Control signals Input Output nMB The MB index that is being processing Context information Input Output Internal processing algorithm Step process 1 Read the previous state and the last decoded symbol index SymIdx in the last call to fuVLD(). 2 After considering the previous state and SymIdx, internal variables are updated. If one of mb_type, mb_cbp, mb_quant, mb_dc or mb_ac is generated, push it into the corresponding queue, respectively 3 The parsing status is changed to a new state and a vld_table_ID representing the next symbol set is generated according to Specification definition 4 The new state and vld_table_ID are output 5 End of processing

F.4.2. Entropy Decoding FU

Name of Function Unit Algo_ VLD_Mp2vSP (Entropy Decoding FU) Description Operation range Entire video session Applicable decoding 13818-2 Simple Profile solution Function prototype Void fuVLD_Sequence (GCU IN_OUT gcu, DATA STREAM_POS IN_OUT bitstream_pos, SIG Int OUT width, SIG Int OUT height); Void fuVLD_Picture (GCU IN_OUT gcu,

8 © ISO/IEC 2008 – All rights reserved DATA STREAM_POS IN_OUT bitstream_pos); Void fuVLD_SLICE (GCU IN_OUT gcu, DATA STREAM_POS IN_OUT bitstream_pos, SIG Int OUT quant_scale, SIG Int OUT nMB); Void fuVLD_MB (GCU IN_OUT gcu, DATA STREAM_POS IN_OUT stream_pos, DATA Int IN vld_table_ID, DATA Int OUT SymIdx); Input/Output Data Input stream_pos a location indicator to describe the consuming status of MPEG-4 visual elementary bitstream (a sequence of one-bit binary values) vld_table_ID ID of the VLC table to be used for decoding the next symbol Output SymIdx coded symbol index obtained after one operation, which represents for one of the following symbols  mb_type: This coded is used to derived the macroblock type  quantiser_scale_code: A 5-bit code which specifies the quantizer.  dct_dc_differential: represents the value of an intra DC differential. (Refer to 6.3.7 of 14496- 2:2004)  DCT_coefficient: represents a non-zero AC coefficient using (LAST, RUN, LEVEL) pair. Control signals Input Output width 13-bits Integer value (fuVLD_ Sequence) height 13-bits Integer value (fuVLD_ Sequence) quant_scale represent the absolute value of quant to be used for dequantizing the macroblock. (fuVLD_Picture, fuVLD_SLICE) nMB the first coded MB in a video packet. (fuVLD_SLICE) if the video bitstream is coded without video packet, this number is equal to the total number of MB in a frame Context information Input Output Internal processing algorithm Step Process 1 Using leading bits from the input bitstream to obtain the symbol by look up the table identified by vld_table_ID 2 Flush used bits for the current symbol 3 Output SymIdx 4 End of processing

F.4.3. MB Generation (MBG) FU

Name of Function Unit Mgnt_MBGen_Mp2vSP (MBG FU) Description Operation range Video packet-basis Applicable decoding solution 13818-2 Simple Profile Function prototype Void fuMBG(GCU IN_OUT gcu, DATA int_queue IN_OUT mb_type_queue, DATA int_queue IN_OUT mb_dc_queue,

© ISO/IEC 2008 – All rights reserved 9 DATA int_queue IN_OUT mb_ac_queue, DATA Int* OUT QFS, SIG Int OUT quantiser_scale); Input/Output Data Input mb_type_queue Refer to 5.1.3 mb_dc_queue Refer to 5.1.3 mb_ac_queue Refer to 5.1.3 Output QFS[6][64] Six lists of 64 integer elements, one list per 8x8 block Control signals Input Output quantiser_scale a 5-bit integer value for describing the quantization step in the current MB, which is derived from the running QP. nMB The index of the MB, whose data is being prepared Context information Input Output Internal processing algorithm Step process 1 Set all elements of QFS to zero 2 Pop mb_type from mb_type_queue (for inter-mode use) 3 Pop quant_scale from mb_quant_queue 4 i=0 //i= {0 .. 5}, 6 blocks 5 Pop a quantized differential DC from mb_dc_queue and store it into QFS[i][0] 6 Set j to 1 7 Pop a series of the quantized (possibly differential) AC values from mb_ac_queue until a symbol with “EOB code” are found. Then, decode (RUN, LEVEL) codes and store results in QFS[i][j..63]. 8 i++. If i < 6, go to Step 7 9 Output data QFS[0..5][0…64] and control signals quant_scale 10 End of processing

F.4.4. DC Reconstruction (DCR) FU

Name of Function Unit Algo_DCR_DPCM (DCR FU) Description Operation range MB-basis Applicable decoding 13818-2 Simple Profile solution Function prototype Void fuDCR (GCU IN_OUT gcu, DATA Int* IN QFS, DATA Int* OUT QFSP, SIG Int IN nMB); Input/Output Data Input QFS[6][64] Refer to 5.1.4 Output QFS’[6][64] Six lists of 64 integer elements, one list per 8x8 block (only the first element of each list in QFS’ is changed if compared to QFS). Control signals Input quant_scale Refer to 5.1 nMB Refer to 5.1 to check the position of the neighboring block (NULL or not), which is important for getting the neighboring DC coefficients Output Context information

10 © ISO/IEC 2008 – All rights reserved Input preceding block DC coefficients Output DC[6] 6 DC coefficients, each is a 12-bits integer for one 8x8 block to be stored in GCU for decoding the next neighboring MB by DCR Internal processing algorithm Step Process 1 i=0 //i= {0 .. 5}, 6 blocks 2 Read the predicted quantised DC coefficient (PQF_DC = QFS[i][0]) for block i. 3 Read preceding DC coefficients of block i either from Context Information or the output of previous iterations (DC[0…i-1]) 4 Calculate QF_DC by adding PQF_DC with the predictor 5 Saturate QF_DC to the range [-2048, 2047] 6 Calculate F_DC’’ by applying inverse quantisation to QF_DC 7 Calculate QFS’[i][0] by saturating F_DC’’ to the range [-2048, 2047] 8 Copy QFS[i][1…63] to QFS’[i][1…63] 9 Output QFS’[i][0…63] for block i 10 i++. If i < 6, go to Step 3 11 End of processing

F.4.5. Inverse Scan (IS) FU

Name of Function Unit Algo_IS_ZigzagAndAlternateVertical_8x8 Description This module inverts the one-dimensional array of coefficients that ordered in zigzag or alternate scan to 2D raster order. Operation Range 8x8 block-basis Applicable decoding solution 13818-2 Simple Profile Function Prototype Void fuIS_ZigzagAndAlternate_8x8(GCU IN_OUT gcu, DATA Int* IN QFS, DATA Int* IN QF, DATA Int IN AltScan, DATA Int* OUT M); Input/Output Data Input A list of 64 integer coefficient elements, one list per QFS[64] 8x8 block. Output A list of 64 reordered integer coefficient elements, QF[64] one list per 8x8 block. Control Signals Input Indicates zigzag or alternate scan. AltScan flag is 0 AltScan when zigzag, 1 when alternate scan. It must set every block. Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read AltScan. 2 Read QFS[0..63]. 3 If AltScan == 1, go to Step 6 4 Rearrange QFS[0..63] in zigzag scan order, and store it into QF[0..63]. 5 Go to Step 7.

© ISO/IEC 2008 – All rights reserved 11 6 Rearrange QFS[0..63] in field scan order, and store it into QF[0..63]. 7 Output QF[0..63]. 8 End of processing

F.4.6. Inverse Quantization with quantizer scale and Qmatrix (MPEG-2)

Name of Function Unit Algo_IQ_QSAndQmatrixMp2v (Inverse quantization (IQ) FU) Description This module computes inverse quantization of 8x8 block with quantizer scale and Qmatrix. Operation Range 8x8 block-basis Applicable decoding solution 13818-2 Simple Profile Function Prototype Void fuIQ_QSAndQmatrixMp2v(GCU IN_OUT gcu, DATA Int* IN QF, DATA Int IN quantizer_scale, DATA Int* IN W, DATA Int* OUT F); Input/Output Data Input A list of 64 integer coefficient elements, one list per QF[64] 8x8 block. Output A list of 64 dequantized integer coefficient elements, F[64] one list per 8x8 block. Control Signals Input quantizer_scale is a number looked up a table indexed quantizer_scale q_scale_type and quantizer_scale_code. W[64] Qmatrix. It must set every block. Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read quantizer_scale. 2 Read W[0..63]. 3 Read QF[0..63]. 4 Dequantize QF[0..63], using quantizer_scale and W[0..63] and store dequantized QF[0..63] into F[0..63]. 5 Output F[0..63]. 6 End of processing

F.4.7. 8x8 DCT Mismatch Control FU Name of Function Unit Algo_MismatchControl_8x8 Description This module controls DCT mismatch. Operation Range 8x8 block-basis Applicable decoding solution 13818-2 Simple Profile 14496-2 Simple Profile Function Prototype Void fuMismatchControlMp2v (GCU IN_OUT gcu, DATA Int* IN IN, DATA Int* OUT OUT); Input/Output Data Input IN[64] A list of 64 integer coefficient elements, one list per 8x8 block.

12 © ISO/IEC 2008 – All rights reserved Output A list of 64 integer coefficient elements, one list per OUT[64] 8x8 block. The element [7][7] corrected according to the sum of 64 input coefficient. Control Signals Input Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read IN[0..63]. 2 Correct IN[63] according to IN[63] and sum of IN[0..63], and store it OUT[0..63]. 3 Output OUT[0..63]. 4 End of processing

F.4.8. Inverse Transform Name of Function Unit Algo_IDCT2D_IEEE1180 (Inverse transform (IT) FU) Description

Operation range MB-basis Applicable decoding 13818-2 Simple Profile solution 14496-2 Simple Profile Function prototype Void fuIT (GCU IN_OUT gcu, DATA Int* IN F, DATA Int* OUT f); Input/Output Data Input F[6][8][8] Refer to 5.1.7 Output f[6][8][8] Six 2D arrays of 8x8 9 bits signed integer elements Control signals Input Output Context information Input Output Internal processing algorithm Step Process 1 i=0 //i= {0 .. 5}, 6 blocks 2 Derive f[[i][0…7] [0…7] from F[[i][0…7] [0…7] by applying the following formula The inverse 8x8 DCT (IDCT) is defined as: 2 N 1 N 1 ( 2 x  1 ) u  ( 2 y  1 ) v  f ( x , y )    C ( u ) C ( v ) F ( u , v ) c o s c o s N u 0 v  0 2 N 2 N with u, v, x, y = 0, 1, 2, …, N-1 where x, y are spatial coordinates in the sample domain u, v are coordinates in the transform domain 1  f o r u , v  0 C ( u ) , C ( v )   2  1 o t h e r w i s e 3 i++. If i < 6, go to Step 2. 4 End of processing

© ISO/IEC 2008 – All rights reserved 13 F.4.9. Inverse Transform (IT) FU

Name of Function Unit Algo_IDCT2D_MPEGCPart1Compliant (Inverse transform (IT) FU) Operation range MB-basis Applicable decoding 13818-2 Main Profile solution 14496-2 Simple Profile Function prototype Void fuIT (GCU IN_OUT gcu, DATA Int* IN F, DATA Int* OUT f); Input/Output Data Input F[6][8][8] Refer to 5.1.7 Output f[6][8][8] Six 2D arrays of 8x8 9 bits signed integer elements Control signals Input Output Context information Input Output Internal Data Local G[6][8][8] Six 2D arrays of 8x8 26 bits signed integer elements Local x0…x7, xa..xd 26 bits signed integers Internal processing algorithm Step Process 1 i=0 //i= {0 .. 5}, 6 blocks 2 Derive G[[i][0…7] [0…7] from F[[i][0…7] [0…7] as follows G[i][v][u] = F[i][v][u] * S[v][u]; v = 0..7, u = 0..7; G[i][0][0] = F[i][0][0] + 2048; Where S[v][u] are elements of matrix: 轾1024 1138 1730 1609 1024 1609 1730 1138 犏1138 1264 1922 1788 1138 1788 1922 1264 犏 犏1730 1922 2923 2718 1730 2718 2923 1922 犏 1609 1788 2718 2528 1609 2528 2718 1788 S= 犏 犏1024 1138 1730 1609 1024 1609 1730 1138 犏 犏1609 1788 2718 2528 1609 2528 2718 1788 犏1730 1922 2923 2718 1730 2718 2923 1922 犏 臌犏1138 1264 1922 1788 1138 1788 1922 1264 3 j=0 //j= {0 .. 7}, 8 rows 4 Update G[[i][j] [0…7] by applying 8-point scaled IDCT as follows x1 = G[i][j][1]; x3 = G[i][j][3]; x5 = G[i][j][5]; x7 = G[i][j][7]; xa = x1 + x7; xb = x1 - x7; x1 = xa + x3; x3 = xa - x3; x7 = xb + x5; x5 = xb - x5;

14 © ISO/IEC 2008 – All rights reserved xc = (x3 >> 3) - (x3 >> 7); xd = xc - (x3 >> 11); xa = xc + (xd >> 1); x3 = x3 - xc; xc = (x5 >> 3) - (x5 >> 7); xd = xc - (x5 >> 11); xb = xc + (xd >> 1); x5 = x5 - xc; x3 = x3 + xb; x5 = x5 - xa; xc = x1 + (x1 >> 5); xd = xc >> 2; x1 = xd + (x1 >> 4); xa = xc - xd; xc = x7 + (x7 >> 5); xd = xc >> 2; x7 = xd + (x7 >> 4); xb = xc - xd; x1 = x1 + xb; x7 = x7 - xa; x0 = G[i][j][0]; x2 = G[i][j][2]; x4 = G[i][j][4]; x6 = G[i][j][6]; xc = (x2 >> 9) - x2; xa = x2 >> 1; x2 = (xc >> 2) - xc; xc = (x6 >> 9) - x6; xb = x6 >> 1; x6 = (xc >> 2) - xc; x2 = x2 - xb; x6 = x6 + xa; xa = x0 + x4; xb = x0 - x4; x0 = xa + x6; x6 = xa - x6; x4 = xb + x2; x2 = xb - x2; G[i][j][0] = x0 + x1; G[i][j][1] = x4 + x5; G[i][j][2] = x2 + x3; G[i][j][3] = x6 + x7; G[i][j][4] = x6 - x7; G[i][j][5] = x2 - x3; G[i][j][6] = x4 - x5; G[i][j][7] = x0 - x1; 5 j=0 //j= {0 .. 7}, 8 columns 6 Update G[[i][ 0…7] [j] by applying 8-point scaled IDCT as follows x1 = G[i][1][j]; x3 = G[i][3][j]; x5 = G[i][5][j]; x7 = G[i][7][j]; xa = x1 + x7;

© ISO/IEC 2008 – All rights reserved 15 xb = x1 - x7; x1 = xa + x3; x3 = xa - x3; x7 = xb + x5; x5 = xb - x5; xc = (x3 >> 3) - (x3 >> 7); xd = xc - (x3 >> 11); xa = xc + (xd >> 1); x3 = x3 - xc; xc = (x5 >> 3) - (x5 >> 7); xd = xc - (x5 >> 11); xb = xc + (xd >> 1); x5 = x5 - xc; x3 = x3 + xb; x5 = x5 - xa; xc = x1 + (x1 >> 5); xd = xc >> 2; x1 = xd + (x1 >> 4); xa = xc - xd; xc = x7 + (x7 >> 5); xd = xc >> 2; x7 = xd + (x7 >> 4); xb = xc - xd; x1 = x1 + xb; x7 = x7 - xa; x0 = G[i][0][j]; x2 = G[i][2][j]; x4 = G[i][4][j]; x6 = G[i][6][j]; xc = (x2 >> 9) - x2; xa = x2 >> 1; x2 = (xc >> 2) - xc; xc = (x6 >> 9) - x6; xb = x6 >> 1; x6 = (xc >> 2) - xc; x2 = x2 - xb; x6 = x6 + xa; xa = x0 + x4; xb = x0 - x4; x0 = xa + x6; x6 = xa - x6; x4 = xb + x2; x2 = xb - x2; G[i][0][j] = x0 + x1; G[i][1][j] = x4 + x5; G[i][2][j] = x2 + x3; G[i][3][j] = x6 + x7; G[i][4][j] = x6 - x7; G[i][5][j] = x2 - x3; G[i][6][j] = x4 - x5; G[i][7][j] = x0 - x1; 7 Derive f[[i][0…7] [0…7] from G[[i][0…7] [0…7] as follows f[i][v][u] = G[i][v][u] >> 13; v = 0..7, u = 0..7;

16 © ISO/IEC 2008 – All rights reserved 8 End processing

F.4.10. Algo_MVR_DPCM Name of Function Unit Algo_MVR_DPCM Description This module reconstruct motion vectors by DPCM. Operation Range 16x16 block-basis Applicable decoding solution 13818-2 Simple Profile Function Prototype Void fuMVR_DPCM(GCU IN_OUT gcu, DATA Int* IN PMV, DATA Int* OUT MV ); Input/Output Data Input PMV[2] A set of predictive motion vector value Output MV[2] A set of motion vector value Control Signals Input Output Context Information Input PMVnext[2] A set of motion vector value for next MB Output Internal Processing Algorithm Step Process 1 Read PMV[0..1] 2 Read PMVnext[0..1] 3 MV[0] = PMVnext[0] + PMV[0] 4 MV[1] = PMVnext[1] + PMV[1] 5 Output MV[0..1] 6 Set PMVnext[0] = MV[0] 7 Set PMVnext[1] = MV[1] 8 End of processing

F.4.11. Bilinear Interpolation by Rounding Up (interpolate) FU

F.4.12. Block Average (BlockAverage) FU This submodule compute the average of two pixels for direct and bi-directional modes compensation; or just pass the input pixel to the output port if the macroblock is in forward or backward modes. (Algo_Interp_TwoHypothesesAvg)

F.4.13. Video Reconstruction (VR) Unit

Name of Function Unit Algo_PR_U8AddI16Saturation (Picture Reconstruction (VR) Unit) Description This module adds texture and MC prediction block. Operation Range 8x8 block-basis Applicable decoding solution 13818-2 Simple Profile Function Prototype Void fuPR_U8AddI16Saturation (GCU IN_OUT gcu, DATA Int* IN f, DATA Int* IN PelPred, DATA Int IN INTRA, DATA Int* OUT d); Input/Output Data Input A list of 64 integer coefficient elements, one list per f[64] 8x8 block. PelPred[64] A list of 64 integer MC coefficient elements, one list per 8x8 block.

© ISO/IEC 2008 – All rights reserved 17 Output A list of 64 integer coefficient elements, one list per d[64] 8x8 block. Control Signals Input FU doesn’t require PelPred, and d[i] = f[i] when INTRA INTRA= 1. It computes d[64] that the saturation of f[i]+ PelPred[i] when INTRA = 0 Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read INTRA. 2 Read f[0..63]. 3 If INTRA == 1, go to Step 7. 4 Read PelPred[0..63]. 5 Calculate f[i]+ PelPred[i], saturate, and store it into d[0..63]. 6 Go to Step 8. 7 Saturate f[0..63], and then store f[0..63] into d[0..63]. 8 Output d[0..63]. 9 End of processing

F.5. FU’s for MPEG-4 SP

AC/DC – IDCT Blocks : DC DC Reconstruction Reconstruction Inverse Inverse AC Inverse Inverse DC/AC Luminance Chrominance Scan Prediction Quantisation Transform Splitter

DCR L DCR C IAP IS IQ IT DC Split 16x16 16x16 16x16

DCR L DCR C IAP 16x8 16x8 16x8

DCR L DCR C IAP 8x8 8x8 8x8

Motion Compensation Blocks : Parser: Formatting Blocks :

Address Frame Interpolation Adder generator buffers

Merger Serialize 444 Address FrameBuf Interpolate Add 16x16

RECONFIGURABLE Merger 422 Address GENERIC 8x16 PARSER

Merger 420 Address 8x8

Authors : Christophe Lucarz ([email protected]) and Andrew Kinane ([email protected]) - 14 December 2006 Fig.6-3. List of Functional Units for MPEG-4 SP

18 © ISO/IEC 2008 – All rights reserved 4:2:0

Y component WD

FrameBuf WA MV RD Address RA Interpolate 16x16 halfpel MOT

BTYPE Add VID Y TEX

MV Y SIGNED f BTYPE IT

BTYPE Y QUANT DCR QF_DC 8x8 L IQ F PTR QFS Y AC_PRED_DIR IAP C D

_ 8x8 QF_AC S F Q QFS DC QFS_AC IS Split PQF_AC Y Component

RECONFIGURABLE U component GENERIC WD

PARSER

FrameBuf MV WA RD Address RA Interpolate 8x8 halfpel MOT

BTYPE Add Merger420 VID VID U TEX

MV U SIGNED f BTYPE IT BTYPE U QUANT DCR MPEG Serialize bits QF_DC 8x8 C IQ F PTR

AC_PRED_DIR QFS U IAP C

D 8x8 _ QF_AC S F Q QFS DC QFS_AC IS Split PQF_AC U Component

WD V component

FrameBuf WA MV RD Address RA Interpolate 8x8 halfpel MOT

Add BTYPE VID V TEX

MV V SIGNED f BTYPE V BTYPE IT QUANT DCR QF_DC 8x8 C IQ F PTR

QFS V AC_PRED_DIR IAP C D

_ 8x8 QF_AC S F Q QFS DC QFS_AC IS Split PQF_AC

V Component

Motion Compensation Blocks AC/DC – IDCT Blocks Authors : Christophe Lucarz ([email protected]) and Andrew Kinane ([email protected]) - 14 December 2006

Fig.6-4. Configuration of Functional Units for MPEG-4 SP

F.5.1. Syntax Parsing (SYP) FU

Name of Function Unit Algo_ SynP_Mp4vSP (Syntax Parsing FU) Description Operation range Entire video session Applicable decoding 14496-2 Simple Profile solution Function prototype Void FU’synFSM(GCU* gcu, DATA Int IN_OUT state, DATA Int IN SymIdx, DATA Int OUT vld_table_ID, SIG Int OUT nMB, DATA int_queue IN_OUT mb_type_queue, DATA int_queue IN_OUT mb_cbp_queue, DATA bool_queue IN_OUT mb_acpred_queue, DATA int_queue IN_OUT mb_dqp_queue, DATA int_queue IN_OUT mb_dc_queue, DATA int_queue IN_OUT mb_ac_queue); Input/Output Data Input state The previous state represented by an integer SymIdx: ID of the last decoded symbol by fuVLD Output state The output state represented by an integer vld_table_ID ID of a VLC table, which consists of all valid bit patterns for decoding the next symbol. QP An array of quantization scale, Then, the decoded symbol is pushed into the corresponding queue, respectively.  mb_type: A macroblock type.  mb_cbp: A macroblock coded pattern.

© ISO/IEC 2008 – All rights reserved 19  mb_acpred: An AC prediction enabling flag.  mb_quant: A QP value is a 5-bit integer that are computed from the quant_scale value in the video header or slice header, and the dquant value in the macroblock headers.  mb_dc: A quantized differential DC value (a 12- bit integer).  mb_ac: A triplet of (LAST, RUN, LEVEL) of AC symbol. Control signals Input Output Context information Input Output Internal processing algorithm Step process 1 Read the previous state and the last decoded symbol index SymIdx in the last call to fuVLD() 2 After considering the previous state and SymIdx, internal variables are updated. If one of mb_type, mb_cbp, mb_acpred, mb_quant, mb_dc or mb_ac is generated, push it into the corresponding queue, respectively 3 The parsing status is changed to a new state and a vld_table_ID representing the next symbol set is generated according to Specification definition 4 The new state and vld_table_ID are output 5 End of processing

F.5.2. Entropy Decoding FU

Name of Function Unit Algo_ VLD_Mp4vSP (Entropy Decoding FU) Description Operation range Entire video session Applicable decoding 14496-2 Simple Profile solution Function prototype Void fuVLD_VO (GCU* gcu, DATA STREAM_POS IN_OUT bitstream_pos, SIG Int OUT width, SIG Int OUT height, SIG Bool OUT short_video_header_flag); Void fuVLD_VOP (GCU* gcu, DATA STREAM_POS IN_OUT bitstream_pos, SIG Bool OUT data_partition_flag, SIG Int OUT intra_vlc_thr, SIG Int OUT quant_scale); Void fuVLD_SLICE (GCU* gcu, DATA STREAM_POS IN_OUT bitstream_pos, SIG Int OUT quant_scale, SIG Int OUT nMB); Void fuVLD_MB (GCU* gcu, DATA STREAM_POS IN_OUT stream_pos, DATA Int IN vld_table_ID, DATA Int OUT SymIdx); Input/Output Data Input stream_pos a location indicator to describe the consuming status of MPEG-4 visual elementary bitstream (a sequence of one-bit binary values)

20 © ISO/IEC 2008 – All rights reserved vld_table_ID ID of the VLC table to be used for decoding the next symbol Output SymIdx coded symbol index obtained after one operation, which represents for one of the following symbols  not_coded: This is a 1-bit flag which signals if a macroblock is coded or not. (Refer to 6.3.6 of 14496-2:2004)  mcbpc: This coded is used to derived the macroblock type and the coded block pattern for chrominance. (Refer to 6.3.6 of 14496-2:2004)  ac_pred_flag: This is a 1-bit flag which indicates if either the first row or the first colmn of ac coefficients are differentially coded for intra coded macroblocks. (Refer to 6.3.6 of 14496-2:2004)  cbpy: A pattern of non-transparent luminance blocks with at least one non-intra DC transform coefficient, in a macroblock. (Refer to 6.3.6 of 14496-2:2004)  dquant: A 2-bit code which specifies the change in the quantizer. (Refer to 6.3.6 of 14496-2:2004)  intra_dc_coefficient: represents the value of an intra DC coefficient. (Refer to 6.3.7 of 14496- 2:2004)  dct_dc_differential: represents the value of an intra DC differential. (Refer to 6.3.7 of 14496- 2:2004)  DCT_coefficient: represents a non-zero AC coefficient using (LAST, RUN, LEVEL) pair. Control signals Input Output width 13-bits Integer value (fuVLD_VO) height 13-bits Integer value (fuVLD_VO) short_video_header_flag one-bit Boolean value (fuVLD_VO) data_partition_flag one-bit Boolean value (fuVLD_VOP) intra_vlc_thr three-bits Integer value (fuVLD_VOP) quant_scale represent the absolute value of quant to be used for dequantizing the macroblock. (fuVLD_VOP, fuVLD_SLICE) nMB the first coded MB in a video packet. (fuVLD_SLICE) if the video bitstream is coded without video packet, this number is equal to the total number of MB in a frame. Context information Input Output Internal processing algorithm Step process 1 Using leading bits from the input bitstream to obtain the symbol by look up the table identified by vld_table_ID 2 Flush used bits for the current symbol. 3 Output SymIdx 4 End of processing

F.5.3. MB Generation (MBG) FU

Name of Function Unit Mgnt_MBGen_Mp4vSP (MBG FU)

© ISO/IEC 2008 – All rights reserved 21 Description Operation range Video packet-basis Applicable decoding solution 14496-2 Simple Profile Function prototype Void fuMBG(GCU* gcu, DATA queue IN_OUT mb_type_queue, DATA queue IN_OUT mb_dqp_queue, DATA queue IN_OUT mb_acpred_queue, DATA queue IN_OUT mb_cbp_queue, DATA queue IN_OUT mb_dc_queue, DATA queue IN_OUT mb_ac_queue, DATA Int* OUT QFS, SIG Bool IN intra_dc_vlc, SIG Int OUT quantiser_scale, SIG Bool OUT ac_pred_flag, SIG Bool* OUT CodedBlockPattern); Input/Output Data Input mb_type_queue Refer to 5.2.2 mb_dqp_queue Refer to 5.2.2 mb_acpred_queue Refer to 5.2.2 mb_cbp_queue Refer to 5.2.2 mb_dc_queue Refer to 5.2.2 mb_ac_queue Refer to 5.2.2 An array of quantized differential DC value (a 12-bit integer) and triplets of (LAST, RUN, LEVEL) of AC symbols per 8x8 block Output QFS[6][64] Six lists of 64 integer elements, one list per 8x8 block Control signals Input intra_dc_vlc one-bit Boolean value, which indicate DC is coded by a separate DC table. Output quantiser_scale a 5-bit integer value for describing the quantization step in the current MB, which is derived from the running QP and dQuant to calculate QP and determine dc_scaler for current MB by referring to Table 7-1 (14496-2:2004) ac_pred_flag one-bit Boolean value for describing whether the AC prediction is enabled for the current MB CodedBlockPattern An array of 6 Boolean values for describing whether the individual block includes one or more DCT coefficients that are decoded as (LAST, RUN, LEVEL) symbol nMB The index of the MB, whose data is being prepared Context information Input Output Internal processing algorithm Step process 1 Set all elements of QFS to zero 2 Pop mb_type from mb_type_queue (for inter-mode use) 3 Pop quant_scale from mb_quant_queue. 4 Pop ac_pred_flag from mb_acpred_queue 5 Pop CodedBlockPattern[0…6] from mb_cbp_queue. 6 i=0 //i= {0 .. 5}, 6 blocks 7 If (intra_dc_vlc == FALSE), set j to 0 and go to Step 10 8 Pop a quantized differential DC from mb_dc_queue and store it into QFS[i][0] 9 Set j to 1. 10 Pop a series of the quantized (possibly differential) AC values from mb_ac_queue until a symbol with LAST == 1 are found. Then, decode (RUN, LEVEL) codes and store results in QFS[i][j..63]

22 © ISO/IEC 2008 – All rights reserved 11 i++. If i < 6, go to Step 7 12 Output data QFS[0..5][0…64] and control signals quant_scale, ac_pred_flag and CodedBlockPattern[0..5] 13 End of processing

F.5.4. DC Reconstruction (DCR) FU To be integrated with 5.5.10 by DCU Name of Function Unit Algo_DCR_AdaptiveHorizontalOrVerticalPred (DCR FU) Description Operation range MB-basis Applicable decoding 14496-2 Simple Profile solution Function prototype Void fuDCR (GCU IN_OUT gcu, DATA Int* IN QFS, DATA Int* OUT QFSP, SIG Int IN quantiser_scale, SIG Bool* IN CodedBlockPattern, SIG Bool IN ac_pred_flag, SIG Int IN nMB); Input/Output Data Input QFS[6][64] Refer to 5.2.4 Output QFS’[6][64] Six lists of 64 integer elements, one list per 8x8 block (only the first element of each list in QFS’ is changed if compared to QFS) Control signals Input quant_scale Refer to 5.2 CodedBlockPattern Refer to 5.2 to determine dct_Mode (INTRAQ or Not), which is used for calculating dQuant ac_pred_flag Refer to 5.2 to determine the direction of inverse DC prediction nMB Refer to 5.2 to check the position of the neighboring block (NULL or not), which is important for getting the neighboring DC coefficients Output QP per MB: checkrange(quantiser_scale + dQuant) by Quant_precision Context information Input 11 (5/3/3) neighboring to compute the direction of DC/AC prediction for DC coefficients 6 (4 Y/1 Cb/1 Cr) block(s) (DCleft/DCtop/DClefttop) Output ac_pred_direction[6] 6 AC prediction directions,: one-bit Boolean [Horizontal/Vertical] to sent it to IS DC[6] 6 DC coefficients, each is a 12-bits integer for one 8x8 block to be stored in GCU for decoding the next neighboring MB by DCR Internal processing algorithm Step process 1 Refer Table 7-1 (14496-2:2004) to retrieve dc_scaler by using Control signal quantiser_scale as the look-up index. 2 i=0 //i= {0 .. 5}, 6 blocks 3 Read the predicted quantised DC coefficient (PQF_DC = QFS[i][0]) for block i. 4 Read 3 neighboring DC coefficients of block i either from Context Information or the output of previous iterations (DC[0…i-1]) 5 If ac_pred_flag == ‘FALSE’, go to Step 7

© ISO/IEC 2008 – All rights reserved 23 6 Determine ac_pred_direction according to Section 7.4.3.1 (14496-2:2004) for block i 7 Calculate QF_DC by adding PQF_DC with the predictor (DCtop/left//dc_scaler) 8 Saturate QF_DC to the range [-2048, 2047]. 9 Calculate F_DC’’ by applying inverse quantisation to QF_DC 10 Calculate QFS’[i][0] by saturating F_DC’’ to the range [-2048, 2047] 11 Copy QFS[i][1…63] to QFS’[i][1…63] 12 Output QFS’[i][0…63] and ac_pred_direction[i] for block i 13 i++. If i < 6, go to Step 3 14 End of processing

F.5.5. Inverse Scan (IS) FU

Name of Function Unit Algo_IS_ZigzagAndAlternateVertical_8x8 Description This module inverts the one-dimensional array of coefficients that ordered in zigzag or alternate scan to 2D raster order. Operation Range 8x8 block-basis Applicable decoding solution 13818-2 Simple Profile Function Prototype Void fuIS_ZigzagAndAlternateVertical_8x8 (GCU IN_OUT gcu, DATA Int* IN QFS, DATA Int* IN QF, DATA Int IN AltScan, DATA Int* OUT M); Input/Output Data Input A list of 64 integer coefficient elements, one list per QFS[64] 8x8 block. Output A list of 64 reordered integer coefficient elements, QF[64] one list per 8x8 block. Control Signals Input Indicates zigzag or alternate scan. AltScan flag is 0 AltScan when zigzag, 1 when alternate scan. It must set every block. Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read AltScan. 2 Read QFS[0..63]. 3 If AltScan == 1, go to Step 6 4 Rearrange QFS[0..63] in zigzag scan order, and store it into QF[0..63]. 5 Go to Step 7. 6 Rearrange QFS[0..63] in field scan order, and store it into QF[0..63]. 7 Output QF[0..63]. 8 End of processing

F.5.6. Inverse AC Prediction (IAP) FU To be integrated with 5.5.13 by DCU Name of Function Unit Algo_IAP_AdaptiveHorizontalOrVerticalPred (Inverse AC prediction (IAP) FU) Description Operation range MB-basis

24 © ISO/IEC 2008 – All rights reserved Applicable decoding 14496-2 Simple Profile solution Function prototype Void fuIAP (GCU* gcu, DATA Int* IN PQF, DATA Int* OUT QF, SIG Int IN quantiser_scale, SIG Bool IN ac_pred_flag, SIG Int IN nMB); Input/Output Data Input PQF[6][8][8] Refer to 5.2.6. Output QF[6][8][8] Six 2D arrays of 8x8 integer elements Control signals Input quant_scale Refer to 5.2 ac_pred_flag Refer to 5.2 to decide AC is differentially coded or not nMB Refer to 5.2 to check the position of the neighboring block (NULL or not), which is important for getting the neighboring AC coefficients Output Context information Input

QFA and QFC: Quantised AC coefficients of left- and top-adjacent Macroblocks  to be used as AC prediction values  two sets of 7 row Luminance coefficients from above adjacent Macroblock QFC[2…3][0][1...7] If the above adjacent Macroblock is not available due to VOP or VP boundary, then QFC[2...3][0][1...7] shall be assumed to be zero.  two sets of 7 column Luminance coefficients from left adjacent Macroblock QFA[1,3][1...7] [0] If the left adjacent Macroblock is not available due to VOP or VP boundary, then QFA[1,3]1...7][0] shall be assumed to be zero  two sets of 7 row Chrominance coefficients from above adjacent Macroblock QFC[4..5][0][1...7] If the above adjacent Macroblock is not available due to VOP or VP boundary, then QFC[4..5][0][1...7] shall be assumed to be zero  two sets of 7 column Chrominance coefficients from left adjacent Macroblock QFA[4..5][1...7] [0] If the left adjacent Macroblock is not available due to VOP or VP boundary, then QFA[4..5][1...7][0] shall be assumed to be zero QPA and QPC The quant_scale values of left- and top-adjacent MBs  If an adjacent MB is not available due to VOP or VP boundary, then those QP values are not used in this FU Output Internal processing algorithm Step process 1 Copy PQF[i][v][u] to QF[i][v][u] (i = 0 to 5, u, v = 0 to 7) 2 if (ac_pred_flag == 'OFF') then Step 12

3 Read quant_scale as QP, and QPA and QPC 4 i=0 //i= {0 .. 5}, 6 blocks

© ISO/IEC 2008 – All rights reserved 25 5 if (‘ac_prediction_direction[i]’ == 'Horizontal' ) goto Step 5; else goto Step 8 6 Set the Horizontal prediction values QFA[1..7] if ( i == 0 ) QFA[1..7] = QFA[1][1..7][0] if ( i == 1 ) QFA[1..7] = QF[0][1..7][0] if ( i == 2 ) QFA[1..7] = QA[3][1..7][0] if ( i == 3 ) QFA[1..7] = QF[2][1..7][0] if ( i == 4 ) QFA[1..7] = QFA[4][1..7][0] if ( i == 5 ) QFA[1..7] = QFA[5][1..7][0] 7 Horizontal inverse prediction is performed as below

QF[i][v][0] = PQF[i][v][0] + (QFA[v] * QPA) // QP v = 1 to 7 8 goto Step 10 9 Set the Vertical prediction values QFC[1..7] if (i == 0 ) QFC[1..7] = QFC[2][0][1..7] if( i == 1 ) QFC[1..7] = QFC[3][0][1..7] if( i == 2 ) QFC[1..7] = QF[0][0][1..7] if( i == 3 ) QFC[1..7] = QF[1][0][1..7] if( i == 4 ) QFC[1..7] = QFC[4][0][1..7] if( i == 5 ) QFC[1..7] = QFC[5][0][1..7] 10 Vertical inverse prediction is performed as below. QF[i][0][u] = PQF[i][0][u] + (QFC[u] * QPC) // QP u = 1 to 7 11 Saturation process for QF[i][v][u], in the range of [-2048, +2047] 12 i++. If i<6, goto Step 4 13 End of processing

F.5.7. Inverse Quantization (IQ) FU

Name of Function Unit Algo_IQ_QSAndQmatrixMp4vOrH.263Scaler (Inverse quantization (IQ) FU) Description Operation range MB-basis Applicable decoding 14496-2 Simple Profile solution Function prototype Void fuIQ (GCU* gcu, DATA Int* IN QF, DATA Int* OUT F, SIG Int IN quantiser_scale); Input/Output Data Input QF[6][8][8] Refer to 5.2.7 Output F[6][8][8] Six 2D arrays of 8x8 12 bits signed integer elements Control signals Input quant_scale Refer to 5.2.2 Output Context information Input Output Internal processing algorithm Step Process 1 Read quant_scale 2 i=0 //i= {0 .. 5}, 6 blocks 3 Copy DC coefficient from QF[i][0][0] to F[i][0][0] for block i 4 Read 63 other quantized coefficients for block i 5 Dequantize 63 AC coefficients 6 Saturation check (see MPEG-4 14496-2:2004). 7 Mismatch control 8 Output 64 dequantized coefficients for block I 9 i++. If i < 6, go to Step 3.

26 © ISO/IEC 2008 – All rights reserved 10 End of processing

F.5.8. Algo_MVR_MedianOfThreeLeftAndTopAndTopRight

F.5.9. DC Reconstruction MxN XXXX (DCR_MxN_XXXX) FU To be changed to C-pseudo code version by DCU To be integrated with 5.5.4 by DCU

Algo_DCR_AdaptiveHorizontalOrVerticalPred_XXXX_MxN (DC Name of Function Unit Reconstruction MxN FU) Generates DC coefficients of L 8x8 block(s) based on prediction and Description DC coefficients of neighbouring blocks. Specific to XXXX data only. Operation range Applicable decoding solution 14496-2 Simple Profile Parameters Name Description MAXW_IN_MB Width of the frame in macroblock MB_COORD_SZ Number of bits used to represent the “macroblock coordinates” data type BTYPE_SZ Number of bits used to represent the “btype” data type SAMPLE_SZ Number of bits used to represent DCT coefficients INTRA BYTPE token value corresponding to an INTRA block INTER BYTPE token value corresponding to an INTER block NEWVOP BYTPE token value corresponding to an NEWVOP information QUANT_MASK 5 bit mask used to extract quant_scale parameter from BTYPE token ACCODED Single bit mask used to extract pattern_code parameter from BTYPE token QUANT_SZ Number of bits used to store quant_scale locally Dummy value used for DC coefficients for uncoded blocks. Should be DC_VAL 2^(bits_per_pixel+2) Single bit mask used to identify if AC prediction has been carried out from ACPRED the BTYPE token Input/Output Tokens A list of L integer element(s), one value per Input QFS_DC[L]* 8x8 block. A list of L integer element(s), one value per Output QF_DC[L]* 8x8 block. Control Tokens A token whose bits define the coding mode Input BTYPE of the input 8x8 block. QP[L]* QP = quantiser_scale A flag to communicate the direction of Output prediction to the IAP block but also info AC_PRED_DIR[L]* about the scan mode of the block to the inverse scan block. Context Tokens Input Output PTR[L]* The address of the 8x8 block used for the prediction in the encoder

© ISO/IEC 2008 – All rights reserved 27 A flag to indicate to the IDCT module that the block is either INTER or INTRA. This is SIGNED[L]* used when saturating the reconstructed pixels if necessary Internal processing actions Actions Process This block contains no actions itself, but instantiates two sub-actors, DCR_addressing_MxN and DCR_invpred_MxN_XXXX Actions schedule State Action New state

*At present these tokens are presented serially in a horizontal raster manner.

F.5.9.1. DC Reconstruction 8x8 Luminance (DCR_8x8_LUMA) FU This FU is the instantiation of the DC Reconstruction MxN XXXX (DCR_MxN_XXXX) FU described in Section F.5.9, with M, N, L and XXXX equal to 8, 8, 1 and ”LUMA” respectively.

F.5.9.2. DC Reconstruction 8x8 Chrominance (DCR_8x8_CHROMA) FU This FU is the instantiation of the DC Reconstruction MxN XXXX (DCR_MxN_XXXX) FU described in Section F.5.9, with M, N, L and XXXX equal to 8, 8, 1 and ”CHROMA” respectively.

F.5.10. DC Reconstruction Addressing MxN (DCR_addressing_MxN) FU’s (CAL- based) To be changed to C-pseudo code version by DCU Algo_DCRAddr_ThreeLeftTop_MxN (DC Reconstruction Name of Function Unit Addressing MxN FU) This module calculates the addresses of the three neighbouring Description blocks used for DC prediction for the each 8x8 block. Operation range Applicable decoding solution 14496-2 Simple Profile Parameters Name Description MAXW_IN_MB Width of the frame in macroblock Number of bits used to represent the “macroblock coordinates” data MB_COORD_SZ type BTYPE_SZ Number of bits used to represent the “btype” data type SAMPLE_SZ Number of bits used to represent DCT coefficients INTRA BYTPE token value corresponding to an INTRA block NEWVOP BYTPE token value corresponding to an NEWVOP information Input/Output Tokens Input Output Control Tokens A token whose bits define the coding mode of the input Input BTYPE 8x8 block.

28 © ISO/IEC 2008 – All rights reserved Output Context Tokens Input The addresses of the 8x8 block neighbour to the left of A[L]* each of the 8x8 blocks in the current macroblock The addresses of the 8x8 block neighbour to the above Output B[L]* left of each of the 8x8 blocks in the current macroblock The addresses of the 8x8 block neighbour to the above of C[L]* each of the 8x8 blocks in the current macroblock Internal processing actions actions process start Check for NEWVOP. If detected, initialise flags and parameters. gwtw Decode VOP width in macroblocks geth Decode VOP height in macroblocks read.intra Check if current 8x8 block is INTRA coded read.other Check if current 8x8 block is not INTRA coded predict Locate prediction address indices A, B and C for current 8x8 block. advance Update flags and move to next 8x8 block if any. Actions schedule State Action New state read start Getw Getw getw Geth Geth Geth Read Read read.intra Predict Read read.other Advance Predict Predict Advance Advance advance Read *At present these tokens are presented serially in a horizontal raster manner.

F.5.10.1. DC Reconstruction Addressing 8x8 (DCR_addressing_8x8) FU This FU is the instantiation of the DC Reconstruction Addressing MxN (DCR_addressing_MxN) FU described in Section F.5.10, with M, N and L equal to 8, 8 and 1, respectively.

F.5.11. DC Reconstruction Inverse Prediction MxN XXXX (DCR_invpred_MxN_XXXX) FU (CAL-based) To be changed to C-pseudo code version by DCU Algo_DCRInvPred_XXXX_MxN (DC Reconstruction Inverse Name of Function Unit Prediction Luma MxN FU) Generates DC coefficients of four 8x8 blocks based on prediction and Description DC coefficients of neighbouring blocks. Specific to XXXX data only. Operation range Applicable decoding solution 14496-2 Simple Profile Parameters Name Description MAXW_IN_MB Width of the frame in macroblock MB_COORD_SZ Number of bits used to represent the “macroblock coordinates” data type BTYPE_SZ Number of bits used to represent the “btype” data type

© ISO/IEC 2008 – All rights reserved 29 SAMPLE_SZ Number of bits used to represent DCT coefficients INTRA BYTPE token value corresponding to an INTRA block INTER BYTPE token value corresponding to an INTER block NEWVOP BYTPE token value corresponding to an NEWVOP information QUANT_MASK 5 bit mask used to extract quant_scale parameter from BTYPE token Single bit mask used to extract pattern_code parameter from BTYPE ACCODED token QUANT_SZ Number of bits used to store quant_scale locally Dummy value used for DC coefficients for uncoded blocks. Should be DC_VAL 2^(bits_per_pixel+2) Single bit mask used to identify if AC prediction has been carried out ACPRED from the BTYPE token Input/Output Tokens A list of L integer element(s), one value per Input QFS_DC[L]* 8x8 block. A list of L integer element(s), one value per Output QF_DC[L]* 8x8 block. Control Tokens A token whose bits define the coding mode Input BTYPE of the input 8x8 block. QP[L]* QP = quantiser_scale A flag to communicate the direction of Output prediction to the IAP block but also info AC_PRED_DIR[L]* about the scan mode of the block to the inverse scan block. Context Tokens The addresses of the 8x8 block neighbour to A[L]* the left of each of the 8x8 blocks in the current macroblock The addresses of the 8x8 block neighbour to Input B[L]* the above left of each of the 8x8 blocks in the current macroblock The addresses of the 8x8 block neighbour to C[L]* the above of each of the 8x8 blocks in the current macroblock The address of the 8x8 block used for the PTR[L]* prediction in the encoder A flag to indicate to the IDCT module that Output the block is either INTER or INTRA. This is SIGNED[L]* used when saturating the reconstructed pixels if necessary Internal processing actions actions process start Check for NEWVOP. If detected, initialise flags and parameters. read.inter_ac Check for an INTER block with coded AC coefficients. Read an INTRA block. Retrieve 'dc_scalar'. See ISO/IEC 14496-2 Table 7-1. read.intra Dequantise the DCT coeff used for prediction according to ISO/IEC 14496-2 Section 7.4.3.2. Set AC_PRED_DIR. This action is XXXX specific. read.other Check for any other block except INTRA. skip Consume BTYPE token

30 © ISO/IEC 2008 – All rights reserved Do inverse prediction and dequantisation of INTRA DC coefficient according getdc.intra to ISO/IEC 14496-2 Section 7.4.3.2 and Section 7.4.4.1.1. getdc.inter Do inverse quantisation of INTER DC coefficient. sat Saturate reconstructed value into appropriate range. advance Move to next 8x8 block and update flags. Actions schedule State Action New state read start getw read read.inter_ac inter read read.intra intra read read.other advance getw skip geth geth skip read intra getdc.intra sat inter getdc.inter sat sat sat advance advance advance read *At present these tokens are presented serially in a horizontal raster manner.

F.5.11.1. DC Reconstruction Inverse Prediction 8x8 Luma (DCR_invpred_8x8_LUMA) FU This FU is the instantiation of the DC Reconstruction Inverse Prediction MxN XXXX (DCR_invpred_MxN_XXXX) FU described in Section F.5.11, with M, N, L and XXXX equal to 8, 8, 1 and ”LUMA” respectively.

F.5.11.2. DC Reconstruction Inverse Prediction 8x8 Chroma (DCR_invpred_8x8_CHROMA) FU This FU is the instantiation of the DC Reconstruction Inverse Prediction MxN XXXX (DCR_invpred_MxN_XXXX) FU described in Section F.5.11, with M, N, L and XXXX equal to 8, 8, 1 and ”CHROMA” respectively.

F.5.12. Inverse AC Prediction MxN (IAP_MxN) FU’s To be changed to C-pseudo code version by DCU To be integrated with 5.5.6 by DCU Algo_IAP_AdaptiveHorizontalOrVerticalPred_MxN Name of Function Unit (Inverse AC Prediction MxN FU) This module computes inverse AC prediction for specific AC coefficients of 8x8 blocks that Description have been flagged in the bitsream as coded in this fashion. Operation range Applicable decoding solution 14496-2 Simple Profile Parameters Name Description MAXW_IN_MB Width of the frame in macroblock Number of bits used to represent the “macroblock coordinates” data MB_COORD_SZ type

© ISO/IEC 2008 – All rights reserved 31 SAMPLE_SZ Number of bits used to represent DCT coefficients Input/Output Tokens L lists of 63 integer AC coefficients elements, Input PQF_AC[63][L]* one list per 8x8 block. L lists of 63 reconstructed integer AC Output QF_AC[63][L]* coefficients elements, one list per 8x8 block. Control Tokens A flag to communicate the direction of Input AC_PRED_DIR[L]* prediction to the IAP block. Output Context Tokens The address of the 8x8 block used for the Input PTR[L]* prediction in the encoder Output

Internal processing actions actions process newvop Check for NEWVOP and if detected, initialise flags. skip Skip blocks with no AC prediction. start Initialise flags and read ac_pred_dir and prediction pointer. copy Read the predicted coefficients and reconstruct AC coefficients. advance Move to next 8x8 block if any and update flags. Actions schedule State Action New state Start newvop start Start skip pred Start start pred Pred copy advance Pred advance start *At present these tokens are presented serially in a horizontal raster manner.

F.5.12.1. Inverse AC Prediction 8x8 (IAP_8x8) FU This FU is the instantiation of the Inverse AC Prediction MxN (IAP_MxN) FU’s described in Section F.5.12, with M, N and L equal to 8, 8 and 1, respectively.

F.5.13. Address Generator MxN (addressMxN) FU’s (CAL-based) To be changed to C-pseudo code version Name of Function Unit Mgnt_FBAddr_MxN (Address Generator MxN FU) Generates addresses for the frame buffer block. Write addresses to save the current pixels for retrieval and read addresses to Description retrieve interpolation pixel values. This block is designed for a token stream of four following 8x8 blocks (for only one macroblock) Operation range Applicable decoding 14496-2 Simple Profile solution Parameters

32 © ISO/IEC 2008 – All rights reserved Name Description SEARCHWIN_IN_MB Size of the search window in macroblock MAXW_IN_MB Width of the frame in macroblock MAXH_IN_MB Height of the frame in macroblock ADDR_SZ Number of bits used to represent the “address” data type FLAG_SZ Number of bits used to represent the “flag” data type MV_SZ Number of bits used to represent the “motion vectors” data type Number of bits used to represent the “macroblock coordinates” MB_COORD_SZ data type BTYPE_SZ Number of bits used to represent the “btype” data type INTRA BYTPE token value corresponding to an INTRA block INTER BYTPE token value corresponding to an INTER block NEWVOP BYTPE token value corresponding to an NEWVOP information MOTION BYTPE token value corresponding to an MOTION block ROUND_TYPE Type of rounding applied Input/Output Data Input MV Motion vectors Output Control signals Input WA Write Address Output RA Read Address Context information Input Output halfpel Indicate if the it’s a half pel displacement Internal processing actions actions Process Pure texture, no need to enable the motion addressing to retrieve cmd.noMotion the pixel values Pure motion, need to retreive pixels values and save current cmdMotion pixels for the next reference image (reading and writing processes activated) Getmvx Getting the x coordinate of a motion vector Getmvy Getting the y coordinate of a motion vector cmd.neither No change in the block, MV is 0 Setting the right parameters for the block to be processed (block init coordinates and pointers for memory mapping) Done The processing is finished Generate the write addresses to save the current value of a pixel Write_addr at a given place in the buffer Generate the read addresses to retreive in the buffer the pixel Read_addr values after interpolation Actions schedule State Action New state cmd.noMotion Init cmd.motion Getmvx Cmd cmd.both Init cmd.neither Init init init Generate Generate Done Cmd

© ISO/IEC 2008 – All rights reserved 33 Write_addr Generate Read_addr Generate getmvx getmvx Getmvy getmvy getmvy Init

F.5.13.1. Address Generator 16x16 (address16x16) FU This FU is the instantiation of the Address Generator MxN (addressMxN) FU described in Section F.5.13, with M and N equal to 16 and 16, respectively.

F.5.13.2. Address Generator 8x8 (address8x8) FU This FU is the instantiation of the Address Generator MxN (addressMxN) FU described in Section F.5.13, with M and N equal to 8 and 8, respectively.

F.5.14. Frame Buffer (framebuf) FU (CAL-based) To be changed to C-pseudo code version Name of Function Unit Mgnt_FB (Frame Buffer FU) The frame buffer is the memory space to store only one Description reference image, used for prediction of future images. The size of the buffer is function of the processed block size. Operation range Applicable decoding 14496-2 Simple Profile solution Parameters Name Description ADDR_SZ Number of bits used to represent the “address” data type BUF_SZ Size of the buffer used to store the frames PIX_SZ Number of bits used to represent the “pixels” data type Input/Output Data Input WD Write Data Output RD Read Data Control signals WA Write address Input RA Read Address Output Context information Input Output Internal processing actions action Process RA send data requested on the output: read_data = framebuffer[read_address] Receive the data to store in the frame buffer: WA framebuffer[write_address] = write_data

34 © ISO/IEC 2008 – All rights reserved F.5.15. Bilinear Interpolation with Rounding Control (interpolate) FU To be changed to C-pseudo code version Algo_Interp_HalfpelBilinearRoundingControl (Interpolation Name of Function Unit FU) In the case of a displacement of one half pixel, it is needed to Description find the values of the new pixels. This is done by the FU. Operation range Applicable decoding 14496-2 Simple Profile solution Parameters Name Description FLAG_SZ Number of bits used to represent the “flag” data type PIX_SZ Number of bits used to represent the “pixels” data type Input/Output Data Input RD Data to be used for interpolation Output MOT Motion pixels Control signals Input Halfpel Indicate the “Half Pixel” case Output Context information Input Output Internal processing actions Action Process Start Initializing the processing done Interpolation finished Row_col_0 Reading data in the case of the first row of first column Interpolation process to find the values of the pixels:

Other

Actions schedule State Action New state Start start Interpolate Row_col_0 Interpolate interpolate Other Interpolate Done Start

F.5.16. DC Split (DCSplit) FU (CAL-based) This module splits DC coefficient from other coefficients. (Mgnt_DCSplit)

© ISO/IEC 2008 – All rights reserved 35 F.5.17. MV Split (MVSplit) FU This module must support backward motion vectors. (Mgnt_MVSplit)

F.5.18. MV Reconstruction (MVSplit) FU This module must support extraction of direct, bidirectional, backward-only and forward-only motion vector modes for B frames. (Algo_MVR_MedianOfThreeLeftAndTopAndTopRight)

F.6. FU’s for AVC BP

Fig6-5. Configuration of Functional Units for AVC Baseline and High Profiles

F.6.1. Byte Stream NAL (NALP) Parsing FU This functional unit first converts AVC byte stream into individual NAL units, and then parses the header of an NAL unit, remove the escape bytes from payload and generates the raw byte sequence payload (RBSP) together with its NAL unit type indicator. Name of Function Unit Algo_NALP (Byte Stream NAL (NALP) Parsing FU) Description Operation range Entire video session Applicable decoding solution 14496-10 Baseline Profile Function prototype Void fuNALP (GCU IN_OUT gcu, DATA STREAM_POS IN_OUT stream_pos, DATA STREAM_POS OUT rbsp_byte_pos, DATA Int OUT NumBytesInRBSP, DATA Int OUT nal_ref_idc, DATA Int OUT nal_unit_type DATA Int OUT state); Input/Output Data Input stream_pos a location indicator to describe the consuming status of MPEG-4 AVC Baseline and High Profile elementary bitstream before parsing a new NAL packet (a sequence of one-bit binary values). Output stream_pos: a location indicator to describe the consuming status of MPEG-4 AVC Baseline and High Profile elementary bitstream after parsing a new NAL packet rbsp_byte_pos a location indicator to describe the consuming status of MPEG-4 AVC RBSP (a sequence of one-bit binary values). NumBytesInRBSP the length in byte of the RBSP being parsed nal_ref_idc the nal_ref_idc of the RBSP being parsed nal_unit_type the NAL unit type of the RBSP being parsed state Output of the initial state of this NAL unit, represented by an integer Control signals Input Output Context information Input Output Internal processing algorithm

36 © ISO/IEC 2008 – All rights reserved Step Process 1 Advance the consuming location indicator of stream_pos to skip all “00” byte until finding and consuming one of the patterns “0x000001” or “0x00000001” 2 Read the next byte pointed by stream_pos, interpret it as nal_unit_type, and advance stream_pos by one byte 3 Copy subsequent bytes pointed by stream_pos to that pointed by rbsp_byte_pos one by one except for the last byte “0x03” of every encounter of the pattern “0x000003”. NumBytesInRBSP counts for the total bytes copied during this step 4 Repeat Step 3 until the next 24 bits pointed by stream_pos is “0x000000” or “0x000001”, or encounter the end of the byte stream 5 Rewind the rbsp_byte_pos to the start and output rbsp_byte_pos, NumBytesInRBSP, nal_unit_type 6 End of processing

F.6.2. CAVLC Syntax Parsing FU Name of Function Unit Algo_SynP_CAVLC (CAVLC Syntax Parsing(SynP_CAVLC) FU) Description Operation range Entire video session of CAVLC Applicable decoding 14496-10 Baseline Profile solution Function prototype Void FU’syn_CAVLC( GCU IN_OUT gcu, DATA STREAM_POS IN_OUT stream_pos, DATA Int IN_OUT state, DATA Int IN SymIdx, DATA Int IN SymLen, DATA Int IN vld_table_ID, DATA Int IN predIntraPredMode, DATA Int IN chroma_qp_index_offset, DATA Int IN chroma_format_idc, SIG Int IN_OUT suffixLength, SIG Int IN_OUT level_prefix, SIG Int IN maxNumCoeff, SIG Int IN quant_scale, SIG Int IN iBlk, SIG Int IN iYUV, DATA int_queue IN_OUT mb_type_queue, DATA bool_queue IN_OUT mb_cbp_queue, DATA int_queue IN_OUT mb_intra_pred_mode_queue, DATA int_queue IN_OUT mb_quant_queue, DATA int_queue IN_OUT mb_coef_queue, DATA Int* OUT coeffLevel, DATA Int OUT total_coeff, DATA Int OUT ZerosLeft, SIG Bool OUT transform_8x8_flag, SIG Bool OUT iDC); Input/Output Data Input state The previous state represented by an integer

stream_pos a location indicator to describe the consuming status of MPEG-4 AVC High Profile elementary bitstream before parsing a new NAL packet (a sequence of one- bit binary values). SymIdx ID of the last decoded symbol by fuED

© ISO/IEC 2008 – All rights reserved 37 SymLen Length of the symbol to be decoded

vld_table_ID ID of a VLC table, which consists of all valid bit patterns for decoding the next symbol. In some cases, it represents ID of the first VLC table in a VLC table group.

predIntraPredmode the predicted value of intra prediction mode

chroma_qp_index_off the offset that shall be added to QPy for addressing set the table of QPc

chroma_format_idc a value which specifies the chroma sampling relative to the luma sampling These 5 queues store different types of decoded mb_type_queue, symbols for reconstructing a macroblock. Once a mb_cbp_queue, symbol is generated, it is then pushed into the mb_intra_pred_mode corresponding queue, respectively. Five types of _queue, symbols are: mb_quant_queue,  mb_type: A macroblock type. mb_coef_queue,  mb_cbp: A macroblock coded pattern that is derived from mb_type and coded_block_pattern.  mb_intra_pred_mode: A series of intra prediction modes within a macroblock that is obtained from mb_type, prev_intra4x4_pred_mode_flag , rem_intra4x4_pred_mode and intra_chroma_pred_mode.  mb_quant: QP values for luma and chroma are 6-bit integers computed from initial_qp, slice_qp_delta, chroma_qp_index_offset, and mb_qp_delta.  mb_coef: A quantized integer transformed coefficient value together with the number of zero coefficients before it, which is computed from coeff_token, trailing_ones_sign_flag, level_prefix, level_suffix, total_zeros and run_before. Output state: The output state represented by an integer

38 © ISO/IEC 2008 – All rights reserved These 5 queues store different types of decoded mb_type_queue, symbols for reconstructing a macroblock. Once a mb_cbp_queue, symbol is generated, it is then pushed into the mb_intra_pred_mode corresponding queue, respectively. Five types of _queue, symbols are: mb_quant_queue, mb_coef_queue,  mb_type: A macroblock type.  mb_cbp: A macroblock coded pattern that is derived from mb_type and coded_block_pattern.  mb_intra_pred_mode: A series of intra prediction modes within a macroblock that is obtained from mb_type, prev_intra4x4_pred_mode_flag , rem_intra4x4_pred_mode and intra_chroma_pred_mode.  mb_quant: QP values for luma and chroma are 6-bit integers computed from initial_qp, slice_qp_delta, chroma_qp_index_offset, and mb_qp_delta. mb_coef: A quantized integer transformed coefficient value together with the number of zero coefficients before it, which is computed from coeff_token, trailing_ones_sign_flag, level_prefix, level_suffix, total_zeros and run_before.

coeffLevel pointer to the value of the level of a coefficient

total_coeff total number of the coefficients in the block to be decoded

ZerosLeft total number of runs left in the block to be decoded Control signals Input suffixLength a value which specifies the length of the suffix of the level of a coefficient level_prefix a syntax element which represents the value of the prefix of a coefficient maxNumCoeff a value which specifies the maximum number of coefficients within a block to be decoded quant_scale quantization parameter iBlk the block index that is being processed iYUV the index of the component of the picture that is being processed Output suffixLength a value which specifies the length of the suffix of the level of a coefficient level_prefix a syntax element which represents the value of the prefix of a coefficient

transform_8x8 flag a flag to indicate whether the size of the block to be decoded is 8x8 or not

© ISO/IEC 2008 – All rights reserved 39 iDC a Boolean value to indicate if the current processing block is a DC block Context information Input Output Internal processing algorithm Step process 1 Read the previous state and the last decoded symbol index SymIdx in the last call to fuED() 2 After considering the previous state and SymIdx, internal variables are updated. If one of mb_type, mb_cbp, mb_intra_pred_mode, mb_quant, or mb_coef is generated, push it into the corresponding queue, respectively 3 The parsing status is changed to a new state and a vld_table_ID representing the next symbol set is generated according to Specification definition 4 The new state and vld_table_ID are output 5 End of processing

F.6.3. CAVLC CTX FU This functional unit is to determine the context that is required to provide dynamic re-organization of VLC tables in AVC. Name of Function Unit Algo_CTXGEN_CAVLC (CAVLC CTX FU) Description Operation range Entire video session Applicable decoding solution 14496-10 Baseline Profile Function prototype Input/Output Data Input vld_table_ID ID of the leading VLD table in one VLD group, which is used for the subsequent symbol decoding CTX_det_type The method to determine a VLD table among a number of VLD tables in a specific VLD group  CTX_none: no context determination is required.  CTX_coeff_token: Determine one from 5 tables in this group.  CTX_total_zeros: Determine one from 18 tables (15 tables for luma and chroma AC, and 3 for chroma DC) in this group.  CTX_run_before: Determine one from 7 tables in this group. Output vld_table_ID ID of the VLD table to be used for decoding the next symbol. Control signals Input chroma_format_idc a value which specifies the chroma sampling relative to the luma sampling TotalCoeff total number of the coefficients in the block to be decoded profile_idc a value which specifies the profile of the sequence ZerosLeft total number of runs left in the block to be decoded

40 © ISO/IEC 2008 – All rights reserved iBlk the block index that is being processed iDC a Boolean value to indicate if the current processing block is a DC block iYUV the index of the component of the picture that is being processed Output Context information Input MbAddrAavailable, Boolean values to indicate if blkA located to the MbAddrBavailable left of the current block and blkB located above the current block are accessible nA, nB the number of non-zero transform coefficient levels in the block of transform coefficient levels blkA located to the left of the current block and the block of transform coefficient levels blkB located above the current block,respectively. Output Internal processing algorithm Step process 1 If CTX_det_type is equal to “CTX_coeff_token”, go to Step 2; if CTX_det_type is equal to “CTX_total_zeros”, go to step 5; if CTX_det_type is equal to “CTX_run_before”, go to Step 7 2 Calculate nC from ChromaDCLevel, MbAddrAavailable, MbAddrBavailable, nA and nB 3 If 0 <= nC < 2, vld_table_offset = 0; If 2 <= nC < 4, vld_table_offset = 1; If 4 <= nC < 8, vld_table_offset = 2; If 4 <= nC, vld_table_offset = 3; If nC = -1, vld_table_offset = 4; 4 go to Step 9 5 Set vld_table_offset to be TotalCoeff-1 6 go to Step 9. 7 Set vld_table_offset to be zeroLeft-1, and saturate it between 0 to 6 8 go to Step 9 9 set vld_table_ID to be vld_table_ID plus vld_table_offset 10 Output vld_table_ID 11 End of processing

F.6.4. Entropy Decoding FU Name of Function Unit Algo_VLD_CAVLC (Entropy Decoding FU) Description Operation range Entire video session Applicable decoding solution 14496-10 Baseline Profile Function prototype Void fuED_SPS (GCU IN_OUT gcu, DATA STREAM_POS IN_OUT bitstream_pos, SIG Int OUT pic_width_in_mbs_minus1, SIG Int OUT pic_height_in_map_units_minus1, SIG Int OUT profile_idc, SIG Int OUT chroma_format_idc, SIG Int OUT log2_max_frame_num_minus4, SIG Int OUT pic_order_cnt_type,

© ISO/IEC 2008 – All rights reserved 41 SIG Int OUT log2_max_pic_order_cnt_lsb_minus4); Void fuED_PPS (GCU IN_OUT gcu, DATA STREAM_POS IN_OUT bitstream_pos, SIG Bool OUT entropy_coding_mode_flag, SIG Int OUT quant_scale, SIG Int OUT chroma_qp_index_offset, SIG Bool OUT deblocking_filter_control_present_flag, SIG Bool OUT constrained_intra_pred_flag, SIG Bool OUT transform_8x8_mode_flag, SIG Int OUT second_chroma_qp_index_offset); Void fuED_SLICE (GCU IN_OUT gcu, DATA STREAM_POS IN_OUT bitstream_pos, SIG Int IN nal_ref_idc, SIG Int IN nal_unit_type, SIG Int IN log2_max_frame_num_minus4, SIG Bool IN entropy_coding_mode_flag, SIG Bool IN deblocking_filter_control_present_flag, SIG Int OUT first_mb_in_slice, SIG Int OUT slice_qp_delta, SIG Int OUT disable_deblocking_filter_idc, SIG Int OUT slice_alpha_c0_offset_div2, SIG Int OUT slice_beta_offset_div2); Void fuED_MB_CAVLC (GCU IN_OUT gcu, DATA STREAM_POS IN_OUT stream_pos, DATA Int IN_OUT state, DATA Int IN_OUT SymIdx, DATA Int OUT SymLen, DATA int_queue mb_type_queue, DATA bool_queue mb_cbp_queue, SIG Bool IN transform_8x8_mode_flag, SIG Bool IN prev_intra_pred_mode_flag, SIG Int IN chroma_format_idc, SIG Int IN suffixLength, SIG Int IN level_prefix, SIG Int IN iBlk, SIG Bool IN iDC, SIG Int IN iYUV); Void fuED_MB_CABAC( ……. ); Input/Output Data Input stream_pos a location indicator to describe the consuming status of an AVC RBSP before decoding a new symbol (a sequence of one-bit binary values) vld_table_ID ID of the VLD table to be used for decoding the next symbol. Output stream_pos a location indicator to describe the consuming status of an AVC RBSP after decoding a new symbol (a sequence of one-bit binary values)

42 © ISO/IEC 2008 – All rights reserved SymIdx coded symbol index obtained after one VLD decoding operation, which represents for one of the following symbols  mb_type  prev_intra4x4_pred_mode_flag  rem_intra4x4_pred_mode  intra_chroma_pred_mode  coded_block_pattern  mb_qp_delta  pcm_sample_luma  pcm_sample_chroma  coeff_token  trailing_ones_sign_flag  level_prefix: the prefix part of level.  level_suffix: the suffix part of level.  total_zeros: the total zero coefficients before the last non-zero coefficient.  run_before:  IT_coefficient: represents a non-zero integer transformed coefficient. Control signals Input Output pic_width_in_mbs_minus1 Integer value (fuED_SPS) pic_height_in_map_units_minus1 Integer value (fuED_SPS) initial_qp (fuED_PPS) chroma_qp_index_offset Integer value (fuED_PPS) deblocking_filter_control_present_flag a 1-bit flag which signals if the deblocking filter control information is present or not (fuED_PPS) first_mb_in_slice the first coded MB in a slice. (fuED_SLICE) slice_qp_delta (fuED_SLICE) disable_deblocking_filter_idc (fuED_SLICE) slice_alpha_c0_offset_div2 (fuED_SLICE) slice_beta_offset_div2 (fuED_SLICE) Context information Input Output Internal processing algorithm Step Process 1 Using leading bits from the input bitstream to obtain the symbol by look up the table identified by vld_table_ID. 2 Flush used bits for the current symbol 3 Output SymIdx. 4 End of processing

F.6.5. MBG4x4 FU Name of Function Unit Mgnt_MBGen_AVCBP (MBG4x4 FU) Description Operation range Video slice-basis Applicable decoding 14496-10 Baseline Profile solution Function prototype Void fuMBG4x4(GCU IN_OUT gcu, DATA int_queue IN_OUT mb_type_queue, DATA bool_queue IN_OUT mb_cbp_queue,

© ISO/IEC 2008 – All rights reserved 43 DATA int_queue IN_OUT mb_intra_pred_mode_queue, DATA int_queue IN_OUT mb_quant_queue, DATA int_queue IN_OUT mb_luma_coef_queue, DATA int_queue IN_OUT mb_chroma_coef_queue, DATA int_queue IN_OUT mb_deblocking_filter_info_queue, DATA Int* OUT QFSY4, DATA Int* OUT QFSC SIG Int OUT mb_type, SIG bool * OUT mb_cbp, SIG Int* OUT intra_pred_mode, SIG Int* OUT mb_quant, SIG Int* OUT mb_deblocking_filter_info); Input/Output Data Input mb_type_queue mb_cbp_queue mb_intra_pred_mode_queue mb_quant_queue mb_luma_coef_queue mb_chroma_coef_queue mb_deblocking_filter_flag Output QFSY4[16][4][4] Sixteen lists of 16 integer elements, one list per 4x4 block. QFSC[8][4][4] Eight lists of 16 integer elements, one list per 4x4 chroma block Control signals Input Output mb_type mb_cbp mb_intra_pred_mode mb_quant mb_deblocking_filter_info Context information Input Output Internal processing algorithm Step Process 1 Set all elements of QFSY4 and QFSC to zero. 2 Pop mb_type from mb_type_queue 3 Pop mb_cbp[0…23] from mb_cbp_queue 4 Pop a series of the intra prediction modes, mb_intra_pred_mode[0…16], from mb_intra_pred_mode_queue. If mb_type is ‘Intra16x16’, mb_intra_pred_mode[0] indicates intra16x16 prediction mode, otherwise, mb_intra_pred_mode[0…15] indicates intra4x4 prediction mode for luma blocks. mb_intra_pred_mode[16] indicates intra chroma prediction mode for chroma blocks 5 Pop a series of the quantizer scales mb_quant[0...1], from mb_quant_queue. mb_quant[0] is quantizer scale for luma coefficients, and mb_quant[1] is quantizer scale for chroma coefficients 6 Pop mb_deblocking_filter_info[0…2] from mb_deblocking_filter_flag_queue. mb_deblocking_filter_info[0] indicates whether the deblocking filter is applied to macroblock being processed or not. mb_deblocking_filter_info[1] is for slice_alpha_c0_offset_div2, and mb_deblocking_filter_info[2] is for slice_beta_offset_div2. 7 i=0 //i= {0 .. 15}, 16 blocks 8 Pop a series of the quantized differential integer transformed values from mb_coef_queue according to the specification and store results in QFSY4[i][0..3][0..3] 9 i++. If i < 16, go to Step 8.

44 © ISO/IEC 2008 – All rights reserved 10 i=0 //i= {0 .. 7}, 8 blocks. 11 Pop a series of the quantized differential integer transformed values from mb_coef_queue according to the specification and store results in QFSC[i][0..4][0..4]. 12 i++. If i<8, go to Step 11 13 Output data QFSY4[0..15][0…3][0…3], QFSC[0..7][0…3][0...3] and control signals mb_type, mb_cbp, mb_intra_pred_mode[0…16], mb_quant[0...1] and mb_deblocking_filter_info[0…2] 14 End of processing

F.6.6. 4x4 Hadamrd DC Reconstruction (DCR) FU Name of Function Unit Algo_DCR_Hadamard_LUMA_4x4 Description This module computes 2D IIT 4x4 DC luminance coefficients of an intra_16x16 prediction block. 2D IIT is achieved by row column decomposition of the coefficients. Operation Range 16x16 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuDCR_Hadamard_LUMA (GCU IN_OUT gcu, DATA Int* IN Cof, DATA Int* OUT Cofout); Input/Output Data Input A list of 16 integer coefficient elements, one list per Cof[16] 4x4 quantised DC block. Output A list of 16 inverse transformed DC coefficient Cofout[16] elements, one list per 4x4 DC block. Control Signals Input A list of 16 integer. It is need to be set every 4x4 ScalingList[16] block process. This parameter is converted to LevelScale4x4 in this FU. Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read Cof[0..15]. 2 Apply inverse 2D Hadamard transformation to it, and store it into Cof_tmp[0..15].. 3 Dequantize Cof_tmp[0..15] using ScalingList[0..15] and store Cofout[0..15]. 4 Output Cofout[0..15]. 5 End of processing

F.6.7. 2x2 Hadamrd DC Reconstruction (DCR) FU Name of Function Unit Algo_DCR_Hadamard_CHROMA_2x2 Description This module computes 2D IIT 2x2 DC chrominance coefficients of intra block. 2D IIT is achieved by row column decomposition of the coefficients. Operation Range 2x2 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuDCR_Hadamard_CHROMA (GCU IN_OUT gcu, DATA Int* IN levarr, DATA Int* OUT Cof);

© ISO/IEC 2008 – All rights reserved 45 Input/Output Data Input A list of 4 integer coefficient elements, one list per levarr[4] 2x2 quantised DC block. Output A list of 4 inverse transformed DC coefficient Cof[4] elements, one list per 2x2 DC block. Control Signals Input A list of 16 integer. It is need to be set every 4x4 ScalingList[16] block process. This parameter is converted to LevelScale4x4 in this FU. Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read levarr[0..3]. 2 Apply inverse 2D Hadamard transformation to it, and store it into Cof_tmp [0..3].. 3 Dequantize Cof_tmp[0..3] using ScalingList[0..15] and store Cof[0..3]. 3 Output Cof [0..3]. 4 End of processing

F.6.8. 4x4 inverse DC reordering (IR4x4) FU Name of Function Unit Algo_IS_Hadamard_LUMA_4x4 Description This module inverts the one-dimensional array of coefficients that reordered. Operation Range 16x16 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIR_4x4 (GCU IN_OUT gcu, DATA Int* IN DCin, DATA Int* OUT DCout); Input/Output Data Input A list of 16 integer coefficient elements, one list per DCin[16] 4x4 block. Output A list of 16 reordered integer coefficient elements, DCout[16] one list per 4x4 block. Control Signals Input Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read DCin[0..15]. 2 Reorder DCin[0..15], and store it into DCout[0..15]. 3 Output DCout[0..15]. 4 End of processing

F.6.9. 4x4 Inverse scan (IS4x4) FU Name of Function Unit Algo_IS_ZigzagAndField_4x4 Description Inverts the one dimensional array of coefficients to 2D raster order. Supports Zigzag and Field scan. Operation Range 4x4 block-basis Applicable decoding solution 14496-10 High Profile

46 © ISO/IEC 2008 – All rights reserved Function Prototype Void fuIS_ZigzagAndField_4x4 (GCU IN_OUT gcu, DATA Int* IN levarr, DATA Int IN Structure, DATA Int* OUT Lev2d); Input/Output Data Input A list of 16 integer coefficient elements, one list per levarr[16] 4x4 block. Output A list of 16 reconstructed integer coefficient elements, Lev2d[16] one list per 4x4 block. Control Signals Input A flag to indicate the inverse scan mode (zigzag or Structure field. Zigzag when Structure=0 and field when Structure=1.) Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read Structure. 2 Read levarr[0..15]. 3 If Structure == 1, go to Step 6. 4 Rearrange levarr[0..15] in zigzag scan order, and store it into Lev2d[0..15]. 5 Go to Step 7. 6 Rearrange levarr[0..15] in field scan order, and store it into Lev2d[0..15]. 7 Output Lev2d[0..15]. 8 End of processing

F.6.10. Algo_IQ_QSAndIDCTScalerHadamard_4x4 Name of Function Unit Algo_IQ_QSAndIDCTScalerHadamard_4x4 Description This module computes inverse quantization of 4x4 DC blocks. Operation Range 16x16 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIQ_QSAndIDCTScalerHadamard_4x4(GCU IN_OUT gcu, DATA Int* IN Cof, DATA Int IN QP, DATA Int* OUT Cofout Input/Output Data Input A list of 16 integer coefficient elements, one list per Cof [16] 4x4 DC coefficient block which is member of 16 4x4 blocks. QP quantiser_scale Output A list of 16 integer coefficient elements, one list per Cofout[16] 4x4 DC block. Control Signals Input Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read QP.

© ISO/IEC 2008 – All rights reserved 47 2 Read Cof[0..15]. 3 Store dequantized Cof[0..15] into Cofout[0..15]. 4 Output Cofout[0..15]. 5 End of processing

F.6.11. Algo_IQ_QSAndIDCTScalerHadamard_2x2 Name of Function Unit Algo_IQ_QSAndIDCTScalerHadamard_2x2 Description This module computes inverse quantization of 2x2 DC blocks. Operation Range 8x8 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIQ_QSAndIDCTScalerHadamard_2x2(GCU IN_OUT gcu, DATA Int* IN Cof, DATA Int IN QP, DATA Int* OUT Cofout Input/Output Data Input A list of 4 integer coefficient elements, one list per Cof [4] 2x2 DC coefficient block which is member of 4 2x2 blocks. QP quantiser_scale Output A list of 4 integer coefficient elements, one list per Cofout[4] 2x2 DC block. Control Signals Input Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read QP. 2 Read Cof[0..3]. 3 Store dequantized Cof[0..3] into Cofout[0..3]. 4 Output Cofout[0..3]. 5 End of processing

F.6.12. 4x4 Inverse quantization with QuantiserScale and ScalingList FU

Name of Function Unit Algo_IQ_QSAndSLAndIDCTScaler_4x4 Description This module computes inverse quantization of 4x4 blocks with scaling list. Operation Range 4x4 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIQ_QSAndSLAndIDCTScaler_4x4 (GCU IN_OUT gcu, DATA Int* IN Lev2d, DATA Int IN QP, DATA Int* IN ScalingList, DATA Int* OUT Cof); Input/Output Data Input A list of 16 integer coefficient elements, one list per Lev2d[16] 4x4 block. QP quantiser_scale

48 © ISO/IEC 2008 – All rights reserved Output A list of 16 reconstructed integer coefficient elements, Cof[16] one list per 4x4 block. Control Signals Input A list of 16 integer. It is need to be set every 4x4 ScalingList[16] block process. This parameter is converted to LevelScale4x4 in this FU. Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read QP. 2 Read ScalingList[0..15]. 3 Read Lev2d[0..15]. 4 Dequantize Lev2d[0..15] using ScalingList[0..15] and store Cof[0..15]. 5 Output Cof[0..15]. 6 End of processing

F.6.13. 4x4 Inverse transform (IT4x4) FU Name of Function Unit Algo_IT2D_NoPreScaling_4x4 Description This module computes 2D IIT of 4x4 blocks. 2D IIT is achieved by row column decomposition of the coefficients. Operation Range 4x4 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIT2D_NoPreScaling_4x4 (GCU IN_OUT gcu, DATA Int* IN Cof, DATA Int* OUT M); Input/Output Data Input A list of 16 integer coefficient elements, one list per Cof[16] 4x4 block. Output A list of 16 inverse transformed coefficient elements, M[16] one list per 4x4 block. Control Signals Input Output Context Information Input Output Internal Processing Algorithm Step Process 1 Read Cof[0..15]. 2 Calculate inverse transformation of Cof[0..15] as 4x4 block. 3 Store it into M[0..15]. 4 Output M[0..15]. 5 End of processing

F.6.14. 4x4 Intra Prediction (AVC LUMA) Name of Function Unit Algo_IntraPred_LUMA_4x4 Description This module predicts intra 4x4 block in Luma by a given prediction mode. Operation Range 4x4 block-basis

© ISO/IEC 2008 – All rights reserved 49 Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIntraPred_LUMA_4x4(GCU IN_OUT gcu, DATA Int* IN Y_LEFT, DATA Int* IN Y_UP, DATA Int IN Y_UP_LEFT, DATA Int* OUT MPR, SIG Int IN PredMode); Input/Output Data Input Y_LEFT[4] 4 left-side neighbouring pixels of current 4x4 block. The upper-side is inputted to the first Y_UP[0..3], and the upper-and-right-side to the latter Y_UP[4..7]. If Y_UP[8] upper-and-right-side data don't available, all inputted by Y_UP[3]. An upper-and-left-side neighbouring pixel of current Y_UP_LEFT 4x4 block. Output MPR[16] A list of 16 pixels, one list per 4x4 block. Control Signals Input Intra 4x4 prediction mode. It is chosen form 9 modes. The modes are Vertical, Horizontal, DC, Diagonal_Down_Left, Diagonal_Down_Right, PredMode Vertical_Right, Horizontal_Down, Vertical_Left, and Horizontal_Up. They are numbered form 0 to 8 respectively. Output Context Information Input A list of block availability. Left side, upper side, and AVAIL[3] upper-and-left side. Output Internal Processing Algorithm Step Process 1 Read AVAIL[0..2]. 2 If AVAIL[0] != 1, go to Step 4 3 Read Y_LEFT[0..7]. 4 If AVAIL[1] != 1, go to Step 6 5 Read Y_UP[0..7]. 6 If AVAIL[2] != 1, go to Step 8 7 Read Y_UP_LEFT. 8 Read PredMode. 9 Calculate MPR[0..15] according to PredMode from Y_LEFT[0..3], Y_UP[0..7], and Y_UP_LEFT if be read and necessary. 10 Output MPR[0..15]. 11 End of processing.

F.6.15. 16x16 Intra Prediction (AVC LUMA) Name of Function Unit Algo_IntraPred_LUMA_16x16 Description This module predicts intra 16x16 block in Luma by a given prediction mode. Operation Range 16x16 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIntraPred_LUMA_16x16 (GCU IN_OUT gcu, DATA Int* IN Y_LEFT, DATA Int* IN Y_UP, DATA Int IN Y_UP_LEFT, DATA Int* OUT MPR, SIG Int IN PredMode); Input/Output Data

50 © ISO/IEC 2008 – All rights reserved Input 16 left-side neighbouring pixels of current 16x16 Y_LEFT[16] block. 16 upper-side neighbouring pixels of current 16x16 Y_UP[16] block. An upper-and-left-side neighbouring pixel of current Y_UP_LEFT 16x16 block. Output MPR[256] A list of 256 pixels, one list per 16x16 block. Control Signals Input Intra 16x16 prediction mode. It is chosen form 4 PredMode modes. The modes are Vertical, Horizontal, DC, and Plane. They are numbered form 0 to 3 respectively. Output Context Information Input A list of block availability. Left side, upper side, and AVAIL[3] upper-and-left side. Output Internal Processing Algorithm Step Process 1 Read AVAIL[0..2]. 2 If AVAIL[0] != 1, go to Step 4 3 Read Y_LEFT[0..15]. 4 If AVAIL[1] != 1, go to Step 6 5 Read Y_UP[0..15]. 6 If AVAIL[2] != 1, go to Step 8 7 Read Y_UP_LEFT. 8 Read PredMode. 9 Calculate MPR[0..255] according to PredMode from Y_LEFT[0..15], Y_UP[0..15], and Y_UP_LEFT if be read and necessary. 10 Output MPR[0..255]. 11 End of processing.

F.6.16. 8x8 Intra Prediction (AVC Chroma) Name of Function Unit Algo_IntraPred_CHROMA_8x8 Description This module predicts intra block in Chroma by a given prediction mode. Operation Range 8x8 block-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIntraPred_CHROMA (GCU IN_OUT gcu, DATA Int* IN C_LEFT, DATA Int* IN C_UP, DATA Int IN C_UP_LEFT, DATA Int* OUT MPR, SIG Int IN PredMode); Input/Output Data Input C_LEFT[8] 8 left-side neighbouring pixels of current 8x8 block. 8 upper-side neighbouring pixels of current 8x8 C_UP[8] block. An upper-and-left-side neighbouring pixel of current C_UP_LEFT 8x8 block. Output MPR[64] A list of 64 pixels, one list per 8x8 block. Control Signals Input PredMode Intra chroma prediction mode. It is chosen form 4 modes. The modes are DC, Horizontal, Vertical, and Plane. They are numbered form 0 to 3 respectively.

© ISO/IEC 2008 – All rights reserved 51 Output Context Information Input A list of block availability. Left side, upper side, and AVAIL[3] upper-and-left side. Output Internal Processing Algorithm Step Process 1 Read AVAIL[0..2]. 2 If AVAIL[0] != 1, go to Step 4 3 Read Y_LEFT[0..7]. 4 If AVAIL[1] != 1, go to Step 6 5 Read Y_UP[0..7]. 6 If AVAIL[2] != 1, go to Step 8 7 Read Y_UP_LEFT. 8 Read PredMode. 9 Calculate MPR[0..63] according to PredMode from Y_LEFT[0..7], Y_UP[0..7], and Y_UP_LEFT if be read and necessary. 10 Output MPR[0..63]. 11 End of processing.

F.6.17. Motion Vector Reconstruction with Multi-Frame Adaptive FU

Name of Function Unit Algo_MVR_MultiFrameAdaptive Description This FU finds motion vector predictor by using reference index and MV of neighbouring blocks (left, top, top right). This process is adaptively according to the partition size of the current block and availability of the neighbouring blocks. Operation Range MB-basis Applicable decoding solution 14496-10 Baseline Profile ad High Profile Function Prototype Void fuMVRMFA ( SIG Int IN mbPartIdx, SIG Int* IN PartSZ, SIG Int* IN refIdxLX, SIG Int* IN refIdxLXN, SIG Int* IN mvLX, SIG Int* OUT mvpLX); Input/Output Data Input Output Control Signals Input Output Context Information Input mbPartIdx The macroblock partition index PartSZ [0] and PartSZ [1] is the width and height of the PartSZ[2] current partition refIdxLX The reference index of the current partition refIdxLXN[0] is the reference index A (left) refIdxLXN[3] refIdxLXN[1] is the reference index B (top) refIdxLXN[2] is the reference index C (top right) mvLX[3] mvLX[0] is the reference motion vector A (left) mvLX[1] is the reference motion vector B (top) mvLX[2] is the reference motion vector C (top right)

52 © ISO/IEC 2008 – All rights reserved Output mvpLX The prediction of the motion vector Internal Processing Algorithm Step Process 1 Read the macroblock partition index from mbPartIdx. 2 Read the reference index of the current partition refIdxLX. 3 Read the macroblock width PartSZ,(MbPartWidth= PartSZ[0], MbPartHeight= PartSZ[1]) 4 Read the reference index refIdxLXA, refIdxLXB, refIdxLXC, mvLXA, mvLXB, mvLXC: refIdxLXA = refIdxLXN[0], refIdxLXB = refIdxLXN[1], refIdxLXC = refIdxLXN[2], mvLXA= mvLX[0], mvLXB = mvLX[1], mvLXC = mvLX[2] 5 If (MbPartWidth = 16 and MbPartHeight=8 and mbPartIdx=0 and refIdxLXB=refIdxLX) go to Step 9; else go to 6 6 If (MbPartWidth= 16 and MbPartHeight=8 and mbPartIdx=1 and refIdxLXA=refIdxLX) go to Step 11; else go to 7 7 If (MbPartWidth = 8 and MbPartHeight=16 and mbPartIdx=0 and refIdxLXA=refIdxLX) go to Step 13; else go to 8 8 If (MbPartWidth = 8 and MbPartHeight=16 and mbPartIdx=1 and refIdxLXC=refIdxLX) go to Step 15; else go to 17 9 mvpLX=mvLXB 10 go to Step 18 11 mvpLX=mvLXA 12 go to Step 18 13 mvpLX=mvLXA 14 go to Step 18 15 mvpLX=mvLXC 16 go to Step 18 17 Calculate the prediction of the motion vector mvpLX mvpLXx=Median(mvLXAx, mvLXBx, mvLXCx) mvpLXy=Median(mvLXAy, mvLXBy, mvLXCy) the subscript x and y stand for the x-direction and y-direction components of a motion vector. 18 End of processing

F.6.18. Address Generator for Multiple Reference Frame (LUMA)

Name of Function Unit Mgnt_FBAddrM_Luma_MxN Description This FU issues address and reference frame ID to luma frame buffer to fetch necessary data for the luma fractional sample interpolation. It also notifies the luma fractional sample interpolation FU which mode is selected via Frac. Operation Range MB-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuFBA_L ( SIG Int* IN MV, SIG Int IN refIdx, SIG Int* IN Location, SIG Int* IN PartSZ, SIG Int* OUT Frac,

© ISO/IEC 2008 – All rights reserved 53 SIG Int* OUT RA, SIG Int OUT refbuf); Input/Output Data Input Output Control Signals Input Output Context Information Input Motion vector of the current macroblock partition MV[2] (MV[0] for x-direction and MV[1] for y-direction) refIdx Reference index Location [0] and Location [1] shows the location of Location[2] the upper-left luma sample of the current partition PartSZ [0] and PartSZ [1] is the width and height of PartSZ[2] the current partition Output Luma location offsets in quarter-sample unit Frac[2] (Frac[0] for x-direction and Frac[1] for y-direction) RA [(M+5)][(N+5)] Read addresses to fetch reference pixels for interpolation (M and N is the width and height of the current luma partition) WA[M][N] Write address of the current parittion to the frame buffer (M and N is the width and height of the current luma partition) refbuf Reference frame buffer index Internal Processing Algorithm Step Process 1 Read the reference frame index from refIdx and bypass the value to select the reference frame buffer via refbuf. 2 Read the location of the upper-left luma sample in full-sample units (xAL= Location [0] and yAL = Location [1]). 3 Read the width and height of the current luma partition (partWidth = PartSZ [0] and partHeight = PartSZ [1]). 4 Read the motion vector of the current macroblock partition (mvLX[0] = MV[0] and mvLX[1] = MV[1]).

5 Calculate luma location offsets in quarter-sample unit xFracL and yFracL by xFracL = mvLX[0] & 3 yFracL = mvLX[1] & 3 Frac[0] = xFracL Frac[1] = yFracL

6 xIntL = clip(0, PictureWidth-1, xAL + (mvLX[0] >> 2) -2 + x) yIntL = clip(0, PictureHeight-1, yAL + (mvLX[1] >> 2) -2 + y) A ; C  A  where Clip(A, B,C)  B ; C  B  C ; others

RA[x][y] = yIntL* PictureWidth + xIntL with x = 0, 1, 2, …, partWidth + 4; y = 0, 1, 2,..., partHeight + 4.

7 WA[x][y] = (yAL+ y)* PictureWidth + (xAL + x) with x = 0, 1, 2, …, partWidth-1; y = 0, 1, 2,..., partHeight-1 8 End of processing

F.6.19. Address Generator for Multiple Reference Frame (CHROMA) Name of Function Unit Mgnt_FBAddrM_Chroma_MxN

54 © ISO/IEC 2008 – All rights reserved Description This FU issues address and reference frame ID to chroma frame buffer to fetch necessary data for the chroma fractional sample interpolation. It also notifies the chroma fractional sample interpolation FU which mode is selected via context information Frac. Operation Range MB-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuFBA_C ( SIG Int* IN MV, SIG Int IN refIdx, SIG Int* IN Location, SIG Int* IN PartSZ, SIG Int* OUT Frac, SIG Int* OUT RA, SIG Int OUT refbuf); Input/Output Data Input Output Control Signals Input Output Context Information Input Motion vector of the current macroblock partition MV[2] (MV[0] for x-direction and MV[1] for y-direction) refIdx Reference index Location [0] and Location [1] shows the location of Location[2] the upper-left luma PartSZ [0] and PartSZ [1] is the width and height of PartSZ[2] the current partition Output Location offsets in one eighth sample unit (Frac[0] Frac[2] for x-direction and Frac[1] for y-direction) Read addresses to fetch reference pixels for RA [(M+1)][(N+1)] interpolation (M and N is the width and height of the current chroma partition) Write address of the current parittion to the frame WA[M][N] buffer (M and N is the width and height of the current chroma partition) refbuf Reference frame index Internal Processing Algorithm Step Process 1 Read the reference frame index from refIdx and bypass the value to select the reference frame via refbuf. 2 Read the location of the upper-left luma sample in full-sample units (xAL= Location [0] and yAL = Location [1]). 3 Read the width and height of the current chroma partition (partWidthC = PartSZ [0] and partHeightC = PartSZ [1]). 4 Read the motion vector of the current block partition (mvLX[0] = MV[0] and mvLX[1] = MV[1]).

5 Calculate luma location offsets in one eighth sample unit xFracC and yFracC by xFracC = mvLX[0] & 7 yFracC = mvLX[1] & 7 Frac[0] = xFracC Frac[1] = yFracC

© ISO/IEC 2008 – All rights reserved 55 6 xIntC = clip(0, PictureWidth/2-1, xAL/2 + (mvLX[0] >> 3) + x) yIntC = clip(0, PictureHeight/2-1, yAL /2+ (mvLX[1] >> 3) + y) A ; C  A  where Clip(A, B,C)  B ; C  B  C ; others

RA[x] [y]= yIntC* PictureWidth/2 + xIntC with x = 0, 1, 2, …, partWidthC; y = 0, 1, 2,..., partHeightC

7 WA[x][y] = (yAL/2 + y)* PictureWidth/2 + (xAL/2 + x) with x = 0, 1, 2, …, partWidthC-1; y = 0, 1, 2,..., partHeightC-1 8 End of processing

F.6.20. Quarter-pel 6-tap Interpolation Filter

Name of Function Unit Algo_Interp_SeparableSixTapQuarterPeAVC Description This FU performs fractional luma sample interpolation with 6-tap FIR linear phase filter. Operation Range MB-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuISSTQP ( DATA Int* IN RD, DATA Int* OUT INTERP, SIG Int* IN PartSZ, SIG Int* IN Frac); Input/Output Data Input RD[M+5][N+5] Integer samples for luma quarter sample interpolation (M and N is the width and height of the current luma partition) Output INTERP[M][N] Luma fractional interpolation samples (M and N is the width and height of the current luma partition) Control Signals Input Output Context Information Input PartSZ [0] and PartSZ [1] is the width and height of PartSZ[2] the current luma partition Luma location offsets in quarter-sample unit Frac [2] (Frac[0] for x-direction and Frac[1] for y-direction) Output Internal Processing Algorithm Step Process 1 Read luma location offset in quarter-sample unit from Frac: xFracL = Frac[0], yFracL = Frac[1] 2 Read the width and height of the current partition (partWidth = PartSZ [0] and partHeight = PartSZ [1]). 3 Interpolation of a fractional luma sample in the current block partition: As referring to 14496-10, interpolating a sample in a fractional position requires at most 6x6 integer samples around the interpolated location:

56 © ISO/IEC 2008 – All rights reserved A aa B

C bb D

E F G a b c H I J d e f g cc dd h i j k m ee ff n p q r K L M s N P Q

R gg S

T hh U

: Integer pixel : quarter pixel

A, B, C,… U are the required integer sample values around the interpolated location. The fractional luma sample can be derived by a 6-tap filter with coefficients (1, -5, 20, 20, -5, 1).

b1 = (E - 5*F + 20*G + 20*H - 5*I + J) b = Clip (0, 256, (b1 + 16) >> 5)

s is derived in the same manner as the derivation of b.

h1= (A - 5*C + 20*G + 20*M - 5*R + T) h = Clip (0, 256, (h1 + 16)>> 5)

m is derived in the same manner as the derivation of h.

j1 = (cc - 5*dd + 20*h1 + 20*m1 - 5*ee + ff) or j1 = (aa - 5*bb + 20*b1 + 20*s1 - 5*gg + hh) Note that cc, dd, ee and ff are derived in the same manner as the derivation of h1 and aa, bb, gg and hh are derived in the same manner as derivation of b1

j = Clip (0, 256, (j1 + 512) >> 10)

A ; C  A  where Clip(A, B,C)  B ; C  B  C ; others

According to the fractional location offset, the interpolation schemes are listed in the following table:

xFracL yFracL Value of the fractional interpolation sample (INTERP [x,y] ) 0 0 G 0 1 (G + h +1 ) >>1 0 2 h 0 3 (M + h +1 ) >>1 1 0 (G + b +1 ) >>1 1 1 (b + h +1 ) >>1 1 2 (h + j +1 ) >>1 1 3 (h + s +1 ) >>1

© ISO/IEC 2008 – All rights reserved 57 2 0 b 2 1 (b + j +1 ) >>1 2 2 j 2 3 (j + s +1 ) >>1 3 0 (H + b +1 ) >>1 3 1 (b + m +1 ) >>1 3 2 (j + m +1 ) >>1 3 3 (m + s +1 ) >>1 with x = 0, 1, 2, …, partWidth-1; y = 0, 1, 2,..., partHeight-1 4 End of processing

F.6.21. Eighth-pel Bilinear Interpolation Filter

Name of Function Unit Algo_Interp_EighthPelBilinear Description This FU performs fractional chroma sample interpolation with bilinear filter. Operation Range MB-basis Applicable decoding solution 14496-10 Baseline Profile Function Prototype Void fuIEPB ( DATA Int* IN RD, DATA Int* OUT INTERP, SIG Int* IN PartSZ, SIG Int* IN Frac); Input/Output Data Input RD[M+1][N+1] Integer samples for chroma quarter sample interpolation (M and N is the width and height of the current chroma partition) Output INTERP[M][N] Chroma fractional interpolation samples (M and N is the width and height of the current chroma partition) Control Signals Input Output Context Information Input PartSZ [0] and PartSZ [1] is the width and height of PartSZ[2] the current partition Chroma location offsets in quarter-sample unit Frac [2] (Frac[0] for x-direction and Frac[1] for y-direction) Output Internal Processing Algorithm Step Process 1 Read chroma location offset in one eighth sample unit from Frac: xFracC = Frac[0], yFracC = Frac[1] 2 Read the width and height of the current partition (partWidthC = PartSZ [0] and partHeightC = PartSZ [1]). 3 Interpolation of a fractional chroma sample in the current block partition: As referring to 14496-10, interpolating a sample in a fractional position requires at most 2x2 integer samples around the interpolated location:

58 © ISO/IEC 2008 – All rights reserved A B

yFracC

xFrac 8-xFrac C a C

8-yFracC

C D

A, B, C, D are the required integer sample values around the interpolated location.

INTERP [x,y] = ((8- xFracC)*( 8- yFracC)* A + xFracC*( 8- yFracC) * B + (8- xFracC)* yFracC*C + xFracC* yFracC* D + 32) >> 6, with x = 0, 1, 2, …, partWidthC-1; y = 0, 1, 2,..., partHeightC-1 4 End of processing

F.6.22. Deblocking Filter (DF) FU Name of Function Unit Algo_DBF_AdaptiveFilter_AVC (Deblocking Filter (DF) FU) Description Operation range MB-basis Applicable decoding solution 14496-10 Baseline Profile Function prototype Void fuDF (GCU IN_OUT gcu, DATA Int* IN ry4, DATA Int* IN rc, DATA Int* OUT dy4, DATA Int* OUT dc, SIG Int* IN mb_deblocking_filter_info, SIG Int* IN mb_quant, SIG Int IN nMB, SIG Int IN mb_type); Input/Output Data Input ry4[16][4][4] rc[8][4][4] Output dy4[16][4][4] decoded image for luma blocks dc[8][4][4] decoded image for chroma blocks Control signals Input mb_deblocking_filter_info[3] mb_quant[2] nMB Output Context information Input RA, RC and RX Reconstructed Pels (before deblocking filter) of left- and top- adjacent blocks mb_available[4] Availablity of left-, top-, topright- and topleft-adjacent macroblocks mbA_quant quantizer scale of left adjacent macroblock mbB_quant quantizer scale of top adjacent macroblock

© ISO/IEC 2008 – All rights reserved 59 Output Internal processing algorithm Step process 1 If mb_deblocking_filter_info[0] == 0, go to Step 3 2 apply deblocking filter according to the specification with mb_deblocking_filter_info[1…2] 3 End of processing

60 © ISO/IEC 2008 – All rights reserved Annex A. Specification of the CAL actor language

1. A.1 Introduction

This report describes CAL, an actor language created as a part of the Ptolemy II project [1] at the UC Berkeley. It is intended primarily as a repository for technical information on the language and its implementation and contains very little introductory material. After a short motivation, we will outline the goals and the guiding principles of the language design. We will also give a short outline of the actor model, and the context that the actors written in CAL are embedded into, describing the kinds of assumptions an actor may and may not, in general, make about it.

A.1.1 Actors and actor composition

Actors. The concept of actor as an entity that is composed with other actors to form a concurrent system has a rich and varied history—some important mile-stones include[6], [9], [3], [4], [5]. A formal description of the notion of actor underlying this work can be found in chapterA.10, which is based on the work in [10] and [7]. Intuitively, an actor is a description of a computation on sequences of tokens (atomic pieces of data) that produces other sequences of tokens as a result. It has input ports for receiving its input tokens, and it produces its output tokens on its output ports. The computation performed by an actor proceeds as a sequence of atomic steps called rings. Each ring happens in some actor state, consumes a (possibly empty) prefix of each input token sequence, yields a new actor state, and produces a finite token sequence on each output port1. Several actors are usually composed into a network, a graph-like structure (often referred to as a model) in which output ports of actors are connected to input ports of the same or other actors, indicating that tokens produced at those output ports are to be sent to the corresponding input ports. Such actor networks are of course essential to the construction of complex systems, but we will not discuss this subject here, except for the following observations:  A connection between an output port and an input port can mean different things. It usually indicates that tokens produced by the former are sent to the latter, but there are a variety of ways in which this can happen: token sent to an input port may be queued in FIFO fashion, or new tokens may ‘overwrite’ older ones, or any other conceivable policy. It is important to stress that actors themselves are oblivious to these policies: from an actors point of view, its input ports serve as abstractions of (prefixes of) input sequences of tokens, while its output ports are the destinations of output sequences.  Furthermore, the connection structure between the ports of actors does not explicitly specify the order in which actors are red. This order (which may be partial, i.e. actors may fire simultaneously), whether it is constructed at runtime or whether it can be computed from the actor network, and if and how it relates to the exchange of tokens among the actors—all these issues are part of the interpretation of the actor network. The interpretation of a network of actors determines its semanticsit determines the result of the execution, as well as how this result is computed, by regulating the flow of data as well as the flow of control among the actors in the network. There are many possible ways to interpret a network of actors, and we call any specific interpretation a model of computation—the Ptolemy project focuses on exploring the issues of models of computation and their composition, cf. [11, 12]. Actor composition inside the actor model that CAL is based on has been studied in[8]. As far as the design of a language for writing actors is concerned, the above definition of an actor and its use in the context of a network of actors suggests that the language should allow to make some key aspects of an actor definition explicit. These are, among others:

1 The notion of actor and firing is based on the one presented in [10], extended by a notion of state in [7].

© ISO/IEC 2008 – All rights reserved 61  The port signature of an actor (its input ports and output ports, as well as the kind of tokens the actor expects to receive from or be able to send to them.  The code executed during a ring, including possibly alternatives whose choice depends on the presence of tokens (and possibly their values) and/or the current state of the actor.  The production and consumption of tokens during a ring, which again may be different for the alternative kinds of rings.  The modification of state depending on the previous state and any input tokens during a ring.

Actor-like systems. It is often useful to abstract a system as a structure of cooperating actors. Many such systems are dataflow-oriented, i.e. they consist of components that communicate by sending each other packets of information, and whose ability to perform computation depends on the availability of sufficient input data. Typical signal processing systems, and also many control system fall into this category.

Writing actors is hard. Writing an actor in a general-purpose programming language is of course possible, but most or all of the information that may be used to reason about its behavior is implicit in the program and can only be extracted using sophisticated analysis, if this is at all feasible. Furthermore, actors often need to be run on different platforms. For instance if actors are used in the design of an embedded system, they need to run in a modeling and simulation environment (such as Matlab or Ptolemy) as well as in the final product. Being able to use the same description of the actor functionality in both cases improves productivity and reduces the probability of errors.

A.1.2 Language design: goals and principles

Designing a programming language is an exercise in balancing a number of sometimes contradicting goals and requirements. The following were the ones that guided the design of CAL.

Ease of use. CAL is intended to be a programming language, not an intermediate format or a representation for automatically generated code. Since we want people to actually write code in it, the notation must be reasonably convenient to write, with consistent syntax rules, keywords, and structures. Because people make mistakes, it needs to be sufficiently redundant to allow effective error detection and localization, but simple and concise enough for frequent use, especially in frequently used areas.

Minimal semantic core. In spite of being a full-edged programming language, we wanted to build CAL on a very small set of semantic concepts, for a number of reasons. First of all, being able to describe a large part of the full language through reductions to a smaller language makes the definition of language semantics much easier. From a practical perspective, this simplifies compiler construction—if there is a generic procedure that transforms any pro-gram into an equivalent program in the core language, then all it takes in order to compile the full language to any given platform is a code generator for the core language. This led to the design of a number of core languages of CAL (refflecting several levels of reduction) that much of the implementation of the language is based on, and which will be described in another place.

Focus and specificity. CAL is a domain-specific language that is aimed at providing a medium for defining actors. It was very important to draw a clear line between those pieces of functionality that were deemed to be part of an actor definition and those that were not. For example, in addition to clearly actor-specific structures such as actions and input/output patterns/expressions expressions and statements were considered to be essential to defining an actor On the other hand, there are many things that CAL explicitly does not contain, such as facilities for defining new types, concepts for connecting actors, of mechanisms to aggregate actors into composites. The fact that CAL is

62 © ISO/IEC 2008 – All rights reserved explicitly agnostic about these issues makes it possible to use the language in a wide variety of contexts, which may provide very different designs in those areas.

Implementation independence and retargetability. Even though our first target for CAL actors is the Ptolemy II platform, we want the language to be retargetable, in the following two senses: First, we would like to be able to take an actor written, say, for Ptolemy II and be able to compile it to some other platform, say to some C code that runs against a different API Secondly, we would like to enable other people to embed CAL into entirely different, but still actor-like, contexts, which have different kinds of objects (and types), different libraries, different primitive data objects and operators. Her we would not necessarily try to reuse the actor libraries written for other plat-forms (although some interesting subset might still be sufficiently generic to be reusable)instead, we would reuse the CAL framework, i.e. its infrastructure such as parsers, transformers and annotators, verification and type checking code generation infrastructure etc. This is why CAL does not have a type system of its own, but relies on the environment to provide one (cf. chapter A.4 for more information). We hope that this will allow the use of CAL in a very wide range of contexts, from full-edged component models (such as JavaBeans) to very resource-constraint embedded platforms.

Making relevant design knowledge explicit and manifest. The key goal of CAL is to enable the author of an actor to express some of the information about the actor and its behavioral properties that are relevant to using the actor (e.g. the verify its appropriate use inside a model, or to generate efficient code from it), but that would be only implicit in a description of the actor in a traditional programming language such as C or Java.

A.1.3 Platform independence and compatibility

CAL is intended to be adaptable to a variety of different platforms. There are a number of ways to interpret the term platform independence, and since it is a very important aspect of the design of CAL, we will discuss our approach to this issue in this section. For example, it could mean that code written in a language can be run on a variety of platforms (which is the interpretation chosen, e.g., in the implementation of the Java programming language). One common approach to achieve code independence would be to define a virtual platform that promises to be implementable in various environments. If this platform is a runtime platform (rather than a source-code level API), this makes not only the source code, but also of the compiled target portable across platforms. This has the obvious ad-vantage of (at least potentially) making every piece of code that is ever written against the virtual platform portable to any implementation, increasing reuse and avoiding fragmentation of the user base. There are, however, downsides to this approach. First, it requires a delicate balance between including all possibly desirable features and selecting those that can be implemented on a wide variety of platforms. If the target platforms are reasonably similar, this may not be a problem, but to the extent that the targets vary, some of them may prevent the inclusion of features that would be very useful on others, resulting in a greatest common denominator design. Second, requiring code and APIs to be independent of any specific platform also makes it harder or impossible to take advantage of platform-specific features. Another interpretation of the term focuses on the language and its concepts, rather than the code written in the language. The C language is an example of this: it provides a set of basic concepts, but it leaves many details open to specific implementations, such the sizes and representations of basic data types, and of course the set of library functions used to create programs. As a result, C code itself is not portable, but relies on the presence of a specific set of libraries, and may rely on a specific representation of the data objects. Of course, techniques exist to improve code portability, such as standardization of library sets, and abstraction mechanisms that deal with different data representations. But the general problem with this approach is, of course, that code written in a language is not automatically portable. The advantage, however, is that the language as well as code written in it may be tuned to exploit the specific features of a platform.

© ISO/IEC 2008 – All rights reserved 63 The design of CAL tries to realize this latter form of portability. The reason is that we intend the language to be used on wide variety of different platforms and that we do not believe that there is a single abstraction that does justice to all of them—particularly because for some of them, performance of the generated code is a very high priority. Nonetheless, portability of source code as well as target code remains a concern. We intend to improve source code portability by defining profile for certain classes of platforms, which define things like type systems, basic function and procedure libraries and the like. But these will be based on our experiences with the language in various scenarios, and thus are a second step in the development of the language and its environment. As for target code portability, it seems more reasonable to use existing infrastructure (such as the Java VM, or the Common Language Runtime) wherever possible, rather than developing one from scratch.

RATIONALE. Throughout this appendix, selected aspects of the language design will be discussed in these boxes, presenting the rationale for the decisions made in the specific cases. These discussions are mainly of interest to language implementors and people interested in the language design. Users only interested in the mechanics of the language may safely ignore them.

IMPLEMENTATION NOTE. Similarly, discussions of implementation techniques, or aspects of the language that require particular consideration when implementing the language on some platform are set in these boxes.

2. A.2 Introductory remarks

Throughout this part, we will present fragments of CAL syntax along with (informal) descriptions of what these are supposed to mean. In order to avoid ambiguity, we will now introduce a few conventions as well as the fundamental syntactic elements (lexical tokens) of the CAL language.

3. A.2.1 Lexical tokens

CAL has the following kinds of lexical tokens:

Keywords. Keywords are special strings that are part of the language syntax and are consequently not available as identifiers. See Annex C for a list of keywords in CAL.

Identifiers. Identifiers are any sequence of alphabetic characters of either case digits, the underscore character and the dollar sign that is not a keyword. Sequences of characters that are not legal identifiers may be turned into identifiers by delimiting them with backslash characters. Identifiers containing the $-sign are reserved identifiers. They are intended to be used by tools that generate CAL program code and need to produce unique names which do not conflict with names chosen by users of the language Consequently, users are discouraged from introducing identifiers that contain the $-sign.

Operators. Operators are written as any string of characters !, @, #, $, %, ^, &, *, /, +, -, =, <, >, ?, ~, --. In addition to these strings, a few keywords are used as operators. The set of operators in CAL is extensible, different platform may augment the set of operators. The language itself defines only a small se of operators—see Annex Dfor a list of predefined operators.

64 © ISO/IEC 2008 – All rights reserved Delimiters. These are used to indicate the beginning or end of syntactical elements in CAL. The following characters are used as delimiters: (, ), {, }, [,], :.

Comments. Comments are Java-style, i.e. single-line comments starting with “//” and multi-line comments delimited by “/*” and “*/”.

Numeric literals. CAL provides two kinds of numeric literals: those representing an integral number and those representing a decimal fraction. Their syntax is as follows2: I n t e g e r  D e c i m a l L i t e r a l | H e x a d e c i m a l L i t e r a l | O c t a l L i t e r a l R e a l  D e c i m a l D i g i t { D e c i m a l D i g i t } ' . ' { D e c i m a l D i g i t } [ E x p o n e n t ] | ' . ' D e c i m a l D i g i t { D e c i m a l D i g i t } [ E x p o n e n t ] | D e c i m a l D i g i t { D e c i m a l D i g i t } E x p o n e n t D e c i m a l L i t e r a l  N o n Z e r o D e c i m a l D i g i t { D e c i m a l D i g i t } H e x a d e c i m a l L i t e r a l  ' 0 ' ( ' x ' | ' X ' ) H e x a d e c i m a l D i g i t { H e x a d e c i m a l D i g i t } O c t a l L i t e r a l  ' 0 ' { O c t a l D i g i t } E x p o n e n t  ( ' e ' | ' E ' ) [ ' + ' | ' - ' ] D e c i m a l D i g i t { D e c i m a l D i g i t } N o n Z e r o D e c i m a l D i g i t  ' 1 ' | ' 2 ' | ' 3 ' | ' 4 ' | ' 5 ' | ' 6 ' | ' 7 ' | ' 8 ' | ' 9 ' D e c i m a l D i g i t  ' 0 ' | N o n Z e r o D e c i m a l D i g i t O c t a l D i g i t  ' 0 ' | ' 1 ' | ' 2 ' | ' 3 ' | ' 4 ' | ' 5 ' | ' 6 ' | ' 7 ' | ' 8 ' H e x a d e c i m a l D i g i t  D e c i m a l D i g i t | ' a ' | ' b ' | ' c ' | ' d ' | ' e ' | ' f ' | ' A ' | ' B ' | ' C ' | ' D ' | ' E ' | ' F ' RATIONALE. The reason for allowing identifiers to essentially be any sequence of characters (by providing an  escaped identifier syntax) is that CAL is intended to interoperate with as many other languages as possible, and therefore cannot assume any particular identifier syntax. We expect that most application will be using C/C++/Java-style host environments, and thus the lexical conventions of CAL are very similar to those found in these languages. But we did not want to exclude other environments just by a too restrictive choice of the identifier space.

A.2.2 Typographic conventions

In syntax rules, keywords are shown in boldface, while all other literal symbols are enclosed in single quotes. In examples, CAL code is represented monospaced. Semantical entities, such as types, are set italic.

A.2.3 Conventions

We use a form of BNF to describe the syntax rules. Literal elements are put in quotes (in the case of symbols and delimiters), or set in boldface (in the case of keywords). An optional occurrence of a sequence of symbols A is written as [ A ] , while any number of consecutive occurrences (including none) are written as { A } . The alternative occurrence of either A or B is expressed as A | B . We often use plural forms of non-terminal symbols without introducing them explicitly. These are supposed to stand for a comma-separated sequence of at least on instance of the non-terminal. E.g., if   

2 In contrast to all other grammar rules in this report, the following rules do not allow whitespaces between tokens.

© ISO/IEC 2008 – All rights reserved 65 A is the non-terminal, we might use As in some production, and we implicitly assume the following definition: As  A { ',' A }.

In the examples we will give in this report, we will assume the usual interpretation of expression literals and mathematical operators, even though strictly speaking these are not part of the language  and depend on the environment. A specific implementation of CAL may not have these operators, or interpret them or the literals in a different manner.

A.2.4 Notational idioms

Like most programming languages, CAL involves a fair number of syntactical constructs that need to be learned and understood by its users in order to use the language productively. The effort involved in gaining familiarity with the language can be a considerable impediment to its adoption, so it makes sense to employ general guidelines for designing the syntax of constructs, which allow users to make guesses about the syntax if they are unsure about the details of a specific language construction. We call these guidelines, which define the style of a language, its notational idioms. The following is a list of notational idioms guiding the design of CAL’s language syntax. Not all of them may hold true all the time, but we tried to conform to them whenever reasonable.

Keyword constructs. Many constructs in CAL are delimited by keywords rather than more symbolic delimiters—such constructs are called keyword constructs. Examples of these would be lambda-expressions (see section A.6.9.1). Other constructs are delimited by symbols (e.g. comprehensions, see section A.6.10.1), or are at least partially lacking delimiters (such as assignments, which begin with a variable name, see section A.7.1).

Alternative end markers. Every keyword construct ends with either the key-word end or the keyword that consists of appending the opening keyword to end. For instance, a lambda-expression can either be written lambda (x) : x * x end or alternatively lambda (x) : x * x endlambda Expression head/body separator. Composite expressions often consist of the opening keyword, a head, a separator, the body, and the closing end marker keyword. In such expressions, the separator is usually the ‘:’-character, as the following examples:

let x = f(k) : g(x, x) end lambda (x) : x + x end

Statement head/body separator. Many statements have a similar structure as the one for expressions. For statements, the keywords do or begin are used as a separator:

while n > 0 do k := f(k); n := n - 1; end procedure p (x) begin println("Result: " + x.toString()); end

A.3 Structure of actor descriptions

Each actor description defines a named kind of actor. Actors may refer to entities defined in the implementation context, which provides a hierarchical namespace for these entities, see section A.3.1 for details. Actor descriptions may use import declarations to use parts of this namespace or the objects defined in it as part of their global environment.

66 © ISO/IEC 2008 – All rights reserved Actors are the largest lexical units of specification and translation. The basic structure of an actor is this:

A c t o r  [ I m p o r t s ] a c t o r I D [ ' [ ' T y p e P a r s ' ] ' ] ' ( ' A c t o r P a r s ' ) ' I O S i g [ T i m e C l a u s e ] ' : ' { V a r D e c l | A c t i o n | I n i t i a l i z a t i o n A c t i o n | P r i o r i t y B l o c k } [ A c t i o n S c h e d u l e ] { V a r D e c l | A c t i o n | I n i t i a l i z a t i o n | P r i o r i t y B l o c k } ( e n d | e n d a c t o r ) T y p e P a r  I D [ ' < ' T y p e ] A c t o r P a r  [ T y p e ] I D [ ' = ' E x p r e s s i o n ] I O S i g  [ P o r t D e c l s ] ' = = > ' [ P o r t D e c l s ] P o r t D e c l  [ m u l t i ] [ T y p e ] I D T i m e C l a u s e  t i m e T y p e

The header of an actor contains optional type parameters and actor parameters, and its port signature. This is followed by the body of the actor, containing a sequence of state variable declarations  (section A.5.1), actions (chapter A.8), initialization actions (section A.8.5), priority blocks (section A.9.3), and at most one action schedule (section A.9.2). Type parameters are variable symbols that are bound to types when the actor is instantiated. They can be used like any other type inside the actor definition. Each type parameter may be optionally bounded, i.e. they may be associated with some type. In this case, the actual type that this parameter is instantiated to is required to be a subtype of the bound (including the bound itself). By contrast, actor parameters are values, i.e. concrete objects of a certain type (although, of course, this type may be determined by a type parameter). They are bound to identifiers which are visible throughout the actor definition. Conceptually, these are non-assignable and immutable, i.e. they may not be assigned to by an actor.

IMPLEMENTATION NOTE. A specific implementation such as the one in Ptolemy might change these parameters, for example in response to user interaction during design. For this to make sense in CAL, the implementation has to ensure the consistency of the actor state with the new parameter values, which it usually does by reinitializing the actor whenever one of its parameters is assigned a new value.

The port signature of an actor specifies the input ports and output ports including their names, whether the port is a multiport or a single port, and the type of the tokens communicated via the port. While single ports represent exactly one sequence of input or output tokens, multiports are comprised of any number of those sequences (called channels, including zero.

A.3.1 Namespaces and imports

An actor description may contain free variables, i.e. references to variables not defined inside the actor. Often, these are functions or procedures, but also types which are predefined as part of the respective implementation context. The collection of all globally visible variable bindings is called the global environment of an actor. However, implementation contexts may be very rich, providing a large number of functions, procedures, and types for actor writers to use. In such cases it would be inappropriate to define all of these as global environment—it would lead to a very large number of variable names, only a very small part of which would actually be used by each actor definition. For this reason, implementation

© ISO/IEC 2008 – All rights reserved 67 contexts may use a hierarchical namespace for naming these entities, where each entity is denoted by a sequence of identifiers separated by dots (a so-called qualified identifier). Actor specifications may use them as part of their glob environment by importing them into it. Effectively, one can think of import declarations as constructing the global environment of an actor description, starting with the default global environment, and adding bindings to it. The qualified identifiers that denote each entity in the hierarchical namespace have two parts: the (possibly empty) sequence of identifiers up to and excluding the last, and the last identifier. The first part is called the subnamespace of package, while the second is called the local name. For example, in the qualified identifiers X.Y.Z, XYZ, and java.util.HashMap, the subnamespaces are X.Y, λ, and java.util, respectively3, while the corresponding local names are Z, XYZ, and HashMap. An import declaration can either make a single entity available as the value of a global variable, or the group of all entities inside the same subnamespace.

I m p o r t  S i n g l e I m p o r t | G r o u p I m p o r t ' ; ' S i n g l e I m p o r t  i m p o r t Q u a l I D [ ' = ' I D ] G r o u p I m p o r t  i m p o r t a l l Q u a l I D Q u a l I D  I D { . I D }

For a single import declaration, the qualified identifier denotes the entity to be imported into the global environment. If the optional identifier following it after an ‘=’ is omitted, the entity denoted  by the qualified identifier is imported under its local name. For instance, the import declaration import A.B.C; imports the entity denoted by A.B.C under the name C into the global environment. If an identifier is specified, it will be the name of the specified entity: import A.B.C = D; imports the same entity under the name D. Group import declarations import entire groups of entities. In this case, the qualified identifier specifies the subnamespace, and all entities in that subname pace are imported under their local names. For example, import all A.B; imports A.B.C as C and A.B.E as E, if these are the two entities in that subnamespace.

A.3.2 Time

CAL supports an abstract notion of time as a way to relate the duration various actor rings to each other, and thus potentially control or constrain the concurrency in a model that consists of several actors. It is essential to the division of responsibility between an actor and its environment that the actor itself does not interpret this time information, as it impacts the coordination of more than one actor, rather than the execution of an isolated actor. For this reason, time is purely declarative in CAL, and the language itself does not attach any meaning to it whatsoever, other than it being a property of actor transitions. In section A.10.2 the time system of an actor is introduced as an algebraic structure that has  a set of time tags  a set of time delays  a (partial) order on the tag set  an addition operation that adds delays to tags to yield larger tags  a zero delay that is the neutral delay with respect to that addition.

3 λ denotes the empty sequence of identifiers

68 © ISO/IEC 2008 – All rights reserved The actor specification, however, only contains the delays. For this reason the optional time-tag in the actor header is only followed by one type, the type of the delay values in action descriptions (see section A.8.3). This type must have the following properties:  There must be a partial order that is compatible with the addition operation used to add the

time delay to tags. More precisely, for any tag t , and any two delays d 1 , d 2 the following must hold:

d1  d2  (t  d1) (t  d2)  There must be a zero delay, say z , which defines the set of valid delays of the specified type    as follows. If d is of the delay type, it is valid iff z  d .  

The time-clause in the actor head functions as a type declaration for the delays. Its presence does not  imply that any or all actions have non-zero delays, and neither does its absence imply that all actions  have zero delays—unless a platform requires type information to be added, in which case an actor that contained delay-clauses in actions but no time-clause would not typecheck and hence not be well-formed. See section A.8.3 on how to specify time delays in actions.

4. A.4 Data types

CAL is optionally typed, i.e. it allows programmers to give each newly introduced identifier a type (see section A.5.1 for more details on declaring variables), but it does not require it, in which case the identifier is said to be untyped In general, the type system is considered part of the external environment that we try to keep CAL actor specifications orthogonal to. In this chapter we will therefore discuss primarily the syntax provided for writing types, leaving the concrete interpretation to the description of CAL implementations on individual platforms. However, CAL does assume a few basic types (some of which are parametric), viz. those that are used as part of some language constructions. There are also some framework rules on how to type checking/inference is to be conducted, which will be discussed in section A.4.6

A.4.1 Objects, variables and types

In general, there are really two kinds of types—the types with which variables are declared (variable types)4, and the types of runtime objects (object types). In most languages, these are either the same sets, or there is a significant overlap However, even in common languages, these sets are not identical: in Java, e.g. abstract classes and interfaces are only variable types, never the types of objects.

RATIONALE. Most programming languages either require explicit typing, or they do not have constructs for expressing types as part of the program source. Some languages perform type inference, i.e. they allow users to omit type declarations as long as they can infer enough information from the context to guarantee the safeness of the program. Making types in CAL optional reects our choice to allow for a wide range of embeddings and applications of the language. CAL could thus be used as a scripting language that is interpreted and completely untyped, or as a language that is used to generate C code or hardware from, and which requires complete typing. Tools are free to reject insufficiently typed programs, or to ignore type annotations if they are not helpful. Of course, tools should make a best effort to infer types whenever they need to know a type that has not been explicitly specified, so as to make actors as reusable as possible across

4 We use variable here to mean any name inside the language, including parameters etc.

© ISO/IEC 2008 – All rights reserved 69 platforms. They should also try to check types whenever feasible, in order to locate errors and to detect malformed actor definitions.

Each variable or parameter in CAL may be declared with a variable type. If it is, then this type remains the same for the variable or parameter in the entire scope of the corresponding declaration. Variable types may be related to each other by a subtype relation, p , which is a partial order on the set of all variable types. When for two variable types t , t ' we have t p t', then we say that t is a subtype of t ' , and t ' is a supertype of t . Furthermore, t may be used anywhere t ' can be used, i.e. variables of subtypes are substitutable for those of supertypes.  It is important that each object has precisely one object type. As a consequence, object types induce    an exhaustive partition on the objects, i.e. for any object type t we can uniquely determine the      “objects of type t ”.

IMPLEMENTATION NOTE.  Stating that each object has an object type does not imply that this type can be determined at run time, i.e. that there is something like run-time type information associated with each object. In many cases, particularly when efficiency is critical, the type of an object is a compile-time construct whose main use is for establishing the notion of assignability, i.e. for checking whether the result of an expression may legally be stored in a variable. In these scenarios, type information is removed from the runtime representation of data objects.

For each implementation context we assume that there is a set T V of variable types and T O of object types. They are related to each other by an assignability relation  TV  TO which has the following interpretation: for any variable type t V and object type t O , tV  tO iff an object of type t O is a legal value for a variable of type t V . The assignability relation may or may not be related to subtyping, but at a minimum it must be  compatible with subtyping in the following sense. For any two variable types t V and t ' V , and any object type t :     O  tV  t'V t'V tO  tV  tO

In other words, if an object type is assignable to a variable type, it is also assignable to any of its supertypes.  A.4.2 Type formats

Even though the CAL language itself does not specify the meaning of most types, it provides notation for expressing types, so actor writers may put this information into their actor descriptions, and an implementation context may use it. There are three basic ways to express types in CAL, and two more constructs for expressing the types of procedural and functional closures (cf. sections A.6.9.1 and A.6.9.2).

T y p e  I D | I D ' [ ' T y p e P a r s ' ] ' | I D ' ( ' [ T y p e A t t r { ' , ' T y p e A t t r } ] ' ) ' | ' [ ' [ T y p e s ] ' - - > ' T y p e ' ] ' | ' [ ' [ T y p e s ] ' - - > ' ' ] ' T y p e A t t r  I D ' : ' T y p e | I D ' = ' E x p r e s s i o n

 70 © ISO/IEC 2008 – All rights reserved A type that is just an identifier either refers to a type parameter (if it occur in the type parameters list of the actor), or it denotes the name of some other non-parametric type. Examples may be String, Integer.

The form T[T1, ..., Tn ] is intended to stand for a parametric type T taking the types T i as parameters. Such parametric type is also called a type constructor. The built-in types are of this kind, e.g. List[Integer] is a list if elements of type Integer, or Map[String, Real] is a finite map from keys of type String to values of type Real.    The next form can be thought of as a more general version of the previous one, where the type constructor has named parameters that may be bound to either types or values. For instance, the type Matrix[element: Real, width = 4, height = 5] might be the type of all matrices of real numbers of a certain size.

The type of a lambda closure is written as [T1, ..., Tn -- > T], where the T i are the argument types, and T is the return type. Similarly, the type of a procedural closure is written as [T1, ..., Tn - - >], with the T i again being the argument types.   A.4.3 Required types    Required types are the types of objects created as the result of special language constructions, usually expressions. The following are built-in types in CAL:  Null—the type containing only the value null.  Boolean—the truth values true and false.  ChannelID—the data type comprising the identifiers of channels. Most likely this will be a synonym for some other simple type, like the one of integer or natural numbers.  Collection[T]—a finite collection of elements of type T .  Seq[T]—a sequence (finite or infinite) of elements of type T .  List[T]—finite lists of elements of type T . Lists are subtypes of the corresponding sequences and also subtypes or the corresponding collections, i.e. List[T]

A.4.4 Structured objects and their types

Many data objects contain other data objects—consider e.g. lists, which contain their elements. An object that contains other objects is called composite or structured, and so is its type. CAL provides two mechanisms for identifying a subobject inside a structured object (in other words, specifying its location inside the composite): fields and indices. The mechanism used to identify locations inside a composite, as well as the fields and indices that are valid for specifying a location (see below), depend on the composite object and its type.

© ISO/IEC 2008 – All rights reserved 71 The location of a subobject can be used to either retrieve it from the composite (see sections A.6.4 and A.6.5) or, in the case of mutable objects and types, to replace the subobject with another object (see sections A.4.5, A.7.1.2, and A.7.1.3).

Fields. Fields are names for subobjects inside a composite. Any given object type provides a finite number of those field names, and using any other name to identify a location inside the composite is an error. Since the names are statically provided as part of the program text (as opposed to being computed at runtime) it is possible to check statically whether a field name is permissible for a given object. If A denotes the composite object, and f is the field name then A .f selects the subobject in that field. Another consequence of the fact that field are provided explicitly is that different fields may be associated with different subobject types, i.e. they may be heterogeneous.   Indices. Indexing into a composite object is a different mechanism for identifying a location inside it. Indices are a number of objects that can be computed at runtime, and which can be though of as the coordinates of the location inside of the composite. Which and how many indices are valid for a

given object is determined by the object. If A denotes the composite object, and E1, ..., E n are n valid indices, then

A[E1, ..., En ] selects the subobject at the specified location. Since indices are computed a runtime, all indexed   locations inside a composite object must be assumed to be of the same static type, i.e. they are homogeneous.  Example 1. A Map[K, V] from a key type K to a value type V accepts as indices objects of type K, and indexing is simply the application of the map. Say the map map {”abc”->15, ”def”->7} (of type Map[String, Integer]) can be indexed by a string as follows: map {”abc”->15, ”def”->7}[”abc”] resulting in the value 15.

The following CAL types are required to support indexing:  Seq[T]—index type is the natural numbers, starting from 0.  Map[K, V]—index type is K.

Abstractly, indexing is the invocation of a special function, the indexer of a given datatype. The type of the resulting expression may depend on the number and types of the indices, and of course also on the indexed object.

A.4.5 Mutable objects and their types

Some structured types allow the modification or mutation of an object, i.e. changing the object at some location inside it. Such a type is called mutable, and so is an object of that type. Mutating objects without any restrictions would be a technique that would render a program essentially unanalyzable in the general case. For this reason, CAL imposes a number of constraints on the ability to use this feature, which will be discussed in the context of mutable variables in sections A.7.1.2 and A.7.1.3. However, CAL makes no guarantee that the various locations of an object are independent, i.e. that all other locations remain unaffected by an assignment A structured mutable type whose locations are independent is called free, its locations are called orthogonal—all predefined types in CAL are of this kind if they support mutation.

72 © ISO/IEC 2008 – All rights reserved A.4.6 Type framework

Section A.4.1 introduced two relations between types: the subtyping relation  between variable types, and the assignability relation  between variables types and object types. It also required that the two relations be consistent with each other in the sense that assignability to a variable type would imply assignability to any of its supertypes.  This section introduces some simple properties that any type system provided by an implementation context must have. These properties, together with the basic minimal type system presented in section A.4.3, constitute the CAL type framework.

Subtype ordering. The subtyping relation  on the variable types is a partial order, i.e. for any

t1, t2, t3  TV the following holds: 1. Reflexivity: t1  t1 t  t  t  t  t  t 2. Anti-symmetry: 1 2 2 1 1 2 3. Transitivity: t  t  t  t  t  t  1 2 2 3 1 3  Finite cover. For any variable type t , there is a unique finite, and possibly empty, set t of  U variable types which has the following properties: 1 .  t' Ut :t' t  t' t   2 .  t',t'' Ut : t' t'' 3 .  t' {s|s > t, s t}:t'' U+t :t' t''  The first condition ensures that the elements of U t are all proper superclasses of t . The second condition requires the set to be minimal. The third condition requires it to be exhaustive: all proper supertypes of t are supertypes of at least one element in U t .   Least upper bound. For any two variable types t1, t 2  TV there is a unique finite, and possibly t t empty, set of variable types 1 2 which has the following properties: 1. t  t1 t2 :t  t1 t  t2 2. t',t'' t  t :t' t'' 1 2  3. t' {t|t t,t t }\t  t :t'' t  t :t' t'' 1 2 1 2 1 2  Again, the first condition ensures that all types in the set are indeed upper bounds of the two types in  question. The second condition requires them to be minimal. The third condition requires the set to  be exhaustive: all supertypes of t 1 and t 2 that are not in that set are supertypes of a type that is.

5. A.5 Variables   Variables are placeholders for other values. They are said to be bound to the value that they stand for. The association between a variable and its value is called a binding. CAL distinguishes between different kinds of bindings, depending on whether they can be assigned to (assignable variable binding), and whether the object they refer to may be mutated (mutable variable binding—cf. sections A.4.5 and A.7.1.3). This chapter first explains how variables are declared inside CAL source code. It then proceeds to discuss the scoping rules of the language, which govern the visibility of variables and also constrain the kinds of declarations that are legal in CAL.

© ISO/IEC 2008 – All rights reserved 73 A.5.1 Variable declarations

Each variable (with the exception of those predefined by the platform) need to be explicitly introduced before it can be used—it needs to be declared or imported (section A.3.1). A declaration determines the kind of binding associated with the variable it declares, and potentially also its (variable) type. There are the following kinds of variable declarations:  explicit variable declarations (section A.5.1.1),  actor parameters (chapter A.3),  input patterns (section A.8.1),  parameters of a procedural or functional closure (section A.6.9).

Variables declared as actor parameters, in input patterns, or as parameters of a procedural or functional closure are neither assignable nor mutable. The properties of a variable introduced by an explicit variable declaration depend on the form of that declaration.

A.5.1.1 Explicit variable declarations

Syntactically, an explicit variable declaration5 looks as follows:

VarDecl  [mutable] [Type] ID [(' =' | ':=') Expression] | FunDecl | ProcDecl

We will discuss function and procedure declarations (FunDecl and ProcDecl) in section A.6.9.3. An explicit variable declaration can take one of the following forms, where T is a type, v an  identifier that is the variable name, and E an expression of type T:  T v—declares an assignable, non-mutable variable of type T with the default value for that type as its initial value. It is an error for the type not to have a default value.  T v := E—declares an assignable, non-mutable variable of type T with the value of E as its initial value.  mutable T v := E—declares an assignable and mutable variable of type T with the value of E as its initial value.  mutable T v = E—declares an non-assignable and mutable variable of type T with the value of E as its initial value.  T v = E—declares a non-assignable, non-mutable variable of type T with the value of E as its initial value.

Variables declared in any of the first four ways are called stateful variables because they or the object they are containing may be changed by the execution of a statement. Variables declared in the last way are referred to as stateless variables. Explicit variable declarations may occur in the following places:  actor state variables  the var block of a surrounding lexical context  variables introduced by a let-block

While actor state variables and variables introduced in a var-block can be state variables as well as non-state variables, a let-block may only introduce non-state variables.

5 These declarations are called “explicit” to distinguish them from more “implicit” variable declarations that occur, e.g., in generators or input patterns.

74 © ISO/IEC 2008 – All rights reserved A.5.2 Variable scoping

The scope of a variable is the lexical construct that introduces it—all expressions and assignments using its name inside this construct will refer to that variable binding, unless they occur inside some other construct that introduces a variable of the same name, in which case the inner variable shadows the outer one. In particular, this includes the initialization expressions that are used to compute the initial values of the variables themselves. Consider e.g. the following group of variable declarations inside the same construct, i.e. with the same scope:

n = 1 + k, k = 6, m = k * n

This set of declarations (of, in this case, non-mutable, non-assignable variables, although this does not have a bearing on the rules for initialization expression dependency) would lead to k being set to 6, n to 7, and m to 42. Initialization expressions may not depend on each other in a circular manner —e.g., the following list of variable declarations would not be well-formed:

n = 1 + k, k = m – 36, m = k * n

More precisely, a variable may not be in its own dependency set. Intuitively, this set contains all variables that need to be known in order to compute the initialization expression. These are usually the free variables of the expression itself, plus any free variables used to compute them and so on— e.g., in the last example, k depended on m, because m is free in m - 36, and since m in turn depends on k and n, and n on k, the dependency set of k is {m,k,n} which does contain k itself and is therefore an error. This would lead to defining the dependency set as the transitive closure of the free variable dependency relation—which would be a much too strong criterion. Consider e.g. the following declaration:

f = lambda (n) : if n = 0 then 1 else n * f(n - 1) end end

Here, f occurs free in the initialization expression of f, which is clearly a circular dependency. Nevertheless, the above definition simply describes a recursive function, and should thus be admissible.

The reason why f may occur free in its own definition without causing a problem is that it occurs inside a closure—the value of f need not be known in order to construct the closure, as long as it becomes known before we use it—i.e. before we actually apply the closure to some argument.

We will now define the dependency sets I v and D v of a variable V among a set of variables that are defined simultaneously in the same scope.

Definition 1 ( I , D —the dependency sets of a variable v ). Consider a set V of variables v which v v  are defined simultaneously, i.e. the initial value of each of these variables defined by an expression

E v which is in the scope of all the variables in V . Let us call the set of free variables of E v in V F v , i.e. we only consider free variables in V . Then the immediate dependency set I of each variable v is   v  defined  as follows        © ISO/IEC 2008 – All rights reserved 75  f o r E v a c l o s u r e I v   D v o th e r w is e

This definition is based on the dependency set D v , which is defined as the smallest set such that the following holds:  ( a ) F  D  v v ( b ) D  D U x v x D v

Intuitively, D v contains those variables in V on which the object bound to v directly or indirectly depends. I v is the set of variables whose values need to be known when the object computed by E v  6 is created—for most expressions, it is the same as D v , but for closures (procedural or functional) this set is empty, because there is no need to evaluate the body of the closure in order to construct the    closure object7. Now we capture the notion of well-formedness of a set of simultaneously defined variables V as a condition on the dependency sets as follows:

Definition 2 (Well-formed declaration set). A set of simultaneously declared variables (a  declaration set) V is well-formed iff for all v  V

v  I v Note that, as in the example above, a variable may occur free in its own initialization expression, but still not be in its own immediate dependency set, as this only includes those variables whose value   must be known in order to compute the value of the declared variable. This notion of well-formedness is useful because of the following property:

Corollary 1 (No mutual dependencies in well-formed variable sets). Given a well-formed variable set V , for any two variables v1 ,v 2  V , we have the following property: (v  I v  I ) 1 v2 2 v1 That is, no two variables ever mutually immediately depend on each other. The proof of this property is trivial, by contradiction and induction over the definition of the   dependency set (showing that mutual dependency would entail self-dependency, and thus contradict well-formedness).  This allows us to construct the following relation over a set of variables:

Definition 3 (Dependency relation). Given a set of variables V defined in the same scope, we define a relation p on V as follows: v v  v  I 1 p 2 1 v2 In other words, a variable is smaller than another according to this relation iff it occurs in its dependency set, i.e. iff it has to be defined before the other can be defined. The well-formedness of the declaration set implies that this relation is a non-reflexive partial order, since variables may not mutually depend on each other.  This order allows us to execute variable declarations in such a way that immediate dependencies are always evaluated before the dependent variable is initialized.

Example 3. Consider the following variable definitions:

6 Strictly speaking, this rule provides a conservative approximation to the set of immediate dependencies.

7 Here we use the fact that closures can be constructed withour the values of their free variables, which is clearly an artifact of the way we envision closures to be realized, but it is a useful one.

76 © ISO/IEC 2008 – All rights reserved a = f(x), f = lambda (v) : if p(v) then v else g(h(v)) end end, g = lambda (w) : b * f(w) end, b=k

Note that f and g are mutually recursive. The following lists the immediate dependencies and the free variable dependencies of each variable above8, along with their intersection with the set {a,f,g,b} which is the set V this case:

v F v F v  V I v I v  V a { f , x } { f } { f , x } { f } f { p, g, h} { g }     g { b , f } { b , f }        b { k }  { k }         Table 1            Now let us compute the dependency set D a of the variable a. We start with the set      Ia V  { f } Now we compute (I  F )V ={f ,g} a f  Then  (Ia  Ff  Fg)V  {f,g,b} Finally, we reach a fixpoint at  Da  (Ia  Ff  Fg  Fb)V  {f,g,b} D  D  D    Trivially, we compute f g b (all definitions that consists only of a closure have empty dependency sets, and so do definitions that only refer to variables which are not in V ). As a result of this analysis, we see that the variables f, g, and b may be defined in any order, but all must be  defined before a, as it depends on all of them.   Example 4. Now consider the following slightly changed variable definitions, with an additional dependency added to b:

a = f(x), f = lambda (v) : if p(v) then v else g(h(v)) end end, g = lambda (w) : b * f(w) end, b = a* k

Again, the following table lists the dependency sets:

v F v F v  V I v I v  V a { f , x } { f } { f , x } { f } f { p, g, h} { g }   g { b , f } { b , f }        b { a , k } { a } { a , k } { a }        Table 2            8 We are disregarding here the implicit variable references that will be introduced when the operators are resolved to

function calls—strictly speaking, they would become part of F v but as they are always referring to global variables, and

would thus disappear from F v  V anyway, we do not bother with them in the example.

 © ISO/IEC 2008 – All rights reserved 77  Now, computing D a proceeds as follows:

I a  V  { f }

( I a  F f )  V  { f , g }

 ( I a  F f  F g )  V  { f , g , b }

( I a  F f  F g  F b )  V  { f , g , b , a }

D a  ( I a  F f  F g  F b  F a )  V  { f , g , b , a }

Obviously, in this case a  D a , thus the set of variable definitions is not well-formed.

 6. A.6 Expressions  Expressions evaluate to a value and are side-effect-free, i.e. they do not change the state of the actor or assign or modify any other variable. Thus, the meaning of an expression can be described by the value it is evaluating to. If the computation of an expression terminates, it results in a value, and that value has an object type. The precise value depends on the environment surrounding the expression when it is evaluated. In general, the objects computed by an expression in different environments may belong to different object types. However, Using the properties of the type system outlined in section A.4.6, we can compute a set of least upper variable type bounds for each expression, knowing the declared types of each of its free identifiers and the types of the literals occurring in it. The object types of all objects ever computed by the expression must be assignable to all of the variable types in that set. The following is an overview of the kinds of expressions and expression syntaxes provided in CAL.

E x p r e s s io n  P r im a r y E x p r e s s io n { O p e r a to r P r im a r y E x p r e s s io n } P r im a r y E x p r e s s io n  [ O p e r a to r ] S in g le E x p r e s s io n { ' ( ' [ E x p r e s s io n ] ' ) ' | '[ ' E x p r e s s io n ' ] ' | ' .' I D }

S i n g l e E x p r e s s i o n  [ o l d ] I D | E x p r e s s i o n L i t e r a l  | ' ( ' E x p r e s s i o n s ' ) ' | I f E x p r e s s i o n | L a m b d a E x p r e s s i o n | P r o c E x p r e s s i o n | L e t E x p r e s s i o n | L i s t C o m p r e h e n s i o n | S e t C o m p r e h e n s i o n | M a p C o m p r e h e n s i o n

We will now discuss the individual kinds of expressions in more detail.  A.6.1 Literals

Expression literals are constants of various types in the language. They look as follows:

E x p r e s s io n L ite r a l  I n te g e r L ite r a l | D e c im a lF r a c tio n L ite r a l | S tr in g L ite r a l | tr u e | fa ls e | n u ll

 78 © ISO/IEC 2008 – All rights reserved The type of true and false is Boolean, the type of null is Null. The exact types of the other literals are determined by the environment, but the intuition behind them is that they represent the integers, the real numbers, and character strings, respectively.

RATIONALE. We are not committing to particular types for the numeric literals because we want to keep the requirements on the type system as weak as possible, to allow for a wide range of implementations. For example, some implementations may have only integer numbers up to a specific word size (say, 32 o 64 bits), while others have variable-sized integers. Such an implementation may want to assign a specific type to an integer literal depending on it numeric size—e.g., it may make it an object of type Integer32 if it fits into 32 bits, and of type Integer if it is larger than that.

A.6.2 Variable references

The expression used to refer to the value bound to a variable at any given point during the execution is simply the name of the variable itself, i.e. an identifier.

A.6.2.1 Old variable references

The code inside an action may refer to the value of a variable at the beginning of the action by prefixing the variable name with the keyword old, as in the following example:

Example 5. …

sum := 0;

action [a] ==> [ old sum / sum] do sum := sum + a; end

The output expression refers to both the value of sum at the beginning of the firing as well as its value at the end of the ring. This code is equivalent to the following:

Integer sum := 0;

action [a] ==> [ oldSum / sum] var oldSum = sum do sum := sum + a; end

© ISO/IEC 2008 – All rights reserved 79 In other words, using old values of variables in an action introduces an implicit non-assignable, non- mutable variable (cf. section A.7.1.4 for the implications of the original variable being mutable). Closures created inside an action may also refer to old variables, and the meaning of this follows from the transformation above: They will always refer to the value of the variable at the beginning of the ring that created them. The old keyword may not be used outside of an action, or in front of a variable that is not an assignable or mutable actor state variable.

A.6.3 Function application

An expression of the form

E(E1, ..., En) is the application of a function to n parameters, possibly none. If the types of the E i are T i then the value of the E expression must be a function of type [T', ..., T'    T]  1 n where each T '  T . The static type of the application expression is the return type of the function, i.e. i i    T .  Functions come in two forms: they are either the result of evaluating a lambda-expression (cf. section A.6.9.1 ), or are provided as part of the context. There is no difference in the way they are used inside  expressions, but they may differ in the way they are evaluated, and also in the way their types are determined.

A.6.4 Field selection

A field selector expression extracts a subobject from a composite object (see A.4.4 for details on composite objects). The syntax is as follows:

FieldSelectorExpr  Expression '.' ID

The result of this expression is the subobject of the value of the expression that is contained in the field specified by the identifier.  A.6.5 Indexing

An indexing expression selects a subobject from a composite object (cf. section A.4.4 for more details). Syntactically, indexing expressions are similar to function applications, although they use square brackets for enclosing the arguments. The general format is

IndexerExpr  Expression '[' Expressions ']'

where the first expression must be of a type that supports an indexer, and the expressions between the brackets must be indices specifying a valid location for the given object. The type of an indexing  expression is determined by the indexer, which is different for each structured data type, and may die according to the number of indices and their types.

A.6.6 Operators

There are two kinds of operators in CAL: unary prefix operators and binary infix operators. A binary operator is characterized by its associativity and its precedence. In CAL, all binary operators associate to the left, while their precedence is defined by the platform, and have fixed predefined

80 © ISO/IEC 2008 – All rights reserved values for built-in operators (which are used to work on instances of built-in types, cf. appendix D.2). Unary operators always take precedence over binary operators.

Example 6. a+b+c is always (a+b)+c. #a + b is always (#a) + b. a + b * c is a + (b * c) if * has a higher precedence than +, which is usually the case.

Operators are just syntactical elements—they represent ordinary unary or binary functions, so the only special rules for operators are syntactical. In general, the set of operators is defined by the implementation context, although a small number of operators are predefined. These operators are represented by keywords, as opposed to strings of symbols, which represent all the other operators (cf. section A.2.1).

A.6.7 Conditional expressions

The simple conditional expression has the following form:

IfExpression if Expression then Expression else Expression end

The first subexpression must be of type Boolean and the value of the entire expression is the value of the second subterm if the first evaluated to true, and the value of the third subterm otherwise.  The type of the conditional expression is the most specific supertype (least upper bound) of both, the second and the third subexpression. It is undefined (i.e. an error) if this does not exist.

A.6.8 Introducing a local scope

In expressions, local variables are introduced using a let-construct. This is often useful to factor out large subexpressions that occur several times.

LetExpression let VarDecls ':' Expression ( end | endlet )

The list of local definitions defines new identifiers and binds them to values. The variables (which are non-mutable and non-assignable) are visible inside the body expression. Its type is the type of the  entire construct.

A.6.9 Closures

Closures are objects that encapsulate some program code along with the variable context (its environment) that was valid when it was created. CAL distinguishes two kinds of closures, which differ in the kind of code they encapsulate  function closures (or simply functions) contain a parametric expression,  procedural closures (or just procedures) contain a parametric list of statements.

The two kinds of closures are used in different contexts, and in different ways—the application of a functional closure to (a tuple of) arguments is an expression (cf. section A.6.3), whereas the call of a procedural closure to (a tuple of) arguments is a statement (cf. section A.7.2).

© ISO/IEC 2008 – All rights reserved 81 A.6.9.1 Lambda-expression and function closures

Function closures are the result of evaluating a lambda-expression. They represent functions that are defined by some expression which is parameterized and may also refer to variables defined in the surrounding context.

L a m b d a E x p r e s s io n  [ c o n st ] la m b d a ' ( ' [ F o r m a lP a r s ] ' ) ' [ ' - - > ' T y p e ] [ v a r V a r D e c ls ] ' :' E x p r e s s io n ( e n d | e n d la m b d a ) F o r m a lP a r  [ T y p e ] I D

Applying function closures is side-effect free, i.e. their application (to arguments) does not change the state. However, in general they may refer to stateful variables, and thus may themselves depend  on the assignment of variables in their context, and thus be affected by side effects of other constructs. The const keyword identifies those function closures for which this is not the case, i.e. which do not refer to variables whose values may change—such a function is also called an invariant function (closure). It does not change the behavior of the closure, i.e. removing it will not affect the value computed by the closure. It is intended to serve as a declaration that expresses the programmers intention, and that may be checked by a compiler. It is an error for a const lambda-closure to refer to assignable or mutable variables.

If the types of the formal parameters are T 1 to T n respectively, and the return type is T then the type of the lambda expression is

[T, ..., T T] 1 n    The type of an invariant function closure is a subtype of the corresponding function closure type, and is written as 

const [T1, ..., Tn T]

The only built-in operation defined on a function closure is its application to a tuple of arguments, cf. section A.6.3.  A.6.9.2 Proc-expressions and procedure closures

Procedure closures are somewhat similar to function closures, in that they encapsulate a piece of code together with the context in which it was defined However, in the case of procedure closures, this piece of code is a list of statements, i.e. executing a procedure closure is likely to have side effects (as opposed to the application of a function closure). Syntactically, a procedure closure looks as follows:

ProcExpression  proc '(' [ FormalPars ] ' )' [ var VarDecls ] ( do | begin ) { Statement } ( end | endproc )

If the types of the formal parameters are T 1 to T n , respectively, then the type of the proc expression is [T, ..., T ]  1 n Since block closures can produce side effects, their execution cannot be part of the evaluation of an   expression. Executing a block closure is a fundamental kind of statement, which is discussed in section A.7.2. 

82 © ISO/IEC 2008 – All rights reserved A.6.9.3 Function and procedure declarations

One very common use for closures is the definition of functions or procedures with a particular fixed name inside some scope, often the actor itself. This can be done using the standard variable declaration syntax, as follows:

timestwo = lambda(x) : 2 * x end

However, since this use is so frequent, CAL provides special syntax that looks a little more familiar and makes the definition of functions and procedures a little easier. The above could also be written like this:

function timestwo (x) : 2 * x end

The general format for these constructs is as follows:

FuncDecl  function ID '(' [ FormalPars ] ' )' [ var VarDecls ':' ] { Statement } end ProcDecl  procedure ID ' (' [ FormalPars ] ' )' [ var VarDecls ' :' ] { Statement } end The variable introduced by these declarations is non-assignable and non-mutable.

A.6.10 Comprehensions  Comprehensions are expressions which construct one of the built-in composite objects: sets, lists, or maps. There are two variants of comprehensions, those with and those without generators. We will first focus on the simple ones without generators, and then turn to the more general comprehensions with generators. The reason for this order of presentation is that the meaning of comprehensions with generators will be defined by reducing them to simple collection expressions.

Note. The Collection type is a supertype of both Set and List, but not Map. In spite of this, we will use the term collection for maps as well in this section, because the way they are constructed is very much the same as for sets and lists. To avoid conFU’sion, we will refer to sets and lists as proper collections or Collections if we want to distinguish them from maps.

A.6.10.1 Simple collection expressions

Simple collection expressions just enumerate the elements of the set or list and the mappings of the map, respectively. They are written as follows:

S im p le S e tC o m p r e h e n s io n  ' { ' [ E x p r e s s io n s ] ' } ' S im p le L is tC o m p r e h e n s io n  ' [ ' [ E x p r e s s io n s [ ' |' E x p r e s s io n ] ] ' ] ' S im p le M a p C o m p r e h e n s io n  m a p ' { ' [ M a p p in g s ] ' } '

The elements of a set are not ordered, and each element occurs inside the set at most once. If two or more element expressions evaluate to the same value, they result in only one element inside the set9.  In contrast, the elements in a list are ordered, and the same element may occur in it more than once. Also, the list syntax allows a tail to be specified, which must be a list of the same type that is appended to the list of the explicitly specified elements.

Example 7. If n is the number 10, then the simple set expression

9 For this reason, it is in general only possible to infer an upper bound on the number of elements from a simple set expression, not the precise number of elements.

© ISO/IEC 2008 – All rights reserved 83 {n, n*n, n-5, n/2} evaluates to the set {10, 100, 5}. If s represents the list [1, 2, 3] then the simple list expression [4, 5, 6 | s] is the list [4, 5, 6, 1, 2, 3].

Simple map expressions explicitly specify the mappings from keys to values. Similar to the case of sets, if two key expressions result in the same value, only one key/value mapping will be generated from them. If the corresponding value expressions are not the same, one of the values will be chosen.

Example 8. Let again n be 10. The map map {n -> 1, n*n -> 2, n-5 -> 3, n/2 -> 4} evaluates to either {10 -> 1, 100 -> 2, 5 -> 3} or to map {10 -> 1, 100 -> 2, 5 -> 4}.

A.6.10.2 Comprehensions with generators

Simple comprehension expressions only allow the construction of sets, lists, or maps of a size that is directly correlated with the size of the expression. In order to facilitate the construction of large or variable-sized collections, CAL provides generators to be used inside an expression constructing a collection. Their syntax looks as follows:

S e t C o m p r e h e n s i o n  ' { ' [ E x p r e s s i o n s [ ' : ' G e n e r a t o r s ] ] ' } ' L i s t C o m p r e h e n s i o n  ' [ ' [ E x p r e s s i o n s [ ' : ' G e n e r a t o r s ] [ ' | ' E x p r e s s i o n ] ] ' ] ' M a p C o m p r e n s i o n  m a p ' { ' [ M a p p i n g s [ ' : ' G e n e r a t o r s ] ] ' } ' M a p p i n g  E x p r e s s i o n ' - > ' E x p r e s s i o n G e n e r a t o r  f o r [ T y p e ] I D [ ' , ' I D s ] i n E x p r e s s i o n { ' , ' E x p r e s s i o n }

The generators, which begin with the for keyword, introduce new variables, and successively instantiate them with the elements of the proper collection after the in keyword. The expression  computing that collection may refer to the generator variables defined to the left of the generator it belongs to. If that expression is of type Collection[T] the corresponding generator variable is of type T . The optional expressions following the collection expression in a generator are called filters—they must be of type Boolean, and only variable bindings for which these expressions evaluate to true  are used to construct the collection.  Example 9. The expression {}, denotes the empty set, while {1, 2, 3} is the set of the first three natural numbers. The set {2 * a : for a in {1, 2 ,3}} contains the values 2, 4, and 6, while the set {a : for a in {1, 2, 3}, a > 1} describes (somewhat redundantly) the set containing 2 and 3. Finally, the set {a * b : for a in {1, 2, 3}, for b in {4, 5, 6}, b > 2 * a} contains the elements 4, 5, 6, 10, and 12. Writing the above as {a * b : for a in {1, 2 ,3}, b > 2 * a, for b in {4, 5, 6}} is illegal (unless b is a defined variable in the context of this expression, in which case it is merely very conFU’sing!), because the filter expression b > 2 * a occurs before the generator that introduces b.

84 © ISO/IEC 2008 – All rights reserved If the generator collection is a set rather than a list, the order in which elements are extracted from it will be unspecified. This may affect the result case of a list comprehension.

Example 10. Because lists are order-sensitive, the list [a : for a in [1, 2 ,3]] is different from the list [a : for a in [3, 2, 1]] If the collection computed in a generator is not itself a list but a set, as in [a : for a in {1, 2, 3}] then the order of the elements in the resulting list will be indeterminate.

Generator semantics. In order to precisely describe the evaluation of a comprehension that contains generators and filters, we need to introduce a few symbols first. We call the original expression E . Removing all generators from E results in the simple collection expression E ' . Now E has N  0 v , ..., v generators. The i-th generator has M i  1 variables, with names i,1 i, M i . The collection C K  0 expression of the i-th generator will be called i . Following the i-th generator are  i filter Fi,1, ..., Fi, K Z expressions, which  we call i . Let denote the corresponding  empty collection expression, i.e. {} for sets, [] for lists, and map {} for maps. The tail expression of lists will be   treated separately: we replace [S | T ] with [S]  T first, and then apply the following algorithm to the   tail-free list comprehension [ S ] .  In the following, we define the meaning of generators in comprehensions by replacing them with a number of previously described constructs, such as function closures, function application, and   conditional expressions. The key to this replacement is a function $mapadd, which we will describe  below.

E  G ( 1 )  E ' i > N G ( i )   l e t a i  C i : G V ( i , 1 ) e n d o t h e r w i s e

 G F ( i , 1 ) j  M i G V ( i , j )   $ m a p a d d ( a i , l a m b d a ( v i , j + 1 ) : G V ( i , j + 1 ) e n d ) o t h e r w i s e

 G ( i  1 ) k  K i G F ( i , k )   i f F i , k t h e n G F ( i , k  1 ) e l s e Z e n d o t h e r w i s e

The a i are distinct and fresh variable symbols, i.e. they are mutually different, and they also differ from any variable symbol occurring in the comprehension.  The $mapadd function takes two arguments, a collection and a unary function. It iterates over the elements of its first argument, and applies the function to each element. The resulting values are  added—for sets, this means it produces the union of all results, for lists the concatenation, and for maps the map-union.

RATIONALE. Of course, whether there exists a function $mapadd at all, and whether it has this name will be implementation-dependent. The reason for choosing a name with a $-sign is because users are discouraged from using the $-sign for their identifiers, so that generated identifiers and internal names can b formed without creating conflicts with names chosen by the user.

Example 11. The expression $mapadd([1, 2, 3], lambda(x) : {x, x + 2} end)

© ISO/IEC 2008 – All rights reserved 85 results in {1, 3} + {2, 4} + {3, 5}, which is {1, 2, 3, 4, 5}. The expression $mapadd([1, 2, 3], lambda(x) : [x, x + 2] end) results in [1, 3] + [2, 4] + [3, 5], which is [1, 3, 2, 4, 3, 5] The expression $mapadd([1, 2, 3], lambda(x): map{x -> x * x, x + 2 -> x} end) results in map{1 -> 1, 3 -> 1} + map{2 -> 4, 4 -> 2} + {3 -> 9, 5 -> 3}, which is either map{1 -> 1, 2 -> 4, 3 -> 1, 4 -> 2, 5 -> 3} or map{1 -> 1, 2 -> 4, 3 -> 9, 4 -> 2, 5 -> 3}

IMPLEMENTATION NOTE. The fact that we define the meaning of generators inside comprehension by replacing them with other constructs is not to suggest that this is a good implementation strategy. Even though it simplifies the language implementation, it is most likely very inefficient, introducing a lot of overhead in the form of closure creation, function application, and many intermediate collection objects that get added up to compute the final result.

A.6.11 Type assertion

A type assertion expression is a way to attach type information to an arbitrary expression inside the program code. Other than type assertions, the only expressions that the user explicitly provides type information for are variables, viz. when they are declared. The types of all other expressions are inferred from those. Depending on the type system, this inference may be more or less precise, and it may or may not be sufficient to guarantee a safe execution of the program code. In such cases, it might be useful to explicitly add a type assertion into the code, either to detect that it will not hold at compile time, or to be able to check it at runtime. The syntax looks as follows:

TypeAssertionExpr '(' Expression '::' Type ')'

At compile time, if types are checked and inferred, there are three possible consequences of this construction depending on the type that the inference yielded for the embedded expression.  1. The type can be shown to be a subtype of the asserted type. In this case, the assertion is always true. 2. The type can be shown to not intersect with the asserted type. In this case, objects computed by the expression will always fail to be of the asserted type, and thus this expression is will always result in an error, which can be reported at compile time. 3. If neither can be shown, the expression may or may not produce objects of the asserted type. This results in three subcases: a. In a conservative system, an error will be signaled at compile time. b. The translator inserts a check that tests for compliance with the asserted type and causes an error if an object computed by the expression fails this check. c. The translator does nothing based on the assumption that the assertion implies that the expression will always produce the proper values.

86 © ISO/IEC 2008 – All rights reserved 7. A.7 Statements

The execution of an action (as well as actor initialization) happens as the execution of a (possibly empty) sequence of statements. The only observable effect of a statement is a change of the variable assignments in its environment. Consequently, the meaning of a statement is defined by how the variables in its scope change due to its execution. CAL provides the following kinds of statements:

S t a t e m e n t  A s s i g n m e n t S t m t | C a l l S t m t | B l o c k S t m t | I f S t m t | W h i l e S t m t | F o r e a c h S t m t | C h o o s e S t m t

A.7.1 Assignment

 Assigning a new value to a variable is the fundamental form of changing the state of an actor. The syntax is as follows:

A s s ig n m e n tS tm t  I D [ I n d e x | F ie ld R e f ] ' := ' E x p r e s s io n ' ; ' I n d e x  '[ ' [ E x p r e s s io n s ] ' ] ' F ie ld R e f  ' .' I D

An assignment without an index or a field reference is a simple assignment while one with a field reference is a field assignment, and one with an index is called an indexed assignment. Field  assignments and indexed assignments are also collectively referred to as mutations (cf. sections A.4.4 and A.4.5 for more information on structured objects and mutability).

A.7.1.1 Simple assignment

In a simple assignment, the left-hand side is a variable name. A variable by that name must be visible in this scope, and it must be assignable. The expression on the right-hand side must evaluate to an object of a value compatible with the variable (i.e. its type must be assignable to the declared type of the variable, if any—see section A.4.1). The effect of the assignment is of course that the variable value is changed to the value of the expression. The original value is thereby overwritten.

A.7.1.2 Field assignment

If a variable is of a type that has fields (see section A.4.4), and if it is mutable (see section A.4.5), assignments may also selectively assign to one of the fields rather than only to the variable itself. The syntax is as follows:

FieldAssignmentStmt  ID '.' ID ':=' Expression ';'

Here, the first identifier is the variable name, while the second is the name, which must be valid for the variable and the object that the variable refers to. 

© ISO/IEC 2008 – All rights reserved 87 A.7.1.3 Assignment with indices

If a variable is of a type that is indexed, and if it is mutable, assignments may also selectively assign to one of its indexed locations, rather than only to the variable itself. The syntax is as follows:

IndexedAssignmentStmt ID '[' Expressions ']' ':=' Expression ';'

In CAL, an indexed location inside an object is specified by a sequence of objects called indices, which are written after the identifier representing the variable, and which enclosed in square  brackets.

A.7.1.4 Assigning to and from mutable variables

In order to be able to reason about the actor state, and to facilitate program transformations, CAL is designed to avoid aliasing of stateful structures. In other words, if a structure can be mutated, no two variables may point to it at the same time. Therefore, when assigning a data structure to a mutable variable, that data structure must be cloned10. Of course, this cloning operation can occur on demand, or lazy, whenever the data structure, or a part of it, is mutated. Whichever implementation is chosen, mutations via a mutable variable must never have an effect on the value of other variables. Similarly, when assigning from a mutable variable, the structure assigned is conceptually copied, so that subsequent mutations of it are not visible via the new variable.

IMPLEMENTATION NOTE. This may pose difficult implementation issues. Consider the following example: Mutable List[Integer] a = ...; List[Integer] b := f(a[7, 1111]); Let us assume that the indexer with two arguments on Lists computes the sublist from the first index to the second, inclusive, i.e. a[7, 1111] computes a list of length 1105 elements. A naive implementation would do just that, i.e. actually create the sublist. However, if the sublist is only an intermediate value in the computation of f, this would be very wasteful, e.g. in this case:

function f(List[Integer] v)-->List[Integer] : [#v] end

Here, f returns a list of length 1 whose only element is the length of its parameter list. Alternatively, a sublist could be represented by a special sublist-object that is backed by the original list, thus avoiding the explicit construction of the new structure. However, consider an f defined like this:

function f(List[Integer] v)-->List[Integer] : v end

Now, f returns its parameter directly, with the consequence that whenever the original list, the one that is the value of the mutable variable a, is changed, so will the value of b, because its implementation is backed by the original list. Obviously, an implementation that tries to achieve good performance there-fore needs to do some bookkeeping of which parts of a data structure could be mutated, and when these get assigned to some variable, either clone them immediately, or mark them for cloning in case they should ever be

10 In fact, it must be deeply cloned, up to the point where mutations can occur.

88 © ISO/IEC 2008 – All rights reserved mutated. In either case, the behavior must be as if the mutable data structure was cloned right away.

A.7.2 Procedure call

The only predefined operation on procedures (cf. section A.6.9.2) is calling them, i.e. invoking them with a number of arguments. Calling a procedure is written as follows:

CallStmt  Expression '(' [ Expressions ] ')' ';'

The first expression must evaluate to a procedure, the other expression must be of the appropriate argument types. The result of this statement is the execution of the procedure, with its formal  parameters bound positionwise to the corresponding arguments.

A.7.3 Statement blocks (begin ... end)

Statement blocks are essentially syntactic sugar for a special case of the call statement, used to introduce a local scope and local variables. Their syntax looks like this11:

BlockStmt  begin [ var LocalVarDecls do ] { Statement } end

The form begin var do end  is equivalent to the following procedure call: proc () var do end () ;

A.7.4 If-Statement

The if-statement is the most simple control-flow construct

IfStmt if Expression then { Statement } [ else { Statement } ] end

As is to be expected, the statements following the then are executed only if the expression evaluates to true, otherwise the statements following the else are executed, if present. The expression must be  of type Boolean.

A.7.5 While-Statement

Iteration constructs are used to repeatedly execute a sequence of statements. A while-construct repeats execution of the statements as long as a condition specified by a Boolean expression is true.

WhileStmt  while Expression [ var VarDecls ] do [ Statements ] ( end | endwhile )

It is an error for the while-statement to not terminate.  A.7.6 Foreach-Statement

The foreach-construct allows to iterate over a collections, successively binding variables to the elements of the expression and executing a sequence of statements for each such binding.

11 Note that this is the one exception from the general rule that each keyword construct can end with an end marker that consists of the string end and the opening keyword. The keyword endbegin would have looked too awful.

© ISO/IEC 2008 – All rights reserved 89 F o r e a c h S tm t  F o r e a c h G e n e r a to r { ' , ' F o r e a c h G e n e r a to r } [ v a r V a r D e c ls ] d o [ S ta te m e n ts ] ( e n d | e n d fo r e a c h ) F o r e a c h G e n e r a to r  fo r e a c h [ T y p e ] I D { ' , ' I D } in E x p r e s s io n { ' , ' E x p r e s s io n } The basic structure and execution mechanics of the foreach-statement is not unlike that of the comprehensions with generators discussed in section A.6.10.2. However, where in the case of comprehensions a collection was constructed piecewise through a number of steps specified by the  generators, a foreach statement executes a sequence of statements for each complete binding of its generator variables.

Example 12. The following code fragment s := 0; foreach Integer a in {1, 2}, b in {1, 2}: s := s + a*b; end results in s containing the number 9.

Foreach-statement semantics. In order to precisely describe the execution of a foreach-statement, we need to introduce a few symbols first. We call the original foreach-statement S . The (optional) declarations we write as D and the body of the foreach-statement as B . Now S has N  1 generators. v , ..., v The i-th generator has M i  1 variables, with names i,1 i,M i . The collection expression of the i- C K  0 th generator will be called i . Following the i-th generator are i filter expressions, which we call Fi,1, ..., Fi,K . i       We can now define the meaning of the foreach-statement by replacing it with a number of previously   described constructs, such as procedure closures, procedure call, and if-statement. The key to this  replacement is a function $iterate, which we will describe below.

S  G ( 1 )  b e g i n v a r D d o B e n d i  N G ( i )   b e g i n v a r a i  C i d o G V ( i , 1 ) e n d o t h e r w i s e

 G F ( i , 1 ) j  M i G V ( i , j )   $ i t e r a t e ( a i , p r o c ( v i , j ) d o G V ( i , j  1 ) e n d o t h e r w i s e

 G ( i  1 ) k  K i G F ( i , k )   i f F i , k t h e n G F ( i , k + 1 ) e n d o t h e r w i s e

The a i are distinct and fresh variable symbols, i.e. they are mutually different, and they also differ from any variable symbol occurring inside the foreach statement.  The $iterate procedure takes two arguments, a collection and a unary procedure. It iterates over the elements of its first argument, and calls the procedure on each element.  See also the Rationale at the end of section A.6.10 for an explanation of the iterator name choice, and the Implementation Note at the end of A.6.10 for a note concerning the efficiency of generator implementations via syntactic substitution.

A.7.7 Choose-Statement

The choose-statement permits the explicit specification of (potentially nondeterministic) choice among a set of alternatives.

90 © ISO/IEC 2008 – All rights reserved C h o o s e S t m t  C h o o s e G e n e r a t o r { ' , ' C h o o s e G e n e r a t o r } [ v a r V a r D e c l s ] d o [ S t a t e m e n t s ] [ e l s e [ [ v a r L o c a l V a r D e c l s ] d o ] [ S t a t e m e n t s ] ] ( e n d | e n d c h o o s e ) C h o o s e G e n e r a t o r  c h o o s e [ T y p e ] I D [ ' , ' I D s ] i n E x p r e s s i o n [ ' , ' E x p r e s s i o n s ] The binding of values to variables happens very much like in the case of comprehension generators or the foreach-statement—see below for a precise definition of the semantics. In contrast to the foreach-statement, the choose-statement executes its body at most once, viz. for the  first combination of values that satisfy all the filters (which are part of the generators just as in the case of the foreach-statement or collection comprehensions). If no such combination can be found, and if an else-branch is present, the statements following the else-keyword are executed instead.

Choose-statement semantics. In order to precisely describe the execution of a choose-statement, we need to introduce a few symbols first. We call the original choose-statement S . The (optional) declarations we write as D , and the body of the foreach-statement as B , the else branch is E . Now v , ..., v S has N  1 generators. The i-th generator has M i  1 variables, with names i,1 i, M i . The C K  0 collection expression of the i-th generator will be called i . Following the i-th generator are i F , ..., F filter expressions, which we i,1 i, K i .   We can now define the meaning of the choose-statement by replacing it with a number of previously     described constructs, such as procedure closures, procedure call, and if-statement. The key to this   replacement is a function $try, which we will describe below. 

S  b e g i n v a r a * : f a l s e d o G ( 1 ) i f n o t a * t h e n E e n d e n d b e g i n v a r D d o a :  t r u e ; B e n d i > N G ( i )   * b e g i n v a r a i  C i d o G V ( i , 1 ) e n d o t h e r w i s e

 G F ( i , 1 ) j  M i G V ( i , j )   $ t r y ( a i , p r o c ( v i , j ) d o G V ( i , j + 1 ) e n d , l a m b d a ( ) : a * e n d ) o t h e r w i s e

 G ( i  1 ) k  K i G F ( i , k )   i f F i , k t h e n G F ( i , k  1 ) e n d o t h e r w i s e

The a i and a * are distinct and fresh variable symbols, i.e. they are mutually different, and they also differ from any variable symbol occurring inside the choose-statement.   The $try procedure takes three arguments, a collection, a unary procedure, and a nullary function. It iterates over the elements of its first argument, and for each element it does the following: It  applies the function (to no arguments, as it is nullary). If the value of that application is false, it proceeds to call the procedure (its second argument) on the element, otherwise it simply returns (disregarding any subsequent elements of the collection). See also the Rationale at the end of section A.6.10 for an explanation of the iterator name choice, and the Implementation Note at the end of section A.6.10 for a note concerning the efficiency of generator implementations via syntactic substitution.

Example 13. The choose-statement can be used to implement simple back-tracking algorithms, as in the following example: s := null; choose a in [1, 2], choose b in if a = 1 then {} else {3, 4} end do s := [a, b];

© ISO/IEC 2008 – All rights reserved 91 else s := []; end

Since for a = 1 the collection for b is the empty set, the body will never be executed for this value of a, because no choice could be made for b. However, if a = 2 two choices can be made for b, and it is unspecified which one is made. After executing this statement, s will be either [2, 3] or [2, 4]. It will never be [], because the else branch is only executed if no choice can be made.

RATIONALE. Having a construct that explicitly allows the use of non-determinism (as opposed to expressions, such as map comprehensions, that may evaluate nondeterministically, but where this behavior is most likely unwanted and potentially erroneous) allows actor authors to express internal choice points which are explicitly not under the control of the actor context (i.e. the model of computation). The responsibility for many other choices, such as which action to re, can be assumed by the model of computation. Using a choose-statement inside an action provides an explicit signal that this action may be nondeterministic, and allows the model of computation to either reject it, or deal with it accordingly.

8. A.8 Actions

An action in CAL represents a (often large or even infinite) number of transition of the actor transition system described in section A.10.3. A CAL actor description can contain any number of actions, including none. The definition of an action includes the following information:  its input tokens,  its output tokens,  the state change of the actor,  additional ring conditions,  the time delay.

In any given state, an actor may take any number of transitions (including zero), and these transitions may be represented by any number of actions in the actor description. The choice between them is ultimately made by the context of an actor (though the actor can constrain the possible choices, see chapter A.9) please see section A.8.4 for details.

The syntax of an action definition is as follows:

A c t i o n  [ A c t i o n T a g ' : ' ] a c t i o n A c t i o n H e a d [ d o S t a t e m e n t s ] ( e n d | e n d a c t i o n ) A c t i o n T a g  I D { ' . ' I D } A c t i o n H e a d  I n p u t P a t t e r n s ' = = > ' O u t p u t E x p r e s s i o n s [ g u a r d E x p r e s s i o n s ] [ v a r V a r D e c l s ] [ d e l a y E x p r e s s i o n ] Actions are optionally preceded by action tags which come in the form of qualified identifiers (i.e. sequences of identifiers separated by dots), see a section A.9.1. These tags need not be unique, i.e. the same tag may be used for more than one action. Action tags are used to refer to actions, or sets of  actions, in action schedules and action priority orders—see chapter A.9 for details.

92 © ISO/IEC 2008 – All rights reserved The head of an action contains a description of the kind of inputs this action applies to, as well as the output it produces. The body of the action is a sequence of statements that can change the state, or compute values for local variables that can be used inside the output expressions. Input patterns and output expressions are associated with ports either by position or by name. These two kinds of association cannot be mixed. So if the actors port signature is Input1, Input2 ==> ... an input pattern may look like this: [a], [b, c] (binding a to the first token coming in on Input1, and binding b and c to the first two tokens from Input2). It may also look like this: Input2: [c] but never like this: [d] Input2:[e] This mechanism is the same for input patterns and output expressions. The following sections elaborate on the structure of the input patterns and output expressions describing the input and output behavior of an action, as well as the way the action is selected from the set of all actions of an actor. In discussing the meaning of actions and their parts it is important to keep in mind that the interpretation of actions is left to the model of computation, and is not a property of the actor itself. It is therefore best to think of an action as a declarative description of how input tokens, output tokens, and state transitions are related to each other. See also section A.8.4.

A.8.1 Input patterns, and variable declarations

Input patterns, together with variable declarations and guards, perform two main functions: (1) They define the input tokens required by an action to fire, i.e. they give the basic conditions for the action to be firable which may depend on the value and number of input tokens and on the actor state, and (2) they declare a number of variables which can be used in the remainder of the action to refer to the input tokens themselves. This is their syntax:

I n p u t P a t t e r n  [ I D ' : ' ] ' [ ' I D s ' ] ' [ R e p e a t C l a u s e ] [ C h a n n e l S e l e c t o r ] C h a n n e l S e l e c t o r  a t E x p r e s s i o n | a t * E x p r e s s i o n | [ a t * ] a n y | [ a t * ] a l l R e p e a t C l a u s e  r e p e a t E x p r e s s i o n

Input patterns differ depending on whether they are written for a single port or a multiport—the former represents one channel of incoming tokens, while the latter represents an arbitrary number  (including zero) of channels. A multiport input pattern has to have a channel selector associated with it, which is a construction that serves to specify the channels that a pattern is applied to (i.e. that the token bound by the pattern are read from). no repeat-clause with repeat-clause single channel T List[T] multichannel Map[CID, T] Map[CID, List[T]] Table 3— Token variable types depending on input pattern kind and presence of repeat-clause. (T is the token type of the corresponding input port.)

The static type of the variables declared in an input pattern depends on the token type declared on the input port, but also on the kind of pattern. Table 3 shows this dependency. It distinguishes between patterns according to whether they contain a repeat-clause, and whether they take tokens from one

© ISO/IEC 2008 – All rights reserved 93 channel (single-channel patterns), or any number of channels (mutlichannel patterns). Channels are specified using channel identifiers, which are objects identifying a specific channel inside a multiport. Usually, channel identifiers are represented by some type in some platform-dependent manner. For instance, non-negative integer numbers can be used to identify channels. Table 3 refers to the type of channel identifiers as CID.

A.8.1.1 Single-port input patterns

A single-port input pattern binds a number of tokens from the input channel associated with the specified port to variables specified as part of the pattern. Because there is exactly one input channel associated with a single port, these patterns are always single-channel patterns.

SinglePortInputPattern [ ID ':' ] ChannelPattern [ RepeatExpression ]

A pattern without a repeat-expression is just a number of variable names inside square brackets. The pattern binds each of the variable names to one token, reading as many tokens as there are variable  names. The number of variable names is also referred to as the pattern length. The static type of the variables is the same as the token type of the corresponding port (Table 3Error: Reference source not found)

Example 14 (Single-port input pattern). Assume the sequence of tokens on the input channel is the natural numbers starting at 1, i.e. 1, 2, 3, 4, 5, ... The input pattern [ a, b, c] results in the following bindings: a = 1, b = 2, c = 3 If the pattern contains a repeat-clause, that expression must evaluate to a non-negative integer, say N . If the pattern length is L the number of tokens read by this input pattern and bound to the L pattern variables is N L . Since in general there may be more tokens than variables ( N times more, exactly), variables are bound to lists of tokens, each list being of length N . In the pattern, the list bound to the  k-th variable contains the tokens numbered k, L k, 2L k, ..., (N1)L k. The static type of these   variables is List[T], where T is the token type of the port (Table 3Error: Reference source not   found).   Example 15 (Single-port input pattern with repeat-clause). Assume again the natural number as input sequence. If the input pattern is [a, b, c] repeat 2 it will produce the following bindings: a = [1, 4] , b = [2, 5] , c = [3, 6]

A.8.1.2 Multiport input patterns

Multiport input patterns contain an expression that functions as a channel selector, i.e. it specifies which channels to read tokens from. There are two kind of channel selectors—those that identify precisely one channel, and those that identify any number of channels.

Single-channel channel selectors. The at-construct is used to select one channel from a multiport. The expression following the at-keyword must evaluate to a valid channel identifier for the given port. The reading of tokens and binding to the pattern variables happens in the same way as described for single-port input patterns above.

Example 16 (at channel selector). Assume four input channels ’a’,’b’,’c’,’d’ with the following input tokens: ’a’: 11, 12, 13, 14, 15, 16, 17, 18, 19

94 © ISO/IEC 2008 – All rights reserved ’b’: 21, 22, 23, 24, 25, 26, 27, 28, 29 ’c’: 31, 32, 33, 34, 35, 36, 37, 38, 39 ’d’: 41, 42, 43, 44, 45, 46, 47, 48, 49 Then the input pattern [a, b, c] repeat 2 at ’c’ would yield the bindings a = [31, 34], b = [32, 35], c = [33, 36]

Multichannel channel selectors. CAL provides three constructs for selecting tokens from more than one channel of a multiport at the same time. They are distinguished by the keyword that introduces them: at*, any, all. They differ in the way they determine the set of channels to read from as follows: 1. Following the at*-keyword is an expression that computes a (possibly empty) collection of channel identifiers. 2. The any-keyword results in those channels being read from that have a sufficient number of tokens available to be bound to the variables of the pattern—which could be none at all, in which case the set of channels would be empty. 3. The all-keyword selects all channels of the multiport.

All multichannel input patterns have in common that they are applied homogeneously, i.e. the same number of tokens is read from all selected channels (and no tokens are read from any of the other channels). The variables are bound to maps—they keys of these maps are channel identifier, while the values are depending on the absence or presence of a repeat-clause, either tokens or lists of tokens. Table 3 Error: Reference source not foundgives the static types of the pattern variables for multichannel patterns.

Example 17 (Multichannel input patterns). Assume four input channels ’a’,’b’,’c’,’d’ with the following input tokens: ’a’: 11, 12, 13, 14, 15, 16, 17, 18, 19 ’b’: 21, 22, 23, 24, 25 ’c’: 31, 32, 33, 34, 35, 36, 37, 38, 39 ’d’: 41, 42, 43 The input pattern [a, b, c] at* {’a’, ’c’} yields the bindings a = map{’a’->11, ’c’->31} b = map{’a’->12, ’c’->32} c = map{’a’->13, ’c’->33} The input pattern [a, b] all results in the bindings a = map{’a’->11, ’b’->21, ’c’->31, ’d’->41} b = map{’a’->12, ’b’->22, ’c’->32, ’d’->42} In this case, the keyword any instead of all would have produced the same result. The input pattern [a, b] repeat 2 any results in the bindings a = map{’a’->[11, 13], ’b’->[21, 23], ’c’->[31, 33]} b = map{’a’->[12, 14], ’b’->[22, 24], ’c’->[32, 34]} In this case, all instead of any would not have worked, because the channel ’d’ does not provide the required number of tokens.

© ISO/IEC 2008 – All rights reserved 95 A.8.1.3 Scoping of action variables

The scope of the variables inside the input patterns, as well as the explicitly declared variables in the var-clause of an action is the entire action—as a consequence, these variables can depend on each other. The general scoping rules from section A.5.2 need to be adapted in order to properly handle this situation. In particular, input pattern variables do not have any initialization expression that would make them depend explicitly on any other variable. However, their values clearly depend on the expressions in the repeat-clause and the channel selector (if present). For this reason, for any input pattern variable

v we define the set of free variables of its initialization expression F v to be the union of the free variables of the corresponding expressions in the repeat-clause and the channel selector. The permissible dependencies then follow from the rules in section A.5.2.  Example 18 (Action variable scope). The following action skeleton contains dependencies between input pattern variables and explicitly declared variables

[n] at c, [k], [a] repeat m * n ==> … var m = k * k, c = f(m) do ... end

These declarations are well-formed, because the variables can be evaluated in the order k, m, c, n, a. By contrast, the following action heads create circular dependencies:

[n] at f (a), [ a] repeat n ==> ... do ... end [a] repeat a[ 0] + 1 ==> ... do ... end [a] repeat n ==> ... var n = f(b), b = sum(a) do ... end

[a] at c, [b] repeat a ==> ... var c = g(b) do ... end

A.8.2 Output expressions

Output expressions are conceptually the dual notion to input pattern—they are syntactically similar, but rather than containing a list of variable names which get bound to input tokens they contain a list of expressions that computes the output tokens, the so-called token expressions.

no repeat-clause with repeat-clause single channel T Seq[T] multichannel Map[CID, T] Map[CID, Seq[T]] Table 4—Token expression types depending on output expression kind and presence of a repeat-clause. (T is the token type of the corresponding output port.)

96 © ISO/IEC 2008 – All rights reserved O u t p u t E x p r e s s i o n  [ I D ' : ' ] ' [ ' E x p r e s s i o n s ' ] ' [ R e p e a t C l a u s e ] [ C h a n n e l S e l e c t o r ] C h a n n e l S e l e c t o r  a t E x p r e s s i o n | a t * E x p r e s s i o n | [ a t * ] a n y | [ a t * ] a l l R e p e a t C l a u s e  r e p e a t E x p r e s s i o n

The repeat-clause and channel selector work not unlike in the case of input patterns, but with one crucial difference. For input patterns, these construct control the construction of a data structure that  was assembled from input tokens and then bound the pattern variables. In the case out output expressions, the values computed by the token expressions are themselves these data structures, and they are disassembled according to the repeat-clause and the channel selector, if these are present. The table 4 shows the kind of data structure a token expression has to evaluate to depending on the presence or absence of a repeat-clause, and depending on whether the output expression is a single- channel or a multichannel output expression.

Single-channel output expressions. In single-channel output expressions with-out repeat-clause, the token expressions represent the output tokens directly, and the number of output tokens produced is equal to the number of token expressions. If a single-channel output expression does have a repeat- clause, the token expressions must evaluate to sequences of tokens, and the number of tokens produced is the product of the number of token expressions and the value of the repeat-expression. In addition, the value of the repeat-expression is the minimum number of tokens each of the sequences must contain.

Example 19 (Single-channel output expressions). The output expression ... ==> [1, 2, 3] produces the output tokens 1, 2, 3. The output expression ... ==> [[1, 2, 3], [4, 5]] repeat 2 produces the output tokens 1, 2, 4, 5. The output expression ... ==> [[1, 2, 3], [4, 5]] repeat 1 at ’b’ produces the output tokens 1, 4 on channel ’b’.

Multichannel output expressions. Similarly, for multichannel output expressions without a repeat expression, the token expressions must evaluate to maps from channel identifiers to tokens. If a repeat-clause is present, the maps must map channel identifiers to sequences of tokens, the number of elements on those sequences being not smaller than the value of the repeat expression. The channel selector places some constraints on the domains of the maps as follows:  For an at* channel selector, the intersection of the domains of the maps must contain the collection of channel identifiers the selector expression evaluates to.  For an all channel selector, the intersection of all domains must contain the set of all channel identifiers currently valid for the corresponding port.  For a any channel selector, no constraints apply to the domains. The channels that output will be produced on are those in the intersection of all domains, which may be empty.

These rules are designed to make it possible to produce output homogeneously on all selected output channels—in other words, all output channels of a multiport output the same number of tokens if they output any tokens at all.

Example 20 (Multichannel output expressions). The output expression

© ISO/IEC 2008 – All rights reserved 97 ... ==> [ map{’a’->1, ’b’->2}, map{’a’->3, ’b’->4, ’c’->5}] at* {’a’, ’b’} produces the following output on the channels ’a’and ’b’: a: 1, 3 b: 2, 4 Nothing is output on channel ’c’: The output expression ... ==> [ map{’a’->[1, 2], ’b’->[3, 4]}, map{’a’->[5, 6], ’b’->[7, 8]}] repeat 2 at* {’a’, ’b’} results in the following output: a: 1, 5, 2, 6 b: 3, 7, 4, 8

A.8.3 Delays

The expression following the delay-keyword specifies the delay of the action. The actor language itself does not attach any meaning to this information, other than that it is part of the label of a state transition of an actor. See section A.10.2 for the precise meaning of time and delay, and section A.3.2 for some more information on time inside the CAL language. If the actor contains a time-clause, then the static type of each delay expression must be a subtype of the type specified in that time-clause.

A.8.4 On action selection: guards and other activation conditions

At any given point during the execution of an actor, an action may potentially fire on some input data. Whether it is activated, i.e. whether in a given situation it actually can fire, depends on whether its activation conditions have all been met. The minimal conditions are as follows: 1. According to the action schedule (see section A.9.2) this action may fire next, according to Definition 4. 2. No higher-priority action is activated (see section A.9.3) 3. There are sufficient input tokens available to bind the input pattern variables to appropriate values. 4. Given such a binding, all guard expressions (which must be Boolean expressions) evaluate to true. 5. There is sufficient room for the output tokens produced by this firing.

Depending on the context in which the actor is embedded, additional conditions for activating actions may apply. A model of computation may reject an actor as non-well-formed if it cannot statically ascertain that it will meet the relevant activation conditions whenever it needs to.

IMPLEMENTATION NOTE. The last activation condition may not be easily satisfiable in the general case, as it may be unknown exactly how many output tokens a firing will produce until it has executed and the output tokens have been computed. The important point is that firing an actor has to appear to the outside as an atomic step—the physical time it takes are not directly relevant, and the execution may even stall due to a lack of space for the output tokens, but it is imperative that the firing looks as if it occurred in one atomic transition.

A.8.5 Initialization actions

Initialization actions are executed at the beginning of an actor’s life cycle. The are very similar to regular actions, with two important differences:

98 © ISO/IEC 2008 – All rights reserved 1. Since the assumption is that at the beginning of an actor execution no input is available, initialization actions have no input patterns. They may produce output, however. 2. With the exception of initialization expressions in variable declarations, an initialization action contains the first code to be executed inside the actor. Any state invariants in the actor may not hold, and instead have to be established by the initialization action.

The syntax of initialization actions is as follows:

I n i t i a l i z a t i o n A c t i o n  [ A c t i o n T a g ' : ' ] i n i t i a l i z e I n i t i a l i z e r H e a d [ d o S t a t e m e n t s ] ( e n d | e n d i n i t i a l i z e ) I n i t i a l i z e r H e a d  ' = = > ' O u t p u t E x p r e s s i o n s [ g u a r d E x p r e s s i o n s ] [ v a r V a r D e c l s ] [ d e l a y E x p r e s s i o n ]

The activation conditions for actions apply also to initialization action—of course, since there is no input, the conditions concerning input tokens become vacuously true.  If an actor should have more than one initialization action, and if more than one is activated at the beginning of an actor execution, one of them is chose arbitrarily. The actor context can of course make than choice.

9. A.9 Action-level control structures

In CAL, an action expresses a relation between the state of an actor and input tokens, and the successor state of the actor and output tokens. In general, CAL actors may contain any number of actions, and in a given situation, any subset of those may be ready to be executed. For example, both actions of the following actor may be able to execute, if there is a token available on either input port:

Example 21 (Nondeterministic Merge). actor NDMerge () A, B ==> C: action A: [x] ==> [x] end action B: [x] ==> [x] end end

It is important to emphasize that the policy used to choose between the two actions above is not part of the actor specification. In practice, the context of the actor will make some choice, based on whatever policy it implements. This flexibility may be desired, but sometimes the actor writer may want to have more control over the choice of the action—e.g., if the Merge actor is supposed to alternate between reading its input ports, one might use actor state to realize this behavior:

Example 22 (Basic FairMerge). actor FairMerge () A, B ==> C: s := 1;

action A: [x] ==> [x] guard s = 1 do s := 2; end

action B: [x] ==> [x] guard s = 2

© ISO/IEC 2008 – All rights reserved 99 do s := 1; end end

This way of specifying action choice has two key drawbacks. First, it is very cumbersome to write and maintain, and it does not scale very well even for modest numbers of actions and states. Furthermore, this way of specifying action choice essentially obFU’scates the “real” logic behind guards, state variable and assignments, so that it becomes harder to extract the intent from the actor description, both for tools and for human readers. These are the key motivations for using action schedules, i.e. structured descriptions of possible orders in which actions may re. Before we can discus action schedules in section A.9.2, we need to take a closer look at how actions are referred to inside of them.

A.9.1 Action tags

Actions are optionally prefixed with action tags (see chapter 8), which are qualified identifiers:

A c tio n T a g  Q u a lI D Q u a lI D  I D { ' .' I D }

The same tag may be used for more than one action. In the following, we write the set of all actions

tagged by a tag t as t , and the tag of some action a as t a . The empty tag is written as , and the set  of all untagged actions is therefore . Action tags are ordered by a prefix ordering: We say that t  t', i.e. t is a prefix of t ' , if t ' starts with all the identifiers in t in the same order, followed by any number of additional identifiers,     including none. For instance, a.b.c  a.b.c.x and a.b  a.b, but a.b  a.c . We call t ' an extension of  t .     When used inside action schedules and priority orderings, a tag denotes the set of actions which are  labeled with tags that are extensions of it. For any tag t this set is called tˆ and is defined as follows:     tˆ  {a | t  t }  def a

A.9.2 Action schedules    Action schedules are structured descriptions of possible sequences in which the actions of an actor may be executed. These sequences are either specified by a finite state machine or a regular expression in the alphabet of action tags. In general, the set of possible sequences may be finite or infinite and any specific sequence may also be finite or infinite. Irrespective of whether a finite state machine is used or a regular expression an action schedule effectively describes a (regular) language L in the alphabet of action tags. This language is used to constrain the legal sequences of action firings as follows.

Definition 4 (Legal action sequence). Given a tag language L , assume a finite sequence of  actions ( a i ) i1 ..n and a sequence (b j ) j 1 ..m with m  n and a strict monotonic function f :{1..m} {1..n} such that the following holds for all j  {1..m} and i  {1..n}: 1. b j  a f ( j )  2. t b     j  1 3. t  , i  f [{1..m}]  ai    ( b ) ( a ) ( b )  In other words, the j are the subsequence in the i with non-empty tags. If j is empty, then ( a ) is a legal action sequence.  i

   100 © ISO/IEC 2008 – All rights reserved  If ( b j ) is not empty, the ( a i ) is a legal action sequence if and only if there exists a sequence of tags

( t j ) j 1 ..m such that the following holds: ˆ 1. for all j  {1..m}, b j  t j  2. there exists a w  L such that ( t j )  w .  A consequence of this definition is that untagged actions may occur at any point in the schedule—  conversely, schedules do not constrain untagged actions in any way.   We will now describe the two ways of defining a tag language: finite state machines and regular expressions.

A.9.2.1 Finite state machines schedules

A finite state machine schedule defines a number of transitions between states (and an initial state) that are each labeled with one or more action tags.

S c h e d u l e F S M  s c h e d u l e [ f s m ] I D ' : ' { S t a t e T r a n s i t i o n ' ; ' } ( e n d | e n d s c h e d u l e ) S t a t e T r a n s i t i o n  I D ' ( ' A c t i o n T a g s ' ) ' ' - - > ' I D { ' | ' ' ( ' A c t i o n T a g s ' ) ' ' - - > ' I D } A c t i o n T a g s  A c t i o n T a g | A c t i o n T a g ' , ' A c t i o n T a g s

The state before the colon is the initial state, and all states are accepting (or final states). The tag language is the set of all sequences of tags that label transitions leading from the initial state to any  other state of the finite state machine. Several transitions starting from the same state may be written as separated by the ‘—‘ character. The following illustrates the use of a finite state machine action schedule to express the FairMerge actor somewhat more concisely.

Example 23 (FairMerge, with FSM schedule). actor FairMerge1 () A, B ==> C:

InA: action A: [x] ==> [x] end InB: action B: [x] ==> [x] end

schedule fsm WaitA : WaitA (InA) --> WaitB; WaitB (InB) --> WaitA; end end

A.9.2.2 Regular expression schedules

In many cases, using an explicit finite state machine to define an action schedule is still rather verbose, and specifying the tag language through a regular expression is much more straightforward. The syntax is as follows:

© ISO/IEC 2008 – All rights reserved 101 S c h e d u l e R e g E x p  s c h e d u l e r e g e x p R e g E x p ( e n d | e n d s c h e d u l e ) R e g E x p  A c t i o n T a g | ' ( ' R e g E x p ' ) ' | ' [ ' R e g E x p ' ] ' | R e g E x p ' * ' | R e g E x p R e g E x p | R e g E x p ' | ' R e g E x p

The simplest regular expression (regexp) is simply a tag, which denotes the language consisting of just this tag. Parentheses are used to explicitly group regexps. The square brackets enclose an  optional regexp, describing the language that consists of all sequences described by the regexp, as well as the empty sequence. The Kleene operator ‘*’ means repetition, juxtaposition of two regexps denotes concatenation of sequences of the two languages, and the ‘—‘ operator describes alternative choice. The binding strength is the order given above, i.e. the regexp A B* | C* D is interpreted as (A (B*)) | ((C*) D) The FairMerge actor can be expressed with regexps as follows:

Example 24 (FairMerge, with regular expression schedule). actor FairMerge2 () A, B ==> C : InA : action A: [x] ==> [x] end InB : action B: [x] ==> [x] end schedule regexp (InA InB)* end end

RATIONALE. The reason for this conceptual redundancy is convenience—many schedules are best written as regular expressions. On the other hand, in many cases producing a regular expression can be cumbersome, and finite state machines are preferable. This is especially true for generated code, which will usually use the finite state machine formulation. Of course, both syntaxes are of identical expressiveness, and can therefore be translated into one another.

Using regular expressions, control structures among actions become much more concise and easier to read and to specify. For example, assume we do not want to predetermine which port of the FairMerge actor is read from first. It is very easy to augment the FairMerge2 description to realize this:

Example 25 (Symmetric FairMerge). actor FairMergeSymmetric () A, B ==> C : InA : action A: [x] ==> [x] end InB : action B: [x] ==> [x] end schedule regexp (InA InB)* | (InB InA)* end end

102 © ISO/IEC 2008 – All rights reserved A.9.3 Priorities

Priorities are very different from action schedules in that they genuinely add to the expressiveness of CAL it would not be possible in general to reduce them to existing constructs, in the way schedules can in principle be reduced to a state variable and guards/assignments. Among other things, priorities allow actors to effectively test for the absence of tokens. As a consequence, actors can express non- prefix monotonic processes12, which is powerful, but at the same time can be dangerous, because it means the results computed by an actor may depend on the way it was scheduled with respect to the other actors in the system. Priorities are defined as a partial order relation over action tags, which induces a partial order relation over the actions. An action can only fire if there is no other enabled action that is higher in this partial order. The order is specified as follows:

PriorityOrder  priority{ PriorityInequality ' ;' } end PriorityInequality  ActionTag ' >' ActionTag { ' >' ActionTag}

The priority inequalities are specified over tags, i.e. they have the form t1  t 2 . These inequalities induce a binary relation on the actions are follows:  a  a  t ,t :t  t  a  tˆ  a  tˆ 1 2 1 2 1 2 1 1 2 2 a3 :a1  a3  a3  a2

The priority inequalities are valid iff the induced relation ? on the actions is an irreflexive partial order, i.e. it is antisymmetric and transitive. Transitivity follows from the definition, but antisymmetry and irreflexivity do not. In fact they do not even follow if the relation on the tags is a partial order. Consider the following example: A.B > X > A This is obviously a proper order on the tags. However, if we have two actions labeled X and A.B, then the induced relation is clearly not antisymmetric, hence the system of priority inequalities is invalid. With priorities, we can express a Merge actor that prefers one input over the other like this:

Example 26 (BiasedMerge).

actor BiasedMerge () A, B ==> C : InA: action A: [x] ==> [x] end InB: action B: [x] ==> [x] end

priority InA > InB; end end

Perhaps more interestingly, we can express a merge actor that is fair, in the sense that it will consume equal amounts of tokens from both inputs as long as they are available, but will not halt due to lack of tokens on only one of its input ports. It is also nondeterministic, i.e. it does not specify the order in which it outputs the tokens.

Example 27 (FairMerge, with priorities).

actor FairMerge3 () A, B ==> C :

12 See [10] for details on prefix monotonicity and its implications for the description of dataflow systems.

© ISO/IEC 2008 – All rights reserved 103 Both: action[x], [y] ==> [x, y] end Both: action[x], [y] ==> [y, x] end One: action A: [x] ==> [x] end One: action B: [x] ==> [x] end

priority Both > One; end end

10. A.10 Actor model

This chapter presents the formal definition of our notion of actor. In general actors may be highly complex and infinite computational structures, which can be non-deterministic, contain and manipulate internal state, and consume and produce units of data (tokens). The purpose of the CAL language (as that of any other programming language) can be seen as providing a finite representation of such structures, while exhibiting their inner structures and regularities in a way that allows tools to identify and check them, and to use them when composing actors or when generating implementations from actor descriptions.

A.10.1 Preliminaries

In the following, we assume a universe of all token values U that can be exchanged between actors. The communication along each connection between actors can be viewed as a sequential stream of tokens, and actors will remove tokens from this stream and add tokens to it. We define the set S  U * S  S  S N d e f as the set of all finite sequences over U. The set  def is the set of all finite and infinite sequences over U . We write the empty sequence as . The length of a finite sequence s is denoted by s . The elements of S (and consequently S ) are partially ordered by their prefix relation: s  r is a      r r s s prefix of , i.e. starts with the tokens of in the order they occur in . For example, abc ⊆ abcd and ab ab, but abc cabd. Note that for any s  S ,   s and s  s .  ⊆ ⊄  Many actors have multiple input and output sequences, so most of the time we will work with tuples   n n n of sequences, i.e. with elements of S or S  for some . The prefix order extends naturally to tuples as follows:    (si)i1..n  (ri)i1..n def i1..n:si  ri  n   Note that since S  and S  under the prefix order both have a least element and every chain in them has a least upper bound, they are complete partial orders. This property is relevant when relating dataflow models with a notion of firing to Kahn process networks, as in [10, 7]. In the following we use projections from S m to S n , with m  n. These are functions that extract from   an m-tuple an n-tuple such that for each value its number of occurrences in the argument is not smaller than the number of occurrences in the result. We write projections using the letter with appropriate subscripts, and use them to map the input and output of an actor onto a subset of its ports. A special kind of projection maps a tuple onto one port, say p , of the actor. We write this projection

as  p .

 

104 © ISO/IEC 2008 – All rights reserved A.10.2 Time systems

We want to allow actors to have a notion of time, without committing to any particular system of time stamps (or tags, cf.[12]). Instead, we introduce the notion of time systems, which are basically sets of time stamps with the properties and operations we need to manipulate them.

Definition 5 (Time system). A time system is a structure

(T,,z,,)

with the following properties: 1. T and  are sets of time tags and delays, respectively. 2.  is a reflexive partial order on T 3. : T   T is defined such that t  T,  Delta:t  t  4. z   is the zero delay such that t  T:t  z t.     Many time systems have a temporal metric associated with them that defines a quantitative measure  of the “distance” between two time tags. If we use a time system together with such a metric we call   it a metric time system. 

Definition 6 (Temporal metric/distance). Given a time system (T,,z,,), a partial function d:T TR

is a temporal metric of this system iff it has the following properties for all t ,t ,t  T :  1 2 3  t1  t2  d(t1,t2)  0   d(t1,t2)  d(t1,t2)  t  t  t  d(t,t ) d(t,t ) d(t ,t ) d(t,t ) 1 2 3 1 2 1 3 2 3 1 31   For any two time tags (T,,z,,) the value (T,,z,,), if defined, is called their (directed)  temporal distance.  Note. d differs from what is usually called a “metric” in several important ways. First of all it is a partial function, and is required to only quantify distances between time tags that are comparable inside the time system. Also, it allows distinct time tags to have a zero temporal distance, and it reflects the “directedness” of the time system by allowing for negative distances.   Since a temporal metric is not required to return a non-zero value for distinct time tags, there is always a trivial temporal metric for each time system, viz. the one that yields 0 for any two tags.

Example 28. Some common time systems are the following:  A familiar system would be  (R,R0 ,,) where the comparisons and operators are the common ones on the real numbers. Its temporal metric is subtraction.  A somewhat more sophisticated time system would be one that employs a notion of delta time:  n (R N,R0  N,LEX, ) The time tags are tuples of a real time stamp and an additional natural number index. Here,  T  R N,   R 0  N ,  adds two tuples positionwise, the order is the lexicographic order on the tuples, and the d  is simply subtraction in the first (real-valued) component. For instance, (3.4,2) (5,4) (1.6,6), and (1.2,50)LEX (1.21,5)LEX (1.21,6), and (1,6)  (2,5).   LEX     

© ISO/IEC 2008 – All rights reserved 105 A common metric that is used with this time system computes the difference of the first component, e.g. d((1.2,5),(1.21,50)) 0.01.  Another time system is vector time. Each time tag (and delay) is an n-ary vector of natural numbers: (Nn,Nn,n,n)  Addition is the usual vector addition as in the previous example. The comparison compares two vectors positionwise, and two vectors are only comparable iff all their components are 3 3 related to each other in the same way—e.g., (1,2,3)  (2,3,4) but (1,2,3)  (2,3,1) and also (2,3,1) 3 (1,2,3). In other words, unlike the previous two examples, this time system is not totally ordered. It is also a system that is often not associated with a temporal metric. Many possible    candidate metrics exist, however, such as sum or product of positionwise differences, or for each component of the vector the difference in that position.

A.10.3 Actor transition systems

We can now define an actor as an entity that makes atomic steps, and that in each such step consumes and produces a finite (and possibly empty) sequence of tokens at each input or output port. It also has a state (which we will not further characterize), which may change during this step.

Definition 7 (Actor transition system, actor, ATS). Let U be the universe of all values, and S  U * be the set of all finite sequences in U . For any non-empty set  of states an m-to-n actor transition system (or just actor or ATS for short) is a labeled transition system  ,,,ℏ 0      with 0 its initial state, and      Sm   Sn   its transition relation. Any (,s,,s',')   is called a transition.  is its source state, s its input tuple,  its delay, s ' its output tuple, and ' its destination state. ( s,, s') are the transition label.  Finally, ℏ is a non-reflexive, anti-symmetric and transitive partial order relation on  , called its priority relation.         Notation 1. For any transition (,s,,s',')   we also write  

 sas''

or, if  is understood or not relevant,   sas''  The set of all m-to-n actors with firing is A m n . The set of all actors is  A  A m n d e f U m , n N  The core of this definition can be found in[10], where “firing rules” defined the input tuples and a firing function mapped those to output tuples. State was added in an extension proposed in[7]. Here,  we add the priority relation, which makes actors much more expressive, by allowing them to ascertain and react to the “absence” of tokens. On the other hand, it can make them hard to analyze, and it may introduce unwanted non-determinism into a dataflow model. Intuitively, the priority relation determines that a transition cannot occur if some other transition is possible. We can see this in the definition of a valid step of an actor, which is a transition such that

106 © ISO/IEC 2008 – All rights reserved two conditions are satisfied: the required input tokens must be present, and there must not be another transition that has priority.

Definition 8 (Enabled transition, step). Given an m-to-n actor  0 ,,,p , a state    and an input tuple v  S m , a transition  sas'' is enabled iff

s  v   r a r'  sa s' r a r'    ''  : r  v  'ℏ  ''

A step from state  with input v is any enabled transition  sas''. The residual input tuple v ' is defined by v  s v'.  Note that the second condition for a transition to be enabled becomes vacuously true if ℏ  ,    leaving s  v , the usual dataflow condition. We call an actor with an empty priority relation a pure  actor.   Bibliography [1] The Ptolemy Project. Department EECS, University of California at Berke- ley (http : //ptolemy.eecs.berkeley.edu). 9 [2] Harold Abelson and Gerald Jay Sussman. Structure and Interpretation of Computer Programs. MIT Press, 2nd edition, 1999. 7 [3] Gul A. Agha. Actors: A Model of Concurrent Computation in Distributed Systems. The MIT Press Series in Artificial Intelligence. MIT Press, 1986. 9 [4] Gul A. Agha, Ian A. Mason, Scott F. Smith, and Carolyn L. Talcott. A foundation for actor computation. Journal of Functional Programming, 1993. 9 [5] J. B. Dennis. First version data flow procedure language. TechnicalMemo MAC TM 61, MIT Lab. Comp. Sci., May 1975. 9 [6] Carl Hewitt. Viewing control structures as patterns of passing messages. Journal of Artifical Intelligence, 8(3):323–363, June 1977. 9 [7] J ¨ ornW. Janneck. Syntax and Semantics of Graphs—An approach to the specifi- cation of visual notations for discrete event systems. PhD thesis, ETH Zurich, Computer Engineering and Networks Laboratory, July 2000. 9, 87, 89 [8] J ¨ orn W. Janneck. Actors and their composition. Technical Report UCB/ERL 02/37, University of California at Berkeley, 2002. 10 [9] Gilles Kahn. The semantics of a simple language for parallel program- ming. In Proceedings of the IFIP Congress. North-Holland Publishing Co., 1974. 9 [10] Edward A. Lee. A denotational semantics for dataflowwith firing. Techni- cal Report UCB/ERL M97/3, EECS, University of California at Berkeley, January 1997. 9, 82, 87, 89, 119 [11] Edward A. Lee. Embedded software. In M. Zelkowitz, editor, Advances in Computers, volume 56. Academic Press, 2002. to appear. 10 [12] Edward A. Lee and Alberto Sangiovanni-Vincentelli. A denotational framework for comparing models of computation. IEEE Transactions on Computer-Aided Design of Integrated Circuits and Systems, 17(12):1217–1229, December 1998. 10, 87 [13] Charles S. Pierce. How to make our ideas clear. In P. P. Wiener, editor, Values in a Universe of Chance. 1878. 7

© ISO/IEC 2008 – All rights reserved 107 Annex B. CAL language syntax

The following is a summary of the CAL language syntax. See chapter A.2 for some relevant conventions and the syntax of lexical tokens.

11. B.1 Actor A c t o r  [ I m p o r t s ] a c t o r I D [ ' [ ' T y p e P a r s ' ] ' ] ' ( ' A c t o r P a r s ' ) ' I O S i g [ T i m e C l a u s e ] ' : ' { V a r D e c l | A c t i o n | I n i t i a l i z a t i o n A c t i o n | P r i o r i t y O r d e r } [ A c t i o n S c h e d u l e ] { V a r D e c l | A c t i o n | I n i t i a l i z a t i o n A c t i o n | P r i o r i t y O r d e r } ( e n d | e n d a c t o r ) T y p e P a r  I D [ ' < ' T y p e ] A c t o r P a r  [ T y p e ] I D [ ' = ' E x p r e s s i o n ] I O S i g  [ P o r t D e c l s ] ' = = > ' [ P o r t D e c l s ] P o r t D e c l  [ m u l t i ] [ T y p e ] I D T i m e C l a u s e  t i m e T y p e I m p o r t  S i n g l e I m p o r t | G r o u p I m p o r t ' ; ' S i n g l e I m p o r t  i m p o r t Q u a l I D [ ' = ' I D ] G r o u p I m p o r t  i m p o r t a l l Q u a l I D Q u a l I D  I D { . I D }

T y p e  I D | I D ' [ ' T y p e P a r s ' ] '  | I D ' ( ' [ T y p e A t t r { ' , ' T y p e A t t r } ] ' ) ' | ' [ ' [ T y p e s ] ' - - > ' T y p e ' ] ' | ' [ ' [ T y p e s ] ' - - > ' ' ] '

T y p e A ttr  ID ' :' T y p e | ID ' = ' E x p re ss io n  VarDecl  [ mutable ] [ Type ] ID [ ( ' =' | ':=' ) Expression ] | FunDecl | ProcDecl 

12. B.2 Expressions  E x p r e s s io n  P r im a r y E x p r e s s io n { O p e r a to r P r im a r y E x p r e s s io n } P r im a r y E x p r e s s io n  [ O p e r a to r ] S in g le E x p r e s s io n { ' ( ' [ E x p r e s s io n s ] ' ) ' | ' [ ' E x p r e s s io n s ' ] ' | ' .' I D }



108 © ISO/IEC 2008 – All rights reserved S i n g l e E x p r e s s i o n  [ o l d ] I D | E x p r e s s i o n L i t e r a l | ' ( ' E x p r e s s i o n s ' ) ' | I f E x p r e s s i o n | L a m b d a E x p r e s s i o n | P r o c E x p r e s s i o n | L e t E x p r e s s i o n | L i s t C o m p r e h e n s i o n | S e t C o m p r e h e n s i o n | M a p C o m p r e h e n s i o n

E x p r e s s io n L ite r a l  I n te g e r L ite r a l | D e c im a lF r a c tio n L ite r a l | S tr in g L ite r a l  | tr u e | fa ls e | n u ll

I f E x p r e s s i o n  i f E x p r e s s i o n t h e n E x p r e s s i o n e l s e E x p r e s s i o n e n d L e t E x p r e s s i o n  l e t V a r D e c l s ' : ' E x p r e s s i o n ( e n d | e n d l e t )  L a m b d a E x p r e s s i o n  [ c o n s t ] l a m b d a ' ( ' [ F o r m a l P a r s ] ' ) ' [ ' - - > ' T y p e ] [ v a r V a r D e c l s ] ' : ' E x p r e s s i o n ( e n d | e n d l a m b d a ) F o r m a l P a r  [ T y p e ] I D P r o c E x p r e s s i o n  p r o c ' ( ' [ F o r m a l P a r s ] ' ) ' [ v a r V a r D e c l s ] ( d o | b e g i n ) { S t a t e m e n t } ( e n d | e n d p r o c ) F u n c D e c l  f u n c t i o n I D ' ( ' [ F o r m a l P a r s ] ' ) ' [ v a r V a r D e c l s ' : ' ] { S t a t e m e n t } e n d P r o c D e c l  p r o c e d u r e I D ' ( ' [ F o r m a l P a r s ] ' ) ' [ v a r V a r D e c l s ' : ' ] { S t a t e m e n t } e n d S e t C o m p r e h e n s i o n  ' { ' [ E x p r e s s i o n s [ ' : ' G e n e r a t o r s ] ] ' } ' L i s t C o m p r e h e n s i o n  ' [ ' [ E x p r e s s i o n s [ ' : ' G e n e r a t o r s ] [ ' | ' E x p r e s s i o n ] ] ' ] '  M a p C o m p r e h e n s i o n  m a p ' { ' [ M a p p i n g s [ ' : ' G e n e r a t o r s ] ] ' } ' M a p p i n g  E x p r e s s i o n ' - > ' E x p r e s s i o n G e n e r a t o r  f o r [ T y p e ] I D [ ' , ' I D s ] i n E x p r e s s i o n { ' , ' E x p r e s s i o n } T y p e A s s e r t i o n E x p r  ' ( ' E x p r e s s i o n ' : : ' T y p e ' ) '

13. B.3 Statements  S t a t e m e n t  A s s i g n m e n t S t m t | C a l l S t m t | B l o c k S t m t | I f S t m t | W h i l e S t m t | F o r e a c h S t m t | C h o o s e S t m t



© ISO/IEC 2008 – All rights reserved 109 A s s i g n m e n t S t m t  I D [ I n d e x | F i e l d R e f ] ' : = ' E x p r e s s i o n ' ; ' I n d e x  ' [ ' [ E x p r e s s i o n s ] ' ] ' F i e l d R e f  ' . ' I D C a l l S t m t  E x p r e s s i o n ' ( ' [ E x p r e s s i o n s ] ' ) ' ' ; ' B l o c k S t m t  b e g i n [ v a r L o c l V a r D e c l s d o ] { S t a t e m e n t } e n d I f S t m t  i f E x p r e s s i o n t h e n { S t a t e m e n t } [ e l s e { S t a t e m e n t } ] e n d

W h i l e S t m t  w h i l e E x p r e s s i o n [ v a r V a r D e c l s ] d o [ S t a t e m e n t s ] ( e n d | e n d w h i l e ) F o r e a c h S t m t  F o r e a c h G e n e r a t o r { ' , ' F o r e a c h G e n e r a t o r }  [ v a r V a r D e c l s ] d o [ S t a t e m e n t s ] ( e n d | e n d f o r e a c h ) F o r e a c h G e n e r a t o r  f o r e a c h [ T y p e ] I D { ' , ' I D } i n E x p r e s s i o n { ' , ' E x p r e s s i o n } C h o o s e S t m t  C h o o s e G e n e r a t o r { ' , ' C h o o s e G e n e r a t o r } [ v a r V a r D e c l s ] d o [ S t a t e m e n t s ] [ e l s e [ [ v a r L o c a l V a r D e c l s ] d o ] [ S t a t e m e n t s ] ] ( e n d | e n d c h o o s e ) C h o o s e G e n e r a t o r  c h o o s e [ T y p e ] I D [ ' , ' I D s ] i n E x p r e s s i o n [ ' , ' E x p r e s s i o n s ]

14. B.4 Actions

 A c t i o n  [ A c t i o n T a g ' : ' ] a c t i o n A c t i o n H e a d [ d o S t a t e m e n t s ] ( e n d | e n d a c t i o n ) A c t i o n T a g  I D { ' . ' I D } A c t i o n H e a d  I n p u t P a t t e r n s ' = = > ' O u t p u t E x p r e s s i o n s [ g u a r d E x p r e s s i o n s ] [ v a r V a r D e c l s ] [ d e l a y E x p r e s s i o n ] I n p u t P a t t e r n  [ I D ' : ' ] ' [ ' I D s ' ] ' [ R e p e a t C l a u s e ] [ C h a n n e l S e l e c t o r ] C h a n n e l S e l e c t o r  a t E x p r e s s i o n | a t * E x p r e s s i o n  | [ a t * ] a n y | [ a t * ] a l l

R e p e a t E x p r e s s i o n  r e p e a t E x p r e s s i o n O u t p u t E x p r e s s i o n  [ I D ' : ' ] ' [ ' E x p r e s s i o n s ' ] ' [ R e p e a t C l a u s e ] [ C h a n n e l S e l e c t o r ]  I n i t i a l i z a t i o n A c t i o n  [ A c t i o n T a g ' : ' ] i n i t i a l i z e I n i t i a l i z e r H e a d [ d o S t a t e m e n t s ] ( e n d | e n d i n i t i a l i z e ) I n i t i a l i z e r H e a d  ' = = > ' O u t p u t E x p r e s s i o n s [ g u a r d E x p r e s s i o n s ] [ v a r V a r D e c l s ] [ d e l a y E x p r e s s i o n ]

15. B.5 Action control

 A c t i o n S c h e d u l e  S c h e d u l e F S M | S c h e d u l e R e g E x p S c h e d u l e F S M  s c h e d u l e [ f s m ] I D ' : ' { S t a t e T r a n s i t i o n ' ; ' } ( e n d | e n d s c h e d u l e ) S t a t e T r a n s i t i o n  I D ' ( ' A c t i o n T a g s ' ) ' ' - - > ' I D { ' | ' ' ( ' A c t i o n T a g s ' ) ' ' - - > ' I D }

 110 © ISO/IEC 2008 – All rights reserved A ctionT ags  A ction T ag | A ctionT ag ' ,' A ctionT ags

ScheduleRegExp schedule regexp RegExp ( end | endschedule )

 R e g E x p  A c t i o n T a g | ' ( ' R e g E x p ' ) '  | ' [ ' R e g E x p ' ] ' | R e g E x p ' * ' | R e g E x p R e g E x p | R e g E x p ' | ' R e g E x p

PriorityOrder  priority { PriorityInequality ' ;' } end PriorityInequality  ActionTag ' >' ActionTag { ' >' ActionTag } 



© ISO/IEC 2008 – All rights reserved 111 Annex C. Keywords

The following table lists the currently active reserved words in CAL. The comment either names the context of its use or provides an example schema of its use. Note that these are not complete syntax rules. The references point to further information on the keyword.

Keyword Comment Reference action action actionhead do statements end A.8 actor actor actorhead : decls, actions, etc. end A.3 all input patterns A.8.1 output expressions A.8.2 and Boolean operator D.1D.2.9 any input patterns A.8.1 output expressions A.8.2 at input patterns A.8.1 output expressions A.8.2 at* input patterns A.8.1 output expressions A.8.2 begin procedure head begin statements end A.6.9.3 proc head begin statements end A.6.9.2 begin statements end A.7.3 choose choose generators do statements end A.7.7 const const lambda head : expression end A.6.9.1 delay action head A.8.3 div arithmetic operator D.1, D.2.6 do actions A.8 structured statements A.7.5 A.7.6 A.7.7 dom domain operator on maps D.1, D.2.5 else if expression then expression else expression end A.6.7 end universal end marker A.7.4 endaction alternative end marker, see action A.7.7 endactor alternative end marker, see actor endchoose alternative end marker, see choose endforeach alternative end marker, see foreach endfunction alternative end marker, see function endif alternative end marker, see if

112 © ISO/IEC 2008 – All rights reserved Keyword Comment Reference endinitialize alternative end marker, see initialize endlambda alternative end marker, see lambda endlet alternative end marker, see let endpriority alternative end marker, see priority endproc alternative end marker, see proc endprocedure alternative end marker, see procedure endschedule alternative end marker, see schedule endwhile alternative end marker, see endwhile false Boolean constant literal D.2.9 for for vars in expression A.6.10.2 foreach foreach generators do A.7.6 statements end fsm schedule fsm tag : A.9.2.1 transitions end function function head : expression A.6.9.3 end guard action header A.8 if if expression then expression A.6.7 else expression end if expression then statements A.7.4 else statements end import import qualid [= var] A.3.1 in membership operator, D.1D.2.1 collections generators A.6.10.2 A.7.6 A.7.7 initialize initialize head do statements A.8.5 end lambda lambda head : expression end A.6.9.1 let let vardecls : expression end A.6.8 map map { expresion [ : A.6.10 generators ] } mod modulo operator D.1D.2.6 multi multi-port declaration A.3 mutable mutable vardecl A.5.1.1 not Boolean operator D.1D.2.9 null literal A.2.1 old old var A.6.2 or Boolean operator D.1D.2.9 priority priority inequalities end A.9.3 proc proc head do statements end A.6.9.2 procedure procedure head do statements A.6.9.3 end

© ISO/IEC 2008 – All rights reserved 113 Keyword Comment Reference regexp schedule regexp regexp end A.9.2.2 repeat input patterns A.8.1 output expressions A.8.2 rng range operator on maps D.1D.2.5 schedule schedule schedule end A.9.2 then if expression then expression A.6.7 else expression end if expression then statements A.7.4 else statements end time actor head A.3 true Boolean constant literal D.2.9 var variable declarations marker while while expression do A.7.5 statements end Table 5

The following keywords are reserved for future use.

assign case default endinvariant endtask endtype ensure invariant out protocol require task type

114 © ISO/IEC 2008 – All rights reserved Annex D. Basic runtime infrastructure

This appendix describes the basic runtime infrastructure, i.e. the kinds of objects and operations on them that implementations must provide in order to implement CAL. A realistic implementation might support many more data types and have a much more extensive library of functions and procedures. Most of the facilities described in this appendix are used in CAL constructs, such as collections in generators, or maps and sets in input patterns and output expressions.

16. D.1 Operator symbols

The following table summarizes the predefined unary operator symbols in CAL.

Operator Operand type(s) Meaning not Boolean logical negation # Collection[T] number of elements Map[K, V] number of mappings dom Map[K, V] domain of a map rng Map[K, V] range of a map - Number arithmetic negation Table 6

The next table lists the predefined binary operator symbols in the CAL language. They are sorted by increasing binding strength. Their binding strength is given by a precedence figure P, higher precedence binds stronger.

P Operator Operand 1 Operand 2 Meaning 1 and Boolean Boolean logical conjunction or Boolean Boolean logical disjunction 2 = Object Object equality != Object Object inequality < Number Number less than Set[T] Set[T] String String Character Character <= analogous to < less than or equal > analogous to < greater than >= analogous to < greater than or equal 3 in T Collection[T] membership 4 + Number Number addition Set[T] Set[T] union List[T] List[T] concatenation Map[K,V] Map[K,V] map union - Number Number difference Set[T] Set[T] set difference 5 div Number Number integral division mod Number Number modulo * Number Number multiplication Set[T] Set intersection

© ISO/IEC 2008 – All rights reserved 115 P Operator Operand 1 Operand 2 Meaning / Number Number division 6 ^ Number Number exponentiation Table 7

17. D.2 Basic data types and their operations

This section shortly lists the basic data types used by CAL, describes their fundamental properties, and the operations they can be expected to support. Many of the basic data types are composite, i.e. elements of these types may be objects that contain other objects. In the cases presented in this section, such a composite can be recursively decomposed into a subobject and a residual composite, until a canonical empty composite is reached. Functions operating on such composites can often easily be described by using selector functions. A selector function takes a composite and another function as arguments. It picks one of the subobjects and passes it and the residual composite into the argument function. In the cases below, we can describe our composite data types by some properties of the data objects and the behavior of the selector function.

D.2.1 Collection[T]—collections

An object of type Collection[T] is a finite collection of objects of type T. The most fundamental operation on it is selecting an element, which is really dividing the collection into an element and a residual collection of the same kind. The selectf selector function does this, and has the following signature:

selectf: [[ A, Collection[A] --> B], Collection[A], B --> B]

Different kinds of collections may differ in a the way the selector function picks the element from the collection (e.g. in the case of lists, it is guaranteed to pick the first element of the list, see D.2.4). The two fundamental operations on a collection are computing its size and testing whether a given object is contained in it. These are represented by two operators, the prefix operator # and the infix operator in. We assume that these are substitued by function calls as follows:

#C ≡ $size(C) a in C ≡ $member(a, C)

We can define the $size and $member functions using the selectf function in the following manner:

function $size(C) : // prefix # selectf(lambda(a, C1) : 1 + $size(C1) end, C, 0) end

function $member(x, C) : selectf(lambda(a, C1) : if a = x then true else $member(x, C1) end end, C, false)

116 © ISO/IEC 2008 – All rights reserved end

Finally, we can define iteration over a collection by writing a procedure that works somewhat analogous to the selectf function: procedure selectp(p, C) begin selectf(lambda(a, C1) : proc () do p(a, C1); end end, C, proc () do end) (); end end

Consequently, the signature of selectp is as follows: selectp: [[A, Collection[A]-->], Collection[A]]

Generator functions and procedures

The selection function and procedure also allow us to construct the various functions and procedures used in defining generators in CAL. For instance, the $mapadd function used in comprehensions (see section A.6.10.2), which is slightly different on whether it produces a set, a list, or a map: function $mapaddset (C, f) : selectf(lambda (a, C1) : f(a) + $mapaddset(C1, f) end, C, {}) end function $mapaddlist (C, f) : selectf(lambda (a, C1) : f(a) + $mapaddlist(C1, f) end, C, []) end function $mapaddmap (M, f) : selectf(lambda (a, M1) : f(a) + $mapaddmap(M1, f) end, M, map {}) end

The $iterate procedure used in the foreach-statement (see section A.7.6) can be built on top of selectp as follows: procedure $iterate (C, p) begin selectp(proc (a, C1) do p(a); $iterate(C1, p); end, C); end Finally, the $try procedure used in the choose-statement (see section A.7.7) can be constructed as follows: procedure $try (C, f, p) begin selectp(proc (a, C1) do if f() then p(a); $try(C1, f, p); end end, C); end

© ISO/IEC 2008 – All rights reserved 117 D.2.2 Seq[T]—sequences

Sequences are arrangements of objects that are indexed by non-negative integers, starting at zero if not empty, and either ending at some maximal index k, or not at all. The first kind of sequence is called finite sequence or list (see section D.2.4), the second kind is called infinite sequence.

The selector function of sequences splits the non-empty sequence into its firs element and the rest of the sequence:

selectf: [[ A, Seq[A] --> B], Seq[A], B --> B]

The only operations on sequences are indexing (using a single integral index) and testing whether a given non-negative integer is a valid index. They can be expressed in terms of the selector as follows:

function $nth(n, S) : // indexer selectf(lambda (a, R) : if n = 0 then a else $nth(n - 1, R) end, S, null) end

function hasElement (n, S) : selectf(lambda (a, R) : if n = 0 then true else hasElement(n - 1, R) end, S, false) end

D.2.3 Set[T] < Collection[T]—sets

Sets are a special kind of collection (see section D.2.1) that guarantee that each object occurs in them at most once. The selection function selectf is nondeterministic on sets with two or more elements, i.e. it is unspecified which element will be chosen. Sets are partially ordered by the subset order, which can be defined as follows

function subset(S1, S2) : selectf(lambda (a, S) : if a in S2 then subset(S, S2) else false end end, S1, true) end

The comparison operators (<, <=, >, >=) are based on the subset order. In addition, two sets are considered equal if they are subsets of each other, i.e. they contain the same elements. The fundamental way of constructing sets is to add one element to an existing set. This can be done using the function addElement, which has the following signature:

118 © ISO/IEC 2008 – All rights reserved addElement : [T, Set[T] --> Set[T]]

The result of addElement (a, S) is the smallest set that contains both, a and all the elements in S. Sets also have operators for union (+), intersection (*) and set difference (-), which can be defined as follows:

function $union (S1, S2) : // infix + selectf(lambda(a, S) : addElement(a, $union(S, S2)) end, S1, {}) end

function $intersection(S1, S2) : // infix * selectf(lambda(a, S) : if a in S2 then addElement (a, $intersection (S, S2)) else $intersection (S, S2) end end, S1, {}) end

function $setDifference(S1, S2) : // infix - selectf(lambda(a, S) : if a in S2 then $setDifference (S, S2) else addElement (a, $setDifference (S, S2)) end end, S1, {}) end

In addition to these basic operations, there are set comprehensions for computing sets (section A.6.10).

D.2.4 List[T] < Collection[T], Seq[T]—lists

Lists are finite, sequential arrangements of objects. They are collections and sequences at the same time. As a consequence, its selector function splits the (non-empty) list into its first element and the rest of the list. For any list, the value computed by the $size function (section D.2.1) defines the valid indices (via $nth and hasElement, see section D.2.2) into the list which are the non-negative integers smaller than that number.

Similar to sets, lists can be constructed elementwise using a cons function, that adds an element to the front of an existing list:

cons : [T, List[T] --> List[T]]

Lists can be concatenated using the + operator, which can be defined a follows:

© ISO/IEC 2008 – All rights reserved 119 function $add (L1, L2) : // operator + selectf(lambda (a, L) : cons(a, $add(L, L2)) end, L1, L2) end

Similarly to sets, list can be created using list comprehensions (section A.6.10).

D.2.5 Map[K,V]—maps

A map of type Map[K, V] maps finitely many keys of type K to values of type V. The set of keys of a map is called its domain, while the set of values is called its range. Like collections, maps are accessed using a selector function. In the case of maps, this has the following signature:

selectf: [ [K, V, Map[K, V] --> A], Map[K, V], A --> A]

It separates a non-empty map into a key, the corresponding value, and the residual map, which it then applies its first argument to. The domain and range of a map can then be defined like this:

function $domain(M) : // operator dom selectf(lambda (k, v, M1) : addElement (k, $domain (M1)) end, M, {}) end

function $range(M) : // operator rng selectf(lambda (k, v, M1) : addElement (v, $range (M1)) end, M, {}) end

The value corresponding to a key in a map is retrieved via an indexer, using the key as in index into the map. The indexing function is defined as follows:

function $get(x, M) // indexer selectf(lambda(k, v, M1) : if x = k then v else $get(x, M1) end end, M, null) end

D.2.6 Number—numbers

Most of the number system of CAL is left to platform definition. The language itself provides two kinds of numeric literals (integers are decimal fractions). There are a number of operators on numbers, cf. section D.1 for details. The type Number contains the subtype Integer of integer numbers.

120 © ISO/IEC 2008 – All rights reserved D.2.7 Character—characters

Characters are usually related to other data types, such as numbers (via some encoding) or strings (as components of strings). The details of this are left to the definition of platforms. Characters are (possibly partially) ordered, and thus support the relational operators <, <=, >, >=.

D.2.8 String < List[Character]—strings

Strings are lists of characters, supported by a special syntax for their construction. It is left to an implementation whether they are identical to a corresponding list of characters, i.e. whether

"abc" = [’a’, ’b’, ’c’]

Strings inherit the order from characters, and expand it lexicographically

function $stringLE (s1, s2) : // operator <= selectf(lambda (c1, r1) : selectf(lambda (c2, r2) : if c1 = c2 then $stringLE(r1, r2) else c1 < c2 end end, s2, false) end, s1, true) end

D.2.9 Boolean—truth values

The Boolean data type represents truth values. It is supported by two constant literals (true and false) as well as a number of operators, cf. section D.1.

D.2.10 Null—the null value

The Null data type has only one representative, the object denoted by the literal null. Apart from quality tests, no operations are defined on this type.

© ISO/IEC 2008 – All rights reserved 121 Annex E. Naming Convention of FU

Functional Unit name convention

This section details the convention used to name the Functional Units. Each Functional Unit has a unique name. The Video Tool Library allows a Functional Unit to be simple or hierarchical. Thus two conventions must be defined.

1. Simple Functional Units name convention

The skeleton of the names is the following:

{Role}_{Name}_[property1-property2-…-propertyN]_[size]_[implementation]

{…}  compulsory […]  optional -  inside field separator _  separator between fields

Some examples: MGNT_Address_mpeg4_16x16 ALGO_Interpolation_halfpel-mpeg4 ALGO_DCRaddressing_mpeg4_8x8

2. Hierarchical Functional Units name convention

Because a network can contain different types of Functional Units (simple or hierarchical), its name cannot contain the “role” field. Thus, the skeleton of the names is the following:

{Name}_[property1-property2-…-propertyN]_[size]_[implementation]

{…}  compulsory […]  optional -  inside field separator _  separator between fields

Some examples: IDCT_2D DCR_L_16x16

3. Description of the fields

{Role} This part is compulsory. It specifies if this Functional Unit implements a specific MPEG standard algorithm or not. If the FU is a MPEG specified video coding tool, the “ALGO” tag must be used (stands for “algorithmic content”). If not, the “MGNT” tag must be used (stands for “data management”).

{Name} This part is compulsory. This field corresponds to the name of the Functional Unit. It must describe as much as possible the action performed by the Functional Unit. In case of an “ALGO” type it should relate to a standard subclause. For the other Functional Unit, one would try to put a name which can be understood by the entire MPEG community.

[property] This part is optional. It provides additional information about the Functional Unit. Several properties can be mentioned in the name.

122 © ISO/IEC 2008 – All rights reserved The property field can be used to specify: - a given characteristic of the algorithm. Example: ALGO_Interpolation_halfpel, “Halfpel” being the property field. - luminance of chrominance appliance: “L” or ”C”. Example: ALGO_Name_C or ALGO_Name_L - standard appliance: “mpeg” + {2,4,avc}. Example: “mpeg24avc”, “mpeg4avc”, “mpeg4”

[size] This part is optional. This field gives an indication concerning the amount of data the Functional Unit has been design for. For example the MGNT_Address_16x16 Functional Unit deals with blocks of 16x16 pixels.

[implementation] This part is optional. It provides any additional information about the implementation of the Functional Unit. If there are various algorithms for one Functional Unit, this field is use to distinguish them.

© ISO/IEC 2008 – All rights reserved 123